0

In a simple app I have some buttons (with id.play_sound1 ... 15), and I want to detect the user click random buttons for example 5 times.

Can I do it in a simpler way than detect onclick on each button?

For example I want to display toast after user click random 5 buttons.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • I never seen 15 play buttons, can i know why you need it. I think you can minify your code. Even there would be ways to handle onClick in short code. I want to know requirement, so i can tell you best. – Khemraj Sharma May 12 '18 at 17:05

2 Answers2

0

check this out

https://stackoverflow.com/a/25905313/6998825

in your case after check the ref

you can do this

int x = 0 ;
Override
public void onClick(View v) {

switch (v.getId()) {

 // increment x here or do whatever you want
    case R.id.oneButton:
        // do your code
        break;

    case R.id.twoButton:
        // do your code
        break;

    case R.id.threeButton:
        // do your code
        break;

    default:
        break;
}

}
Hassan Badawi
  • 302
  • 1
  • 10
0

Place your buttons in array, generete random number, so that number would be an button-array index.

SAYE
  • 1,247
  • 2
  • 20
  • 47