Skip to content

Instantly share code, notes, and snippets.

@chrishuan9
Created April 24, 2012 17:43
Show Gist options
  • Save chrishuan9/2481886 to your computer and use it in GitHub Desktop.
Save chrishuan9/2481886 to your computer and use it in GitHub Desktop.
Android Alert Dialog
AlertDialog ad = new AlertDialog.Builder(this).create();
ad.setCancelable(false); // This blocks the 'BACK' button
ad.setMessage("Hello World");
ad.setButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
ad.show();
//AlertDialog with Button
AlertDialog deleteAlert = new AlertDialog.Builder(this).create();
deleteAlert.setTitle("Delete List");
deleteAlert.setMessage("Are you sure you want to delete this list?");
deleteAlert.setButton("Yes", new OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
//...
}
});
deleteAlert.setButton2("No", new OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
//...
}
});
deleteAlert.show();
//AlertDialog with items
final String [] items=new String []{"Item 1","Item 2","Item 3","Item 4"};
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setTitle("Items alert");
builder.setItems(items, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
TextView txt=(TextView)findViewById(R.id.txt);
txt.setText(items[which]);
}
});
builder.show();
//src: http://android-pro.blogspot.com/2010/09/using-alerts-in-android.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment