Open In App

How to Apply One Listener to Multiple Buttons in Android?

Last Updated : 06 Jun, 2021
Like Article

In this article we are going to write a shortcode for applying click events over different buttons, rather than writing different methods for different buttons, we are going to build only a single method that is onClick() for all buttons present and by using the concept of switch case we can perform different activities over different buttons. Now, without wasting further time let’s look at the implementation.

What we are going to build in this article? 

In this article, we will develop a sample application that will contain three buttons and by clicking those three buttons we can perform different actions by using only a single onClick() method. A sample video is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language.

Step by Step Implementation 

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.

Step 2: Working with the activity_main.xml file

Now it’s time to design the layout of the application. So for that go to the app >res > layout > activity_main.xml and paste the below-written code in the activity_main.xml file.


<?xml version="1.0" encoding="utf-8"?>
      <!--add three buttons in layout-->
        android:text="Button 1" />
        android:text="Button 2" />
        android:text="Button 3" />

Step 3: Working with the file

Go to the app > java > package name > file and refer to the following code. Below is the code for the file. Comments are added inside the code to understand the code in more detail.


import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    // creating three buttons 
      // by the of btn1, btn2,btn3
    Button btn1, btn2 ,btn3;
    protected void onCreate(Bundle savedInstanceState) {
        // connecting buttons with the 
          // layout using findViewById()
        btn1= findViewById(;
        btn2= findViewById(;
        btn3= findViewById(;
        // apply setOnClickListener over buttons
    // common onClick() for all buttons
    public void onClick(View v) {
        switch (v.getId()){
            // cases applied over different buttons
                // Toast message appears when button pressed
                Toast.makeText(this, "button1 pressed", Toast.LENGTH_SHORT).show();
                Toast.makeText(this, "button2 pressed", Toast.LENGTH_SHORT).show();
                Toast.makeText(this, "button3 pressed", Toast.LENGTH_SHORT).show();

That’s all, now the application is ready to install on the device. Here is what the output of the application looks like.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads