Working With Buttons In Android

Introduction

This article explains how to work with buttons in Android. Android Studio is used to create the sample.

First create an XML file and add a button by dragging and dropping one and by creating the id in the XML file. In this I am adding one button and two editTexts in the XML file so on a button click you will see the sum of the values that you will enter in the editText.

Step 1

Use the following procedure to create the sample application.

Click on "File" -> "New Project".

button1

Click the "Next" button.
projectcreation2

Click the "Next"
button.

projectcreation3 


Click the "Next" button.

 CreateProject4


Click the "Finish" button.

Step 2

Create an XML file with the following.

In this first you will create an XML file and add a button by dragging and dropping and by creating an id in the XML file. In this I am adding a button and two editTexts in the XML file. On a button click you will see the sum of the values that you have entered into the editText.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context=".MainActivity" >

 

    <EditText

        android:id="@+id/editText1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignParentTop="true"

        android:layout_marginTop="24dp"

android:layout_centerHorizontal="true"

        android:ems="10" />

 

    <EditText

        android:id="@+id/editText2"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_marginTop="100dp"

        android:layout_centerHorizontal="true"

        android:ems="10" >

 

        <requestFocus />

    </EditText>

 

    <Button

        android:id="@+id/btn_add"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_centerHorizontal="true"

        android:layout_centerVertical="true"

        android:text="ADD" />

 

</RelativeLayout>


Step 3

Create a Java class file with the following.

In the Java class file create the id of the editText and button. Set the button on its click listener. The onclick event will do the addition operation of a number. When you get the numbers from the edittext, the numbers are of the editText type so youi first need to parse the number in Integer type. After getting both the numbers add thr numbers and set the result of the addtion of both numbers in a toast.

 

package com.customcheckbox;

 

import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast;

 

public class MainActivity extends Activity {

    private EditText editNumber1,editNumber2;

    private Button btn_add;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

 

        addListenerOnButton();

 

    }

    public void addListenerOnButton(){

        editNumber1=(EditText)findViewById(R.id.editText1);

        editNumber2=(EditText)findViewById(R.id.editText2);

        btn_add=(Button)findViewById(R.id.btn_add);

 

        btn_add.setOnClickListener(new OnClickListener(){

 

            @Override

            public void onClick(View view) {

                String value1=editNumber1.getText().toString();

                String value2=editNumber2.getText().toString();

                int num1=Integer.parseInt(value1);

                int num2=Integer.parseInt(value2);

                int sum=num1+num2;

                Toast.makeText(getApplicationContext(),String.valueOf(sum),Toast.LENGTH_LONG).show();

            }

 

        });

 

    }

    @Override

    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.

        getMenuInflater().inflate(R.menu.main, menu);

        return true;

    }

}


Step 4

Android Manifest.xml file

 

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.customcheckbox"

    android:versionCode="1"

    android:versionName="1.0" >

 

    <uses-sdk

        android:minSdkVersion="7"

        android:targetSdkVersion="16" />

 

    <application

        android:allowBackup="true"

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name"

        android:theme="@style/AppTheme" >

        <activity

            android:name="com.customcheckbox.MainActivity"

            android:label="@string/app_name" >

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

 

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

    </application>

</manifest>

Step 5

Addition after a button click:


output
 


output2
 

Up Next
    Ebook Download
    View all
    Learn
    View all