Create an android application to perform addition of two numbers and display result on next activity

I have created one class called SplashActivity.I have added two numbers on this activity .I want to show the answer on the next activity but it does not show there .I have created two EditText fields then a button. When the button is clicked new activity should start and the answer of that addition should be displayed .

EditText e=(EditText)findViewById(R.id.editText3); EditText e2=(EditText)findViewById(R.id.editText4);` int num1=Integer.parseInt(e.getText().toString()); int num2=Integer.parseInt(e2.getText().toString()); int sum=num1+num2; Intent in=new Intent(this,SecondSplash.class); in.putExtra("value",sum); startActivity(in);

And Its Xml is

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.sammar.firstapplication.SplashActivity" android:background="@drawable/background"> <EditText android:id="@+id/editText3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="76dp" android:ems="10" android:inputType="numberSigned" android:hint="Enter First Number"/> <EditText android:id="@+id/editText4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="24dp" android:ems="10" android:inputType="numberSigned" android:hint="Enter Second Number" android:layout_below="@+id/editText3" android:layout_alignStart="@+id/editText3" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="46dp" android:onClick="onButtonClick" android:text="ADD" android:layout_above="@+id/button6" android:layout_alignParentStart="true" android:layout_marginBottom="51dp" />

And this is the code of SecondActivity. In this activity i have created a bundle which receives the data .

TextView txtView; String value; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second_splash); Bundle b=getIntent().getExtras(); if(b!=null) { value=b.getString("value"); } txtView =(TextView)findViewById(R.id.textView2); //value=getIntent().getExtras().getString("value"); txtView.setText(value);

and its XML is

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.sammar.firstapplication.SecondSplash"> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_alignParentTop="true" android:layout_marginStart="132dp" android:layout_marginTop="191dp" /> </RelativeLayout>

I cannot get the value of sum on the second activity. Kindly helpenter code here.

In my last example we have discussed about the textview and button. Text view to display any text. In this example we are going to create an app to add two numbers.

Create an android project and use following Text Fields control to take input from the user.

I have taken two textview to display message and one textview to display output. Two plan text field i.e EditText to take input and one button.

Design Code

<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Enter First Number:-" android:id="@+id/textView" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/editText" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/textView" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Enter Second Number" android:id="@+id/textView2" android:layout_below="@+id/editText" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/editText2" android:layout_below="@+id/editText" android:layout_toRightOf="@+id/editText" android:layout_toEndOf="@+id/editText" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Sum of Two No" android:id="@+id/button" android:layout_below="@+id/editText2" android:layout_centerHorizontal="true" android:layout_marginTop="52dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tv_result" android:layout_below="@+id/button" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" />

This screen will display like:-

Create an android application to perform addition of two numbers and display result on next activity
Figure 1

Now add a click event to button. Suppose I have created sum method in java file and map this event to click

Public void sum(View v) { }

Create an android application to perform addition of two numbers and display result on next activity
Figure 2

Now use the following code to sum of these two EditText.

public void sum(View v) { //get the edit text EditText t1=(EditText)findViewById(R.id.editText); EditText t2=(EditText)findViewById(R.id.editText2); //convert value into int int x=Integer.parseInt(t1.getText().toString()); int y=Integer.parseInt(t2.getText().toString()); //sum these two numbers int z=x+y; //display this text to TextView TextView tv_data=(TextView)findViewById(R.id.tv_result); tv_data.setText("The sum is "+z); }

Now execute the code and you will get following output:-

Create an android application to perform addition of two numbers and display result on next activity
Figure 3

In order to develop an Android App for adding two numbers first, the thing we have to do is take two inputs numbers from the user and by clicking SUM buttons which will add these two numbers.

Please follow the steps below for creating an Android app to add two numbers:

Step 1) First, go to the text field here and select the number fields for adding two numbers.

Create an android application to perform addition of two numbers and display result on next activity

Step 2) You can double-click the text box and add the text to this text field but right now text field is not required because we are not going to display any text on these text boxes but you the id is important. Id is the unique id which differentiates one phone from another. For example, this Edit Text id 1 is the id of the first number text and Edit Text id 2 is the id of the second added text.

Create an android application to perform addition of two numbers and display result on next activity

Step 3) Now to show this number text or edit we can provide the hint to this text box so for that you just select this edit text and go to the property called a hint. So there's a property called hint and here we can provide the hint to the user that what he’s expected to enter here. The hints we assign will appear on our text boxes but it will not appear as a text, it will appear as a hint. Whatever text you write here will be provided as a hint.

Create an android application to perform addition of two numbers and display result on next activity

Step 4) Now to add these numbers and to display the message, we can display a message by these plain text views or any other text views from here. We can adjust the length of the text view. For now, let’s take a large text view.

Create an android application to perform addition of two numbers and display result on next activity

Step 5) We are going to add one more button to our activity so that whenever this button is clicked, we should be able to see the results. Therefore in the widgets, just drag and drop the button.

Create an android application to perform addition of two numbers and display result on next activity

Step 6) Now our design is complete. We have two text boxes. We have a result text box or TextEdit or TextView and we have our button. We can also see them in the component tree.

Create an android application to perform addition of two numbers and display result on next activity

Step 7) Now everything is done, so we will go to our java folder and in the java folder, we are going to go inside the MainActivity.

Create an android application to perform addition of two numbers and display result on next activity

Step 8 ) In Activity.Java please create a class in order to add the import.android.view.View in your main class or main activity.java.

public void onButtonClick(View v) { } import android.view.Menu; import android.view.MenuItem; import android.view.View;

Step 9) Now inside the function we have to create a method to add the two integers for that first we have to insert Edit text and declare our integer variable, for example, num 1 and we will assign whatever user will enter in this first text box and we will convert this text to the number and then we are going to use this number to add the values. So this will give us the sum of these two numbers and then we can display this sum to our text view. So we will take t1 from which we want to send our text and then dot set text and then we set the text so this is the integer and then we need to convert this integer to text. So we will write Integer.tostring(sum). 

public class MainActivity extends { public void onButtonClick(View V) { EditText e1=(EditText) findViewByID(R.id.editText); EditText e2=(EditText) findViewByID(R.id.editText2); TextView t1=(TextView) findViewByID(R.id.textView); int num1 =Integer.parseInt(e1.getText().toString()(; int num2 =Integer.parseInt(e2.getText().toString()); int sum = num1+num2; t1.setText(Integer.toStringly(sum)); }}

Step 10) Now how can we tell this button that we want to call this method when we press this button. So what we can do is just select the button and in the Properties, we have to find property called onClick and once you are on click property here, you can just click the combo box and we will be able to see onButtonclick method automatically appearing there. So now when we go into the design view, you will see the onButtonclick method.

Create an android application to perform addition of two numbers and display result on next activity

Step 11) Now whenever we run the program, we will be the result in our emulator. Now in order to check the application please enter the Number1 and Number2 and click sum, see the result.

Create an android application to perform addition of two numbers and display result on next activity

Step 12) Now In a similar way, you can use this activity to calculate the subtraction, division or multiplication of these two numbers. So you can add some more methods and some more buttons and you can whatever you enter here, you can click the subtraction button, or addition button or multiplication button or division button. That’ll give you the same result. So this application you can simply extend it to arithmetic operations, different arithmetic operations with the numbers.