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?
Asked
Active
Viewed 83 times
-4
-
1Duplicate https://stackoverflow.com/questions/25905086/multiple-buttons-onclicklistener-android – aslamhossin Aug 11 '17 at 06:52
-
you can use Listview or recycle view to make 60 buttons. – Chetan Joshi Aug 11 '17 at 06:53
-
Use recycler view to make 60 buttons – seema Aug 11 '17 at 07:10
2 Answers
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