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 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.

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

Activity_main.xml



    
    

MainActivity.java

package com.ebhor.addition;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {
    EditText number1;
    EditText number2;
    TextView result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        number1 = (EditText) findViewById(R.id.editTextNumber);
        number2 = (EditText) findViewById(R.id.editTextNumber2);
        result = (TextView) findViewById(R.id.textView2);
        Button add = (Button) findViewById(R.id.button);
        add.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                int num1 = Integer.parseInt(number1.getText().toString());
                int num2 = Integer.parseInt(number2.getText().toString());
                int sum = num1 + num2;
                result.setText("Sum :" + sum);
                Toast.makeText(MainActivity.this, "Sum :"+sum, Toast.LENGTH_SHORT).show();
            }
        });
    }

}
  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:
  4. It initializes a Button called add and sets an OnClickListener for it.
  5. When the “add” button is clicked, it does the following:

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.

package com.ebhor.addition;

import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.mobsandgeeks.saripaar.ValidationError;
import com.mobsandgeeks.saripaar.Validator;
import com.mobsandgeeks.saripaar.annotation.NotEmpty;

import java.util.List;


public class MainActivity extends Activity implements Validator.ValidationListener {
    @NotEmpty(message = "Please Enter number1")
    EditText number1;
    @NotEmpty(message = "Please Enter number2")
    EditText number2;
    TextView result;
    Validator validator;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        number1 = (EditText) findViewById(R.id.editTextNumber);
        number2 = (EditText) findViewById(R.id.editTextNumber2);
        result = (TextView) findViewById(R.id.textView2);
        Button add = (Button) findViewById(R.id.button);
        validator = new Validator(this);
        validator.setValidationListener(this);

        add.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                validator.validate();
            }
        });
    }

    public void addNumbers() {
        int num1 = Integer.parseInt(number1.getText().toString());
        int num2 = Integer.parseInt(number2.getText().toString());
        int sum = num1 + num2;
        result.setText("Sum :" + sum);
        Toast.makeText(MainActivity.this, "Sum :" + sum, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onValidationSucceeded() {
        addNumbers();
    }

    @Override
    public void onValidationFailed(List errors) {
        for (ValidationError error : errors) {
            View view = error.getView();
            String message = error.getCollatedErrorMessage(this);

            // Display error messages ;)
            if (view instanceof EditText) {
                ((EditText) view).setError(message);
            } else {
                Toast.makeText(this, message, Toast.LENGTH_LONG).show();
            }
        }
    }

}

Hope you learn Addition of two numbers in Android.

Read More

Simple Interest Calculator in Android

Android Studio Hello World First Example