In my case I all my activities extend a common base-activity class called BaseActivity
, where I have two overloads for a method called showAlertDialog()
:-
protected void showAlertDialog(Context context, String title,
String msg, DialogButton dialogButton) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(title);
builder.setMessage(msg);
Drawable alertDrawable = getResources().getDrawable(R.drawable.alert);
alertDrawable.setBounds(0, 0, 32, 32);
builder.setIcon(alertDrawable);
if (dialogButton != null) {
if (dialogButton.equals(DialogButton.YES) || dialogButton.equals(DialogButton.OK)) {
int textId = dialogButton.equals(DialogButton.YES) ? R.string.yesTxt : R.string.ok;
builder.setPositiveButton(textId, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int id) {
}
});
} else if (dialogButton.equals(DialogButton.NO))
builder.setNegativeButton(R.string.noTxt, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int id) {
onNegativeButtonClicked(dialogInterface, id);
}
});
}
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
protected void showAlertDialog(Context context, String title,
String msg, DialogButton dialogButton,
DialogButton nxtDialogButton) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(title);
builder.setMessage(msg);
Drawable alertDrawable = getResources().getDrawable(R.drawable.alert);
alertDrawable.setBounds(0, 0, 32, 32);
builder.setIcon(alertDrawable);
if (dialogButton != null && nxtDialogButton != null) {
if (dialogButton.equals(DialogButton.YES) || dialogButton.equals(DialogButton.OK)) {
int textId = dialogButton.equals(DialogButton.YES) ? R.string.yesTxt : R.string.ok;
builder.setPositiveButton(textId, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int id) {
onPositiveButtonClicked(dialogInterface, id);
}
});
}
if (nxtDialogButton.equals(DialogButton.NO))
builder.setNegativeButton(R.string.noTxt, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int id) {
onNegativeButtonClicked(dialogInterface, id);
}
});
}
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
protected void onPositiveButtonClicked(DialogInterface dialogInterface, int id) {
//Override this to handle positive button press
}
protected void onNegativeButtonClicked(DialogInterface dialogInterface, int id) {
//Override this to handle negative button press
}
where DialogButton
is defined as :-
public enum DialogButton {
OK, YES, NO
}
Usage :-
showAlertDialog(this, "Heading", "A dialog with one button", DialogButton.OK)
showAlertDialog(this, "Heading", "A dialog with two button", DialogButton.YES, DialogButton.NO)
PS : Instead of using R.string.yesTxt
, I could have done a reverse lookup for string in DialogButton
enum (thought for some this I didn't realise this at that time).