Skip to content

Instantly share code, notes, and snippets.

@a-v-ebrahimi
Last active January 25, 2019 15:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save a-v-ebrahimi/1699803 to your computer and use it in GitHub Desktop.
Save a-v-ebrahimi/1699803 to your computer and use it in GitHub Desktop.
Show alert with one, two or more buttons (android)
public void onClick(View view) {
if(view == this.alertBtn){
alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Alert 1");
alertDialog.setMessage("This is an alert");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return;
} });
}else if(view == this.alert2Btn){
alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Alert 2");
alertDialog.setMessage("This is another alert");
alertDialog.setIcon(R.drawable.search);
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return;
} });
alertDialog.setButton2("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return;
}});
}else if(view == this.alert3Btn){
alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Alert 3");
alertDialog.setMessage("This is the third alert");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return;
} });
alertDialog.setButton2("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return;
}});
alertDialog.setButton3("Middle", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return;
}});
}
alertDialog.show();
}
public static void showAlert(Context ctx, String title, String message, String btn_label)
{
AlertDialog alertDialog = new AlertDialog.Builder(ctx).create();
alertDialog.setTitle(title);
alertDialog.setMessage(message);
alertDialog.setButton(btn_label, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return;
} });
alertDialog.show();
}
public static void showAlert(Context ctx, int title, int message, int btn_label) {
showAlert(ctx,ctx.getResources().getString(title),ctx.getResources().getString(message),ctx.getResources().getString(btn_label));
}
public static void showAlertWithTwoButton(Context ctx, int title, int msg, int yes, int no, View.OnClickListener onClickListener) {
showAlertWithTwoButton(ctx,ctx.getString(title),ctx.getString(msg),ctx.getString(yes),ctx.getString(no),onClickListener);
}
private static void showAlertWithTwoButton(Context ctx, String title, String msg, String yes, String no, View.OnClickListener onClickListener) {
AlertDialog alertDialog = new AlertDialog.Builder(ctx).create();
alertDialog.setTitle(title);
alertDialog.setMessage(msg);
// alertDialog.setIcon(R.drawable.search);
alertDialog.setButton(yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (onClickListener!=null)
onClickListener.onClick(null);
return;
} });
alertDialog.setButton2(no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return;
}});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment