Skip to content
Related Articles

Related Articles

Want a more fast-paced & competitive environment to learn the fundamentals of Android?
Click here to head to a guide uniquely curated by our experts with the aim to make you industry ready in no time!

Android | App to Add Two Numbers

Improve Article
Save Article
Like Article
  • Difficulty Level : Basic
  • Last Updated : 22 Sep, 2021

Below are the steps for Creating a Simple Android Application to Add Two Numbers

  • STEP-1: First of all go to the xml file

  • STEP-2: Now go to the text and write the code for adding 3 textview,2 textedit and Button and Assign ID to each component. Assign margin top, left, right for the location.

  • STEP-3: Now, open up the activity java file.

  • STEP-4: Declare few variables and the values entered in the Text Views can be read by using an id which we have set in the XML code above.
  • STEP-5: Add the click listener to the Add button.
  • STEP-6: When the Add button has been clicked, add the values and store it into the sum variable.
  • STEP-7: To show the output in the result text view, set the sum in the textview.

Complete code of layout xml file and java file is given below.


<!--The activity_main.xml is a layout file
 an XML-based layout is a file that defines
 the different widgets to be used in the UI
 and the relations between those widgets
 and their containers. -->
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=""
    <!-- Text view for result view-->
        android:textStyle="bold" />
    <!--take the input first number-->
        android:inputType="number" />
    <!-- for message input first number-->
        android:text="First number"
        android:textSize="20dp" />
    <!--view message -->
        android:text="Second number"
        android:textSize="20dp" />
    <!-- take input for second number -->
        tools:ignore="MissingConstraints" />
    <!-- button for run add logic and view result -->
        android:text="ADD" />

// Each new activity has its own layout and Java files, 
// here we build the logic for adding two number 
package org.geeksforgeeks.addtwonumbers;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
    // define the global variable
    // variable number1, number2 for input input number
    // Add_button, result textView
    EditText number1;
    EditText number2;
    Button Add_button;
    TextView result;
    int ans=0;
    protected void onCreate(Bundle savedInstanceState) {
        // by ID we can use each component which id is assign in xml file
        number1=(EditText) findViewById(;
        number2=(EditText) findViewById(;
        Add_button=(Button) findViewById(;
        result = (TextView) findViewById(;
        // Add_button add clicklistener
        Add_button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // num1 or num2 double type
                // get data which is in edittext, convert it to string
                // using parse Double convert it to Double type
                double num1 = Double.parseDouble(number1.getText().toString());
                double num2 = Double.parseDouble(number2.getText().toString());
                // add both number and store it to sum
                double sum = num1 + num2;
                // set it ot result textview

After Complete layout xml file it will be shown as given below


Note: Similarly, Android App to subtract, multiply and divide numbers can be made by making minor changes in the Java and XML code.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!