showDialog(MainActivity.this, "title", "message", "OK", "Cancel", {...}, {...});
Kotlin
fun showDialog(context: Context, title: String, msg: String,
positiveBtnText: String, negativeBtnText: String?,
positiveBtnClickListener: DialogInterface.OnClickListener,
negativeBtnClickListener: DialogInterface.OnClickListener?): AlertDialog {
val builder = AlertDialog.Builder(context)
.setTitle(title)
.setMessage(msg)
.setCancelable(true)
.setPositiveButton(positiveBtnText, positiveBtnClickListener)
if (negativeBtnText != null)
builder.setNegativeButton(negativeBtnText, negativeBtnClickListener)
val alert = builder.create()
alert.show()
return alert
}
Java
public static AlertDialog showDialog(@NonNull Context context, @NonNull String title, @NonNull String msg,
@NonNull String positiveBtnText, @Nullable String negativeBtnText,
@NonNull DialogInterface.OnClickListener positiveBtnClickListener,
@Nullable DialogInterface.OnClickListener negativeBtnClickListener) {
AlertDialog.Builder builder = new AlertDialog.Builder(context)
.setTitle(title)
.setMessage(msg)
.setCancelable(true)
.setPositiveButton(positiveBtnText, positiveBtnClickListener);
if (negativeBtnText != null)
builder.setNegativeButton(negativeBtnText, negativeBtnClickListener);
AlertDialog alert = builder.create();
alert.show();
return alert;
}