Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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