Skip to content

Niyander Tech

Learn with fun

Menu
  • Home
  • Categories
    • Android
    • Alpine.js Components
    • Genshin Impact News
    • Jobs and Internship
    • Coursera Hub
  • Our Policies
    • Privacy Policy
    • Terms and Conditions
    • Contact Us
  • Coursera Search Engine
  • AI Tools
    • AI Background Remover
    • AI Video Downloader
    • 98+ Beautiful CSS Box-Shadow
    • G-Drive Download Link Generator
Menu
Add Two Number Program app in Android Studio

Add Two Number Program app in Android Studio

Posted on November 19, 2020September 6, 2024 by Niyander

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!

 

 

Category: Android

Post navigation

Splash Screen in Android →

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Notice

Hey guys! Need a Google Drive Direct Download Link Generator? I made a simple tool that does just that. 🚀
Convert any shared Drive link into a direct download link instantly.
Try it out now: https://niyander.com/projects/tools/gdrive/
Let me know your thoughts!

Notice

Hi everyone! I've built a collection of 98+ CSS Box-Shadow Examples for developers and designers! 🎨
Click on any card to copy the shadow — super handy for your projects.
Explore them all here: https://niyander.com/projects/tools/box-shadow/
Hope you find it useful!

Notice

Hey folks! Excited to share my new tool — an All-in-One Social Media Downloader! 📥
Download videos, photos, and audio from TikTok, YouTube, Instagram, Facebook, and more.
Check it out here: https://niyander.com/projects/tools/sm/
Your feedback is welcome!

Notice

Hey friends! I just launched a free AI Background Remover called Panda AI! 🐼
Easily remove backgrounds from images with just one click.
Try it out now: https://niyander.com/projects/tools/bg/
Let me know what you think!

May 2025
M T W T F S S
 1234
567891011
12131415161718
19202122232425
262728293031  
« Apr    

About

Greetings, Hey i am Niyander, and I hail from India, we strive to impart knowledge and offer assistance to those in need.

  • Alpine.js Components
  • Android
  • Bootstrap
  • Coursera Hub
  • Genshin Impact News
  • Jobs and Internship
  • Uncategorized

Hot Topics

  • Microsoft Azure Data Scientist Associate (DP-100) Quiz Answers + Review
  • Prepare for DP-100: Data Science on Microsoft Azure Exam Answers + Review
  • Genshin Impact Version 5.6 redeem codes
  • More Software Engineering Jobs in Japan for Expats – April 2025 Edition
© 2025 Niyander Tech | Powered by Minimalist Blog WordPress Theme