Reputation: 2167
How would I set up the onClickListener for a button inside an alertDialog? Do I just use the onClick of the whole activity or do I make a new one inside the alertDialog builder?
EDIT: Sorry I didn't make it clear first time but this isn't for the positive/negative buttons. This is for a button within a custom xml.
Thanks in advance
Upvotes: 0
Views: 98
Reputation: 1550
If you are using alert dialog then, create button in alertdialog and set Dialoginterface.onClicklistner.
Or If you are creating custom dialog with your own view then set click listener on button.
Upvotes: 0
Reputation: 152
If you are wanting a standard button to dismiss the dialog then @Sergio is correct. If you are using a custom xml layout:
Use the findViewById and attach a specific listener for that button. Using an activity wide click handler would cause you problems if you were to add another button.
Android docs shows this as:
final Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
http://developer.android.com/reference/android/widget/Button.html
Upvotes: 1
Reputation: 655
If you are using a builder for building the alert dialog, then set the new button similar to this:
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//your code
dialog.dismiss();
}
});
Upvotes: 0