Basic Android Application to Calculate the Sum of Two Numbers
In order to develop an Android App for adding two numbers first, 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.
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.
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.
Step 4) Now to add these numbers and to display the message, we can display 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.
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.
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.
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.
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.
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).
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.
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.
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 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.
Read Next About Android Application Project Basic Overview