Skip to content

Instantly share code, notes, and snippets.

@sharifulislam52
Last active March 1, 2018 15:23
Show Gist options
  • Save sharifulislam52/f46e5e42c97429e15f4ddea7e78c3309 to your computer and use it in GitHub Desktop.
Save sharifulislam52/f46e5e42c97429e15f4ddea7e78c3309 to your computer and use it in GitHub Desktop.
/**
* Alert 1
* Normal Alert
*/
AlertDialog.Builder alertDialog = new AlertDialog.Builder(your_activity.this); // we build alert
alertDialog.setMessage("Alert_text") // alert text with setMessage() method
.setCancelable(true); // user can cancel this alert for 'true'
AlertDialog alert = alertDialog.create(); // we create alert for show
alert.setTitle("Alert_title");
alert.show(); // display alert
// alertDialog, your_activity, Alert_text, setCancelable(), alert, Alert_title
/**
* Alert 2
* Alert with positive and nagative button
*/
AlertDialog.Builder alertDialog = new AlertDialog.Builder(your_activity.this); // we build alert
alertDialog.setMessage("Alert_text") // alert text with setMessage() method
.setCancelable(true) // user can't cancel this alert for false
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// what we want to do
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// what we want to do
}
});
AlertDialog alert = alertDialog.create(); // we create alert for show
alert.setTitle("Alert!");
alert.show(); // display alert
// alertDialog, your_activity, Alert_text, setCancelable(), yes, no, alert, Alert_title
/**
* Alert 3
* Custom Alert with custom xml file
*/
AlertDialog.Builder alertBulder = new AlertDialog.Builder(your_activity.this);
View nView = getLayoutInflater().inflate(R.layout.custom_xml_file,null);
// write what want to do ----->
//cast
xml_view view1 = (xml_view) nView.findViewById(R.id.view1_id);
// <----- write what want to do
alertBulder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// what we want to do
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// what we want to do
}
});
alertBulder.setCancelable(false);
alertBulder.setView(nView);
AlertDialog dialog = alertBulder.create();
dialog.show();
// alertBulder, your_activity, nView, custom_xml_file, xml_view, view1, view1_id, yes, no, setCancelable(), alert, Alert_title
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment