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 MainActivity.java file, declare the two EditText views and the Button view as class-level variables.
  4. In the onCreate() method of the MainActivity.java 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.

Activity_main.xml

MainActivity.java

Adding validation in

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.