Last Updated: February 25, 2016
·
232
· TomTasche

default clicklistener for dialogs on Android

if you want to create a dialog that should close itself after the user pressed a button, instead of writing something like this:

Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.tutorial_title).setMessage(R.string.tutorial_message);

builder.setNeutralButton(getString(R.string.tutorial_ok), new OnClickListener() {

   @Override
   public void onClick(DialogInterface dialog, int which) {
       dialog.dismiss();
   }
});
builder.create().show();

you could just as well pass null as a OnClickListener and it will do exactly the same thing:

builder.setNeutralButton(getString(R.string.tutorial_ok), null);