Android one OnClick method for multiple buttons?

83,987

Solution 1

If you want to reduce the coding lines then use View's OnClick() with switch statement and if you want to handle separately all click (for easily understanding and maintaining code) then use separate all button's onClick().

Update:

If you have declared Buttons in your Activity layout xml file, than write attribute android:onClick="" with same method name for all buttons and implement that method in your activity. Now you have one method for all buttons and in that method differentiate buttons with id.

Example:

<?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" >
    <Button android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:onClick="buttonOnClick"
            android:layout_height="wrap_content"
            android:text="Hello, I am a Button 1" />
    <Button android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:onClick="buttonOnClick"
            android:layout_height="wrap_content"
            android:text="Hello, I am a Button 2" />
    <Button android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:onClick="buttonOnClick"
            android:layout_height="wrap_content"
            android:text="Hello, I am a Button 3" />
</LinearLayout>

Now in your Activity implement buttonOnClick like,

public void buttonOnClick(View view)
{
 switch(view.getId())
 {
  case R.id.button1:
  // Code for button 1 click
  break;

  case R.id.button2:
  // Code for button 2 click
  break;

  case R.id.button3:
  // Code for button 3 click
  break;
 }
}

Or you can apply same switch case for dynamically added buttons in your activity, like instead of buttonOnClick you have to use implemented View's OnClickListerner's onClick.

Solution 2

Use this way:

@Override
public void onCreate(Bundle savedInstanceState) {
        button1.setOnClickListener(onClickListener);
        button2.setOnClickListener(onClickListener);
        button3.setOnClickListener(onClickListener);
}

private OnClickListener onClickListener = new OnClickListener() {
     @Override
     public void onClick(View v) {
         switch(v.getId()){
             case R.id.button1:
                  //DO something
             break;
             case R.id.button2:
                  //DO something
             break;
             case R.id.button3:
                  //DO something
             break;
         }

   }
};

Solution 3

this.btnAddFriedtoFacebook = (Button) this.findViewById(R.id.btnAddFriedtoFacebook);
this.btnAddFriedtoFacebook.setOnClickListener(this.backButtonClickListener);

public OnClickListener backButtonClickListener = new OnClickListener()
{
public void onClick(final View view)
{
   if (view == MatchInfoActivity.this.btnBack)
    {
        MatchInfoActivity.this.finish();
    }
    if( view == MatchInfoActivity.this.btnAddFried){
       Intent i = new Intent(Intent.ACTION_VIEW);
       MatchInfoActivity.this.startActivity(i);
    }
    if( view == MatchInfoActivity.this.btnAddBuddy){
       Intent i = new Intent(Intent.ACTION_VIEW);
       MatchInfoActivity.this.startActivity(i);
    }
}
};

Here is the good way.

Solution 4

I think registering onClick in xml (layout) is better approach.

EDIT:

Found related threads :

  1. Best practice for defining button events in android
  2. best practices for handling UI events
Share:
83,987
Ron Gross
Author by

Ron Gross

Updated on December 22, 2020

Comments

  • Ron Gross
    Ron Gross over 3 years

    I started program little bit in android, I have 3 buttons in a single activity.

    I saw some example codes that assign the same OnClick event to all the buttons (even if they perform completely different action) and in the method Switch(id) case case case...

    What is the better approach? one onClick method and switching or a lot of methods, one for each button?

    Thanks.