Addition of two numbers in Android

This is a basic program for Addition of two numbers in Android

Steps to add two numbers

  1. Create a new Android project in Android Studio.
  2. Open the activity_main.xml file and add two EditText or NumberText views for the user to input the numbers and a Button view for the user to initiate the addition process.
  3. In the file, declare the two EditText views and the Button view as class-level variables.
  4. In the onCreate() method of the file, initialize the EditText views and the Button view by finding them by their respective IDs.
  5. Set an OnClickListener on the Button view to listen for when the user clicks it.
  6. In the onClick() method of the OnClickListener, get the text from the EditText views, convert them to integers, add them together, and display the result in a Toast message or in a TextView on the screen.

Addition of two numbers in Android
Addition of two numbers in Android


  1. It defines an Android activity called MainActivity.
  2. In the onCreate method, it sets the content view to the layout defined in R.layout.activity_main.
  3. It initializes three UI elements:
    • number1: An EditText widget for entering the first number.
    • number2: An EditText widget for entering the second number.
    • result: A TextView widget for displaying the result of the addition.
  4. It initializes a Button called add and sets an OnClickListener for it.
  5. When the “add” button is clicked, it does the following:
    • It retrieves the values entered in number1 and number2 EditText widgets as strings.
    • It converts these string values to integers using Integer.parseInt().
    • It calculates the sum of num1 and num2.
    • It sets the TextView (result) to display the calculated sum.
    • It also displays a toast message with the sum as a notification.

To validate we will use the Saripaar library.

Add this library to the build.gradle in the dependencies section as below

Add android-saripaar:2.0.3
Add android-saripaar:2.0.3

To use Saripaar in the program

  1. Implement the Interface Validator.ValidationListener
  2. Override two methods public void onValidationSucceeded() and public void onValidationFailed(List errors)

Overridden implementation is given below.

Hope you learn Addition of two numbers in Android.

Read More

Simple Interest Calculator in Android

Android Studio Hello World First Example