1

I got an AlertDialog that has one positive and negative button each. Within the onClick(DialogInterface dialog, int item) event handler, which is a member function of Activity, how do I tell which button has been clicked? Base on my observation, the "item" parameter has -1 as value if positive button is clicked, or -2 if it's negative button. However, I couldn't find any information on such from API doc hence I feel that this could be broken at any time.

1 Answer 1

4

AlertDialog's positive and negative buttons use the DialogInterface.OnClickListener interface to respond to selection. Like you said, the onClick method of this callback looks like this:

public void onClick(DialogInterface dialog, int which) {
  // ...
}

The which parameter indicates which button was clicked and can have, among others, the following values: 1) AlertDialog.BUTTON_POSITIVE (-1) 2) AlertDialog.BUTTON_NEGATIVE (-2)

So, you clicked the positive button if which is -1 and the negative if which is -2.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks dude. I didn't notice the static fields.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.