0% found this document useful (0 votes)
6 views3 pages

Java Code for Adding Two Numbers

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Java Code for Adding Two Numbers

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Adding two numbers

Javafile

public class MainActivityextends AppCompatActivity{

EditTextinput_num1,input_num2;
TextView displayResult;
Button btn_sum,btn_change;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);

input_num1= (EditText) findViewById([Link].editText1);


input_num2= (EditText) findViewById([Link].editText2);
displayResult=(TextView) findViewById([Link]);
btn_sum=(Button) findViewById([Link].btn_result);
//btn_change=(Button) findViewById([Link].btn_reset);

btn_sum.setOnClickListener(new [Link]() {
@Override
public void onClick(View view) {
double
num1=[Link](input_num1.getText().toString());
double
num2=[Link](input_num2.getText().toString());
double sum = num1 + num2;
[Link]([Link](sum));

}
});

Xmlcode

<?xml version="1.0" encoding="utf-8"?>


<LinearLayoutxmlns:android="[Link]
android"
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
tools:context=".MainActivity">

<TextView
android:id="@+id/display"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textSize="22sp" />

<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:hint="@string/first_text"
android:inputType="numberDecimal"
android:textSize="22sp"

/>

<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:hint="@string/second_text"
android:inputType="numberDecimal"
android:textSize="22sp"

/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp"
>

<Button
android:id="@+id/btn_result"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="20dp"
android:text="@string/btn_text"
android:textSize="22sp" />

<Button
android:id="@+id/btn_reset"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="20dp"
android:text="@string/btn_reset"
android:textSize="22sp"

/>

</LinearLayout>

</LinearLayout>

You might also like