Multiple Buttons OnClickListener() - Android

Learn about how to set an on-click listener for multiple buttons on the Android

Photo by Gal Shir

It is a very basic practice to implement onClickListener() for multiple views in Activity, Fragments or RecyclerView. But making it easier to implement and handle is a different task. For some reason, many people try to implement multiple onClickListener() for individual views. But you can implement the interface to handle it easily. You don't have to write a new onClickListener for every button. Just Implement View.OnClickListner to your Activity/Fragment. It will add a new method to your class called onClick() for handling click events for Button, TextView, etc.

You Just Simply have to Follow these steps to make it easy.

Step 1: Implement OnClickListener() interface in your Activity/Fragment

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    	// TODO
}  

After implementing an interface to your Activity/Fragment it will ask you to implement its method onClick().

Step 2: Implement onClick() method in your Activity/Fragment

public class MainActivity extends AppCompatActivity implements View.OnClickListener {       

    @Override
    public void onClick(View v) {
     // handling onClick Events
    }
}

Here, you can see that it added the onClick() method to your class. It is the default method called when onClick is called from view. It means that when the user clicks on your view, your class will redirect calls to this method.

Step 3: Implement OnClickListener() For Buttons

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

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

        Button one = (Button) findViewById(R.id.buttonOne);
        one.setOnClickListener(this); // calling onClick() method
        Button two = (Button) findViewById(R.id.buttonTwo);
        two.setOnClickListener(this); 
        Button three = (Button) findViewById(R.id.buttonThree);
        three.setOnClickListener(this); 
    }

    @Override
    public void onClick(View v) {
    }
}

Here we are adding this keyword to the onClickListener() method. It redirects the call to the onClick() method. If you don't use this keyword here, your call will not be redirected to the onClick() method when a user performs a click.

Note that: If you haven't implemented View.OnClickListener interface to your class will show an error when you implement this keyword to the buttons onClickListener() method.


Step 4: Find Buttons by ID and Implement Your Code for the button click.

public class MainActivity extends AppCompatActivity implements 
View.OnClickListener {
       
    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.buttonOne:
                // code for button when user clicks buttonOne.
                break;

            case R.id.buttonTwo:
                // do your code
                break;

            case R.id.buttonThree:
                // do your code
                break;

            default:
                break;
        }
    }
}

Here, we are separating all the views by their ID. So we can implement different tasks for different views. It will allow you to handle all the onClick calls from one place and it will help you to make your code look clean and more readable.


Check our latest articles:


Thanks for reading this article. Hope you would have liked it!. Please share and subscribe to this blog to support.

Pragnesh Ghoda

A forward-thinking developer offering more than 8 years of experience building, integrating, and supporting android applications for mobile and tablet devices on the Android platform. Talks about #kotlin and #android

8 Comments

Please let us know about any concerns or query.

  1. Would u please do one with just two buttons to set on different activites

    ReplyDelete
    Replies
    1. You can use same type of implementation for both activities. Implement `OnClickListener` interface in activity/fragment. Override `onClick()` method, and assign that listener with `setOnClickListener()` method of buttons.

      Delete
  2. what can i do if i want to do same thing with 3 button . this code dose not work for same thing

    ReplyDelete
    Replies
    1. use same method or call for those 3 buttons by passing same case into switch.

      case R.id.buttonOne:
      case R.id.buttonTwo:
      case R.id.buttonThree:
      // code for button when user clicks buttonOne.
      break;

      Delete
  3. I Get java. Lang. Runtime exception

    ReplyDelete
  4. I have an error that says "Constant expression required." What can I do about this?

    ReplyDelete
Previous Post Next Post

Contact Form