loader image

Android Program to Add Two Numbers

Write an android program to add two numbers

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_centerHorizontal="true"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textview1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="Enter number 1"
        android:textSize="18sp" />

    <EditText
        android:id="@+id/first"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/textview1"
        android:layout_alignParentTop="false"
        android:layout_alignParentRight="false"
        android:layout_centerHorizontal="true"
        android:ems="10"
        android:inputType="number" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/first"
        android:layout_centerHorizontal="true"
        android:text="Enter Number 2"
        android:textSize="18sp" />

    <EditText
        android:id="@+id/second"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/textView2"
        android:layout_centerHorizontal="true"
        android:ems="10"
        android:inputType="number" />

    <Button
        android:id="@+id/buttonadd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/second"
        android:layout_centerHorizontal="true"
        android:text="Add" />

    <TextView
        android:id="@+id/result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/buttonadd"
        android:layout_centerHorizontal="true"
        android:textSize="24sp" />

</RelativeLayout>

MainActivity.java

package org.teachics.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;

public class MainActivity extends AppCompatActivity {
EditText firstnum,secondnum;
TextView r;
Button bt;
double a,b,c;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        firstnum=(EditText) findViewById(R.id.first);
        secondnum=(EditText) findViewById(R.id.second);
        bt=(Button) findViewById(R.id.buttonadd);
        r=(TextView) findViewById(R.id.result);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                a=Double.parseDouble(firstnum.getText().toString());
                b=Double.parseDouble(secondnum.getText().toString());
                c=a+b;
                r.setText("Sum="+c);
            }
        });
    }
}

Output

android program to add two numbers
Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Atul

nice code

Scroll to Top