Skip to content

Instantly share code, notes, and snippets.

@kostovtd
Last active December 7, 2016 21:03
Show Gist options
  • Save kostovtd/dbbacf70012793c317ac4c3b3c1f445c to your computer and use it in GitHub Desktop.
Save kostovtd/dbbacf70012793c317ac4c3b3c1f445c to your computer and use it in GitHub Desktop.
A code snippet for overriding the behavior of AlertDialog buttons
final AlertDialog dialog = new AlertDialog.Builder(context)
.setView(v)
.setTitle(R.string.my_title)
.setPositiveButton(android.R.string.ok, null) //Set to null. We override the onclick
.setNegativeButton(android.R.string.cancel, null)
.create();
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
Button button = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// TODO Do something
}
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment