josephoneill
josephoneill

Reputation: 853

Android: Many Buttons, one OnClickListener?

I"m currently working on an android application, and had a few questions.

1) In my app, there is a Battery Mod section. Their will be over 50 buttons for it. So, instead of doing something like:

 public class MyActivity extends Activity implements OnClickListener {

  @Override public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);
     Button buttonA = (Button) findViewById(R.id.buttonA);    
     buttonA.setOnClickListener(this);
     Button buttonB = (Button) findViewById(R.id.buttonB); 
     buttonB.setOnClickListener(this);
  }

//etc... etc...

public void onClick(View v) {

  switch (v.getId()) {
     case R.id.buttonA: 
      // do something
      break;
     case R.id.buttonB:
      // do something else
      break;
  }
}  
}

Is there a simpler way to do this? When you click the battery mod button,it will display an image of the mod, along with an option to install it. Other than the image, it'll be the same with each mod. If there isn't a simpler way.

Upvotes: 1

Views: 3474

Answers (4)

Diego Rodriguez
Diego Rodriguez

Reputation: 98

You could set the onClick callback of each button in the layout file. Something like:

<Button
    android:id="@+id/buttonId"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/buttonText"
    android:onClick="onItemClick" />

Then, you add in your Activity/Fragment:

public void onItemClick(View view) {
    // Do something in response to button click
}

More information here: http://developer.android.com/guide/topics/ui/controls/button.html#HandlingEvents

Upvotes: 2

Phil
Phil

Reputation: 36289

One useful component to all Views that is not discussed much for this purpose is the tag attribute. You can set the View's tag to any Object:

Object foo = new Object();
button1.setTag(foo);

You can use this to easily assign tag to the name of the image, or perhaps the id of the Drawable resource. Then use one onClick method for all of these buttons, and simply unpack the tag to determine how it should be used:

public void onClick(View v)
{
    //any button was clicked. Now determine what to do using the tag.

    Object tag = v.getTag();
    //TODO: use tag.


}

Upvotes: 3

323go
323go

Reputation: 14274

You could iterate through your buttons like so --

int[] buttons = { R.id.button1, R.id.button2, ... , R.id.button30 };

for( int i=0; i < buttons.length; i++ ) {
    final Button b = (Button) findViewById( buttons[ i ] );
    b.setOnClickListener( ... );
}

Upvotes: 1

wtsang02
wtsang02

Reputation: 18863

What differs is the id. So you can't loop through that, and you will need to type it out like:

private static final BUTTON_IDS={R.id.buttonA,R.id.buttonB,R.id.buttonC....R.id.buttonZ};

then loop it:

    for(int i=0;i<BUTTON_IDS.lenght;i++{
        Button buttonXYZ = (Button) findViewById(BUTTON_IDS[i]);    
        buttonXYZ.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                    if(...){
                        //your spical cases
                    }
                    else{
                        //do awesome generic stuff.
                    }

                    }
                });
    }

Upvotes: 0

Related Questions