0

I'm making a little reactionspeed app in Android Studio, which checks whether the user clicks the button that matches a randomly generated number. If the correct button was pressed, it executes the NewRound method.

Currently, I have this:

public void ButtonOneClick(View view) {
    if (rnd == 1) {
        NewRound(view);
    }
}


public void ButtonTwoClick(View view) {
    if (rnd == 2) {
        NewRound(view);
    }
}

public void ButtonThreeClick(View view) {
    if (rnd == 3) {
        NewRound(view);
    }
}

public void ButtonFourClick(View view) {
    if (rnd == 4) {
        NewRound(view);
    }
}

public void ButtonFiveClick(View view) {
    if (rnd == 5) {
        NewRound(view);
    }
}

Would there be a shorter and more efficient way to do this?

user14678216
  • 2,886
  • 2
  • 16
  • 37
  • Yes, there is... One listener and checking view id... Like `arrayOfButtonaIds[rnd-1]==view.getId()` – Selvin Jan 06 '21 at 13:04
  • First, method names in Java should be `camelCased`, and not `UpperCamelCased`. I think you are looking for the switch statement to match against the button ID, in just one method. – Deadbeef Jan 06 '21 at 13:19
  • 2
    @Deadbeef You must not switch ressource ids: http://tools.android.com/tips/non-constant-fields – The incredible Jan Jan 06 '21 at 13:46
  • No. cause, you are clicking on different button not a button. Every button must have separated listener. So, answer is `no` –  Jan 06 '21 at 15:44
  • @istiak *Every button must have separated listener.* this is not true, so answer is **yes** – Selvin Jan 06 '21 at 18:44
  • @Selvin prove it.. –  Jan 06 '21 at 20:42
  • Does this answer your question? [Multiple Buttons' OnClickListener() android](https://stackoverflow.com/questions/25905086/multiple-buttons-onclicklistener-android) – Muhannad Fakhouri Jan 07 '21 at 08:29
  • @Istiak you prove that it is not possible – Selvin Jan 07 '21 at 11:11

2 Answers2

0

Yes, there is. Just register all buttons to the same onClick Listener in the code of your Activity. The Activity therefore has to implement View.onClickListener. The interface then requires you to implement onClick method. The following example may solve your problem:

@Override
public void onClick(View view) {
    if(button0 == view && rnd == 2){ // checks which button was pressed
        NewRound(view);
    }

    if(button1 == view && rnd == 3){
        NewRound(view);
    }

    if(button2 == view && rnd == 4){
        NewRound(view);
    }
}

If any of the buttons in your activity are pressed the onClick method will be called. The if conditions will make sure that the method NewRound(view) is only called in the correct case.

JF2602
  • 1
  • 1
  • 1
0

Of course, there are simpler ways of doing things. Instead of using multiple onClick() methods you can use just one by doing this, In any of your activity you can implements the onClickListener.

public class MainActivity extends AppCompatActivity implements View.onClickListener{

Button button1, button2, button3,button4;

@Override
protected void onCreate(Bundle saveInstanceState){

super.onCreate(saveInstanceState);
setContentView(R.layout. activity_main);

button1 = findViewById(R.id.button1_ID);
button2 = findViewById(R.id.button2_ID);
button3 = findViewById(R.id.button3_ID);
button4 = findViewById(R.id.button4_ID);

button1.setOnclickListener(this);
button2.setOnclickListener(this);
button3.setOnclickListener(this);
button4.setOnclickListener(this);

}

@Override
public void onClick(View v){

int select_button = 0;
if(v.getId() == R.id.button1_ID){select_button=1; NewRound(view); }

if(v.getId() == R.id.button2_ID){select_button=2 ;NewRound(view); }

if(v.getId() == R.id.button3_ID){select_button=3 ;NewRound(view); }

if(v.getId() == R.id.button4_ID){select_button=4; NewRound(view); }

}}

You can also use switch() method inside onClick method.

@Override
public void onClick(View v){

switch(v.getId()){
case R.id.button1_ID: NewRound(view); break;
case R.id.button2_ID: NewRound(view); break;
case R.id.button3_ID: NewRound(view); break;
case R.id.button4_ID: NewRound(view); break;

}
}
Neldison
  • 48
  • 7
  • Do not use switch case with view ids as in future release of android build tool they will not be constant... See link from The incredible Jan's comment under the question – Selvin Jan 07 '21 at 06:11