-4

I have about 60 buttons and I want when click on any on them to do the same actions: get the text of the clicked button. check the text and if is correct disable it, otherwise display a toast. I want to avoid to have a switch with 60 cases. Is there a shorter way to do it?

2 Answers2

1

Use ButterKnife and implememnt onclick as:

@OnClick({ R.id.button1, R.id.button, R.id.button})//as many you want
public void onButtonClick(Button button) {
  if (button.getText().equals("correctText")) {
    //Do whatever you want
  } else {
    Toast.makeText(this, "message", LENGTH_SHORT).show();
  }
}
Charu
  • 1,055
  • 13
  • 18
0

In layout xml add your onClick method name like this

 <Button    android:layout_width="wrap_content"
            android:onClick="buttonOnClick"
            android:layout_height="wrap_content"/>

and in class file or Activity use like this

public void buttonOnClick(View view)
{
      //do your functionality here
}
Rahul
  • 1,380
  • 1
  • 10
  • 24