This is a basic program for Addition of two numbers in Android
Steps to add two numbers
- Create a new Android project in Android Studio.
- 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.
- In the MainActivity.java file, declare the two EditText views and the Button view as class-level variables.
- In the onCreate() method of the MainActivity.java file, initialize the EditText views and the Button view by finding them by their respective IDs.
- Set an OnClickListener on the Button view to listen for when the user clicks it.
- 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="38dp" android:text="Addition of two numbers" android:textSize="34sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.496" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_wrapBehaviorInParent="horizontal_only" /> <Button android:id="@+id/button" android:layout_width="295dp" android:layout_height="65dp" android:layout_marginTop="268dp" android:text="ADD" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.491" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:toggleCheckedStateOnClick="false" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="368dp" android:text="Sum :" android:textSize="34sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.199" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <EditText android:id="@+id/editTextNumber" android:layout_width="296dp" android:layout_height="62dp" android:layout_marginTop="108dp" android:ems="10" android:inputType="number" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.495" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <EditText android:id="@+id/editTextNumber2" android:layout_width="295dp" android:layout_height="58dp" android:layout_marginTop="196dp" android:ems="10" android:inputType="number" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.491" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> |
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | 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(); } }); } } |
- It defines an Android activity called
MainActivity
. - In the
onCreate
method, it sets the content view to the layout defined inR.layout.activity_main
. - It initializes three UI elements:
number1
: AnEditText
widget for entering the first number.number2
: AnEditText
widget for entering the second number.result
: ATextView
widget for displaying the result of the addition.
- It initializes a
Button
calledadd
and sets anOnClickListener
for it. - When the “add” button is clicked, it does the following:
- It retrieves the values entered in
number1
andnumber2
EditText widgets as strings. - It converts these string values to integers using
Integer.parseInt()
. - It calculates the sum of
num1
andnum2
. - It sets the
TextView
(result
) to display the calculated sum. - It also displays a toast message with the sum as a notification.
- It retrieves the values entered in
To validate we will use the Saripaar library.
Add this library to the build.gradle
in the dependencies section as below
To use Saripaar in the program
- Implement the Interface
Validator.ValidationListener
- Override two methods
public void onValidationSucceeded()
andpublic void onValidationFailed(List errors)
Overridden implementation is given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | 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<ValidationError> 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.