Skip to content Skip to sidebar Skip to footer

Detect Which Button Was Pressed

Here's my situation: I programmatically give some buttons an onClickListener, however, I can't fully-handle this event, because I'd like to detect which button was pressed to give

Solution 1:

If i correctly understand you can do something like this:

final View.OnClickListenersoundButtonOnClickListener=newView.OnClickListener() {
    @OverridepublicvoidonClick(View v) {
        if(v instanceof Button) {
            Buttonbutton= (Button) v;
            Stringtext= button.getText().toString();

            if(text == "1") {
                //...
            } elseif(text == "2") {

            }
            //ORswitch (text) {
                case"1": {
                    break;
                }
                case"2": {
                    break;
                }
                //...
            }
        }
    }
};

But in my opinion better use tag instead of text:

//set tag for your button
button.setTag(number); 

//use this tagIntegernumber = (Integer) v.getTag();
if(number != null) {
    //...
}

Solution 2:

Your onClick has a View v Object which should be the view that triggered the event. So this view should be your button. Now you could get the text of that button with v.getText(). Then parse the text to your number and use it.

Solution 3:

If u want to know the text of the pressed button, use this:

publicvoidonClick(View v) {
    Buttonb= (Button)v;
    StringbuttonText= b.getText().toString();

    //Do your thing with buttonText
}

Solution 4:

It's very simple to detect your button. Simply you can do like

button.setId(2000 + 0);

for reference you can check it -

for (int i = 0; i < ch.length; i++) {
     Button button = newButton(this);
     button.setTextColor(context.getResources().getColor(R.color.black));
     button.setPadding(5, 5, 5, 5);
     if (Integer.parseInt(String.valueOf(ch[i])) == 0) {
            button.setText("No");
            button.setId(2000 + 0);
            layout.addView(button, layoutParams);
            button.setOnClickListener(this);
        }
        if (Integer.parseInt(String.valueOf(ch[i])) == 1) {
            button.setText("Less");
            button.setId(2000 + 1);
            layout.addView(button, layoutParams);
            button.setOnClickListener(this);
        }
        if (Integer.parseInt(String.valueOf(ch[i])) == 2) {
            button.setText("Half");
            button.setId(2000 + 2);
            layout.addView(button, layoutParams);
            button.setOnClickListener(this);
        }
}

and in onClick

@OverridepublicvoidonClick(View v) {
    switch (v.getId()) {
        case2000:
            Log.e(TAG,"button 0 clicked");
            break;
        case2001:
            Log.e(TAG,"button 1 clicked");
            break;
        case2002:
            Log.e(TAG,"button 2 clicked");
            break;
        case2003:
            Log.e(TAG,"button 3 clicked");
            break;
    }
}

Post a Comment for "Detect Which Button Was Pressed"