Создайте новый файл в любом месте вашего проекта по имени DialogFactory
и внедрите следующий код внутри файла
public class DialogFactory {
public static Dialog createSimpleOkErrorDialog(Context context, String title, String message) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(context)
.setTitle(title)
.setMessage(message)
.setNeutralButton("OK", null);
return alertDialog.create();
}
public static Dialog createSimpleOkErrorDialog(Context context,
@StringRes int titleResource,
@StringRes int messageResource) {
return createSimpleOkErrorDialog(context,
context.getString(titleResource),
context.getString(messageResource));
}
public static Dialog createSimpleOkErrorDialog(Context context, String message) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(context)
.setTitle("ERROR")
.setMessage(message)
.setNeutralButton("OK", null);
return alertDialog.create();
}
public static Dialog createSimpleOkErrorDialog(Context context,
@StringRes int messageResource) {
return createSimpleOkErrorDialog(context, context.getString(messageResource));
}
public static ProgressDialog createProgressDialog(Context context, String message) {
ProgressDialog progressDialog = new ProgressDialog(context);
progressDialog.setTitle("Processing...");
progressDialog.setMessage(message);
progressDialog.setCancelable(false);
return progressDialog;
}
public static ProgressDialog createProgressDialog(Context context,
@StringRes int messageResource) {
return createProgressDialog(context, context.getString(messageResource));
}
/**
* Show dialog.
*
* @param ctx the ctx
* @param msg the msg
* @param btn1 the btn1
* @param btn2 the btn2
* @param listener1 the listener1
* @param listener2 the listener2
* @return the alert dialog
*/
public static android.app.AlertDialog showDialog(Context ctx, String msg, String btn1,
String btn2, DialogInterface.OnClickListener listener1,
DialogInterface.OnClickListener listener2) {
android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(ctx);
builder.setMessage(msg).setCancelable(false)
.setPositiveButton(btn1, listener1);
if (btn2 != null && listener2 != null)
builder.setNegativeButton(btn2, listener2);
int LAYOUT_FLAG;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
} else {
LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
}
android.app.AlertDialog alert = builder.create();
alert.show();
return alert;
}
/**
* Show dialog.
*
* @param themeRes the theme ID
* @param ctx the ctx
* @param msg the msg
* @param btn1 the btn1
* @param btn2 the btn2
* @param listener1 the listener1
* @param listener2 the listener2
* @return the alert dialog
*/
public static android.app.AlertDialog showDialog(int themeRes, Context ctx, String msg, String btn1,
String btn2, DialogInterface.OnClickListener listener1,
DialogInterface.OnClickListener listener2) {
android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(ctx, themeRes);
builder.setMessage(msg).setCancelable(false)
.setPositiveButton(btn1, listener1);
if (btn2 != null && listener2 != null)
builder.setNegativeButton(btn2, listener2);
int LAYOUT_FLAG;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
} else {
LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
}
android.app.AlertDialog alert = builder.create();
alert.getWindow().setType(LAYOUT_FLAG);
alert.show();
return alert;
}
/**
* Show dialog.
*
* @param ctx the ctx
* @param title the title
* @param msg the msg
* @param btn1 the btn1
* @param btn2 the btn2
* @param listener1 the listener1
* @param listener2 the listener2
* @return the alert dialog
*/
public static android.app.AlertDialog showDialog(Context ctx, String title, String msg, String btn1,
String btn2, DialogInterface.OnClickListener listener1,
DialogInterface.OnClickListener listener2) {
android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(ctx);
builder.setTitle(title);
builder.setMessage(msg).setCancelable(false)
.setPositiveButton(btn1, listener1);
if (btn2 != null && listener2 != null)
builder.setNegativeButton(btn2, listener2);
android.app.AlertDialog alert = builder.create();
alert.show();
return alert;
}
/**
* Show dialog.
*
* @param ctx the ctx
* @param msg the msg
* @param btn1 the btn1
* @param btn2 the btn2
* @param listener1 the listener1
* @param listener2 the listener2
* @return the alert dialog
*/
public static android.app.AlertDialog showDialog(Context ctx, int msg, int btn1,
int btn2, DialogInterface.OnClickListener listener1,
DialogInterface.OnClickListener listener2) {
return showDialog(ctx, ctx.getString(msg), ctx.getString(btn1),
ctx.getString(btn2), listener1, listener2);
}
/**
* Show dialog.
*
* @param ctx the ctx
* @param themeRes the theme ID
* @param msg the msg
* @param btn1 the btn1
* @param btn2 the btn2
* @param listener the listener
* @return the alert dialog
*/
public static android.app.AlertDialog showDialog(Context ctx, int themeRes, String msg, String btn1,
String btn2, DialogInterface.OnClickListener listener) {
return showDialog(themeRes, ctx, msg, btn1, btn2, listener,
(dialog, id) -> dialog.dismiss());
}
/**
* Show dialog.
*
* @param ctx the ctx
* @param msg the msg
* @param btn1 the btn1
* @param btn2 the btn2
* @param listener the listener
* @return the alert dialog
*/
public static android.app.AlertDialog showDialog(Context ctx, String msg, String btn1,
String btn2, DialogInterface.OnClickListener listener) {
return showDialog(ctx, msg, btn1, btn2, listener,
(dialog, id) -> dialog.dismiss());
}
/**
* Show dialog.
*
* @param ctx the ctx
* @param msg the msg
* @param btn1 the btn1
* @param btn2 the btn2
* @param listener the listener
* @return the alert dialog
*/
public static android.app.AlertDialog showDialog(Context ctx, int msg, int btn1,
int btn2, DialogInterface.OnClickListener listener) {
return showDialog(ctx, ctx.getString(msg), ctx.getString(btn1),
ctx.getString(btn2), listener);
}
/**
* Show dialog.
*
* @param ctx the ctx
* @param msg the msg
* @param listener the listener
* @return the alert dialog
*/
public static android.app.AlertDialog showDialog(Context ctx, String msg,
DialogInterface.OnClickListener listener) {
return showDialog(ctx, msg, ctx.getString(android.R.string.ok), null,
listener, null);
}
/**
* Show dialog.
*
* @param ctx the ctx
* @param msg the msg
* @param listener the listener
* @return the alert dialog
*/
public static android.app.AlertDialog showDialog(Context ctx, int msg,
DialogInterface.OnClickListener listener) {
return showDialog(ctx, ctx.getString(msg),
ctx.getString(android.R.string.ok), null, listener, null);
}
/**
* Show dialog.
*
* @param ctx the ctx
* @param msg the msg
* @return the alert dialog
*/
public static android.app.AlertDialog showDialog(Context ctx, String msg) {
return showDialog(ctx, msg, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
}
/**
* Show dialog.
*
* @param ctx the ctx
* @param msg the msg
* @return the alert dialog
*/
public static android.app.AlertDialog showDialog(Context ctx, int msg) {
return showDialog(ctx, ctx.getString(msg));
}
/**
* Show dialog.
*
* @param ctx the ctx
* @param title the title
* @param msg the msg
* @param listener the listener
*/
public static void showDialog(Context ctx, int title, int msg,
DialogInterface.OnClickListener listener) {
android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(ctx);
builder.setMessage(msg).setCancelable(false)
.setPositiveButton(android.R.string.ok, listener);
builder.setTitle(title);
android.app.AlertDialog alert = builder.create();
alert.show();
}
public static void showEditTextDialog(Context ctx, String title, String hint,
DialogInterface.OnClickListener listener) {
AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
builder.setTitle(title);
final EditText input = new EditText(ctx);
input.setHint(hint);
input.setSingleLine(true);
input.setLines(5);
input.setMaxLines(5);
input.setGravity(Gravity.LEFT | Gravity.TOP);
builder.setView(input);
builder.setPositiveButton(android.R.string.ok, listener);
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
}
public static void showDialog(Context ctx, String title, String msg,
DialogInterface.OnClickListener listener) {
android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(ctx);
builder.setMessage(msg).setCancelable(false)
.setPositiveButton(android.R.string.ok, listener);
builder.setTitle(title);
android.app.AlertDialog alert = builder.create();
alert.show();
}
/**
* Hide keyboard.
*
* @param ctx the ctx
*/
public static final void hideKeyboard(Activity ctx) {
if (ctx.getCurrentFocus() != null) {
InputMethodManager imm = (InputMethodManager) ctx
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(ctx.getCurrentFocus().getWindowToken(),
0);
}
}
/**
* Hide keyboard.
*
* @param ctx the ctx
* @param v the v
*/
public static final void hideKeyboard(Activity ctx, View v) {
try {
InputMethodManager imm = (InputMethodManager) ctx
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void showExitDialog(Activity activity) {
final boolean[] check = new boolean[1];
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setPositiveButton("Yes", (dialog, which) -> {
activity.finish();
if (check[0]) {
TinyDB.getInstance(activity).putBoolean("dialog_status", true);
} else {
TinyDB.getInstance(activity).putBoolean("dialog_status", false);
}
}
).setNegativeButton("No", (dialog, which) -> {
});
AlertDialog dialog = builder.create();
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.show();
Button b = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
b.setTextColor(ContextCompat.getColor(activity, R.color.colorAccent));
b = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
b.setTextColor(ContextCompat.getColor(activity, R.color.colorAccent));
}
}
Теперь при любом действии, которое вы хотите вызвать в диалоге, просто вызовите метод с сообщением и вашими нужными именами кнопок как
DialogFactory.showDialog(MainActivity.this, "Do you want to exit", "Yes", "Cancel", (dialog, which) -> finish());
Помните, что в диалоговом классе фабрики вы должны импортировать все связанные имена классов.