Android

Add Two Number Program app in Android Studio

In this blog post i am gone to share the process of creating an Android app using Android Studio that adds two numbers. This is a beginner-friendly project to help you understand the basic structure of an Android app. By the end of this, you’ll have an app where users can input two numbers, click a button, and see the sum displayed on the screen. / Add Two Number Program app in Android Studio

Add Two Number Program app in Android Studio

The app will look simple, with two input fields for numbers, a button to trigger the addition, and a text view to display the result.

Activity_main.xml
This XML file contains the layout of the app, including the input fields, button, and result text view. Copy and paste the following code into your activity_main.xml file.

<?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"
android:background="#FFFFFF"
tools:context=".MainActivity">

<TextView
android:id="@+id/textView5"
android:layout_width="304dp"
android:layout_height="91dp"
android:layout_marginTop="55dp"
android:gravity="center"
android:text="@string/add_two_numbers"
android:textColor="#E91E63"
android:textSize="36sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<EditText
android:id="@+id/etNum1"
android:layout_width="247dp"
android:layout_height="66dp"
android:layout_marginTop="56dp"
android:gravity="center"
android:hint="First Number"
android:inputType="number"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/textView5" />

<EditText
android:id="@+id/etNum2"
android:layout_width="247dp"
android:layout_height="66dp"
android:layout_marginTop="48dp"
android:gravity="center"
android:hint="Second Number"
android:inputType="number"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/etNum1" />

<Button
android:id="@+id/btnadd"
android:layout_width="234dp"
android:layout_height="65dp"
android:layout_marginTop="48dp"
android:text="Add"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/etNum2" />

<TextView
android:id="@+id/tvanswer"
android:layout_width="271dp"
android:layout_height="82dp"
android:layout_marginTop="172dp"
android:gravity="center"
android:text="Answer"
android:textSize="36sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/btnadd" />
</androidx.constraintlayout.widget.ConstraintLayout>

 

MainActivity.java
This Java file handles the logic for the app. Copy and paste the following code into your MainActivity.java file:

package com.example.add;
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;

public class MainActivity extends AppCompatActivity {
private EditText num1;
private EditText num2;
private Button add;
private TextView result;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

num1 = findViewById(R.id.etNum1);
num2 = findViewById(R.id.etNum2);
add = findViewById(R.id.btnadd);
result = findViewById(R.id.tvanswer);

add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int number1 = Integer.parseInt(num1.getText().toString());
int number2 = Integer.parseInt(num2.getText().toString());
int sum = number1 + number2;
result.setText("Answer is = " + sum);
}
});
}
}

 

strings.xml
If you want to customize the name of the app and other strings, update the strings.xml file like this:

<resources>
<string name="app_name">Add Two Numbers</string>
<string name="add_two_numbers">Add Two Numbers</string>
</resources>

 

Changing the App Icon

  1. Go to an App Icon Generator site and generate your custom icon.
  2. Download the icon files and unzip them.
  3. In Android Studio, navigate to your app’s res folder, and replace the current mipmap files with the ones from the icon generator.
  4. Update the AndroidManifest.xml to point to your new icon:
<application
android:icon="@mipmap/addicon"
android:roundIcon="@mipmap/addicon"
android:label="@string/app_name"
android:theme="@style/Theme.Add">

 

By following these steps, you’ll have a fully functional Android app that can add two numbers. Keep experimenting with the design and functionality to learn more!