Button Example Android Studio



Hi Today's my post on How to use Button in Android .Button is a UI widget . If you are very beginner first go through my first example of Android Hello World which helps you how to run a very basic program in android.

Step 1 : Create a new Project

Button Android Studio

Step 2: Create a new activity.I named it as Button Activity

Button Android Studio

Step 3:
 Now you can see screen as activity_button.xml. Just drag two edittext widget and one button .
Go to properties and change name as ADD or anything you want .
Android Button Example
Button Android Studio

Code of activity_button.xml is as shown below :



<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" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".ButtonActivity">

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/editText1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="41dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/editText2"
        android:layout_below="@+id/editText1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="45dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add"
        android:id="@+id/button"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>


Step 4: 

In activity class ButtonActivity.java we need to make a event listener for ADD button which will take values from both edittext widgets ,add them and show result on toast.


Code for ButtonActivity.java


package com.example.sony.androidbuttonexample;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class ButtonActivity extends ActionBarActivity {
    private Button btnSum;
    private EditText edt1,edt2;

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

        addbuttonlistener();
    }

    public void addbuttonlistener()
    {


       btnSum =(Button)findViewById(R.id.button);
        edt1 = (EditText)findViewById(R.id.editText1);
        edt2 = (EditText)findViewById(R.id.editText2);

        btnSum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

             int s1=Integer.parseInt(edt1.getText().toString());
             int s2=Integer.parseInt(edt2.getText().toString());

                int s3= s1 + s2;

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

            }
        });
    }

}



Output :



Android Button Example
Button Android Studio
                        

1 comments:

This comment has been removed by the author.

Post a Comment