Multiple Buttons OnClickListener() - Android


Android - Setting onClickListener for Multiple Buttons


It is very basic practice to implement onClickListener() for more than one views. But making it easier to implement and handle is different task. For some reasons, many people try to implement mulitiple onClickListener() for individual views. But you can implement interface to handle it easily. You don't have to write new onClickListener for Every Button. Just Implement View.OnClickLister to your Activity/Fragment. It will implement new Method called onClick() for handling onClick Event s for Button,TextView etc. 

You Just Simply have to Follow these steps for making it easy.


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

public class MainActivity extends AppCompatActivity implements 
View.OnClickListener {
}
After implementing 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 onClick() method to your class. It is default method called when onClick is called from view. It means that when user will perform click on your view, your class will redirect call 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 call to the onClick() method. If you don't use `this` keyword here, your call will not be redirected to onClick() method when user performs click. 

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

Step 4 : Find Buttons by ID and Implement Your Code for 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 saperating all the views by its ID. So we can implement different tasks for diferent views. It will allow you to handle all the onClick calls from one place and it will look better if you manage your code like this. 

Comments

  1. This comment has been removed by the author.

    ReplyDelete
  2. 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

Post a Comment

Popular posts from this blog

Using BottomNavigationView Android

Android - Transfer data using sync adapters