Android Button Example

In this tutorial you will get android button example.

Button is a very basic and commonly used UI widget of Android. It can be pressed or clicked and we can perform some action on its click event.

We can define a button in XML layout by using <Button> tag. In this tutorial I have given an example in which if the user presses the button then a message is displayed in Toast.

We can handle the click event on button in two ways.

  1. Using OnClickListener
  2. Using android:onClick

 

Android Button Example

OnClickListener

We can handle button click event by using OnClickListener in following way.

Create a new project with package name thecrazyprogrammer.androidexample and add following code in respective files.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btn"
        android:text="Click Here"/>
</LinearLayout>

 

MainActivity.java

package thecrazyprogrammer.androidexample;

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

public class MainActivity extends Activity {
    Button btn;

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

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

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"You Clicked Me :)",Toast.LENGTH_LONG).show();
            }
        });
    }
}

 

Screenshot

Android Button Example

 

In case you use more than one button in Activity then you have to apply OnClickListener separately on each button as shown below.

button1.setOnClickListener(new View.OnClickListener() {
	@Override
	public void onClick(View v) {
		//code for work to perform on button1 click comes here
	}
});

button2.setOnClickListener(new View.OnClickListener() {
	@Override
	public void onClick(View v) {
		//code for work to perform on button2 click comes here
	}
});

 

android:onClick

We can also handle click event by assigning a method to button in XML layout by using android:onClick attribute. When button is clicked, the associated method is called. Make sure the method is public void and accept View as a parameter. See below example to learn how to implement this.

Create a new project with package name thecrazyprogrammer.androidexample and add following code in respective files.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btn"
        android:text="Click Here"
        android:onClick="btnAction"/>
</LinearLayout>

 

MainActivity.java

package thecrazyprogrammer.androidexample;

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

public class MainActivity extends Activity {
    Button btn;

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

        btn = (Button)findViewById(R.id.btn);
    }

    public void btnAction(View view){
        Toast.makeText(MainActivity.this,"You Clicked Me :)",Toast.LENGTH_LONG).show();
    }
}

 

In above example I have used method name as btnAction. You can give its name according to you. 

In case you use more than one button in Activity then click event for each button can be handled in following way.

public void btnAction(View view){
        if(view.getId()==R.id.button1){
            //code for work to perform on button1 click comes here    
        }

        if(view.getId()==R.id.button2){
            //code for work to perform on button2 click comes here    
        }        
}

 

Comment below if you have any queries related to above android button example.

1 thought on “Android Button Example”

Leave a Comment

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