Add two number in Android

In this Article we create how to use sum of two numbers in android using Android Studio.

Step 1: Lets open the Android Studio and select the “Start a new Android Studio project”

Step 2: Then Create a Application name and hit next button.

2

Step 3: Now, choose your SDK Version and hit Next.

3
Step 4: Then choose Blank Activity and hit next.

4

Step 5: And last we create a new blank Activity with an app bar and hit Finish.

5

Step 6: Now, see the my Content_main.xml file.
//Code for ,Xml file.

<?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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.sumof2num.MainActivity"
    tools:showIn="@layout/activity_main">

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="24dp"
        android:ems="10" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText1"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="34dp"
        android:ems="10" >
        <requestFocus />
    </EditText>
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/Sum" />
</RelativeLayout>

Step 7: Then, see the my MainActivity.java file.

//Code for .java file.

package com.example.sumof2num;

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


public class MainActivity extends Activity {
            private EditText et1,et2;
            private Button buttonSum;
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);

                addListenerOnButton();

            }
            public void addListenerOnButton() {
                et1 = (EditText) findViewById(R.id.editText1);
                et2 = (EditText) findViewById(R.id.editText2);
                buttonSum = (Button) findViewById(R.id.button1);

                buttonSum.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View view) {
                        String value1 = et1.getText().toString();
                        String value2 = et2.getText().toString();
                        int a = Integer.parseInt(value1);
                        int b = Integer.parseInt(value2);
                        int sum = a + b;
                        Toast.makeText(getApplicationContext(), String.valueOf(sum), Toast.LENGTH_LONG).show();
                    }
                });
            }
}


Step 8: Now, we Click the “Run” and select the “Debug app” (Shift+9).

Step 9: Then, select the Android virtual device and hit ok.

6

Step 10: Now, My emulator is open. It means My app is Successfully Run.

emulator

Step 11: Now, Lets Check the Output.

emuresult

Tags: ,

  • Keerthana Natarajan

    can i have the program to add two numbers without using a button instead by using onfocuschangelistener