tags:

views:

95

answers:

3

Hi all, i need to know, how to recognize, which button is pressed. Like if i have two buttons ,say button 1 and button2,and both of them performing the same method, say method(),how to determine which button pressed ?

Regards

+1  A: 

If by "performing the same method" you mean theirs OnClickListener then you have to reference the parameter being passed to it.

public void onClick(View v) {
    if(v==btnA) {
       doA();
    } else if(v==btnB) {
       doB();
    }
}
Asahi
A: 

Ok got the solution

if (yesButton.getId() == ((Button) v).getId()) { // remainingNumber } else if (noButton.getId() == ((Button) v).getId()) { // it was the second button }

shishir.bobby
+2  A: 

Most ellegant pattern to follow:

public void onClick(View v) {
switch(v.getId())
{
case R.id.button_a_id:
// handle button A click;
break;
case R.id.button_b_id:
// handle button B click;
break;
default:
throw new RuntimeException("Unknow button ID");
}

This way it's much simplier to debug it and makes sure you don't miss to handle any click.

Paul Turchenko