Создайте Java-класс для своего настраиваемого диалогового окна оповещения
public class Dialog {
private static final int resId = R.layout.dialog_dialog;
private AlertDialog alertDialog;
/**
* Custom Dialog
*
* @param context
* @param titleText
* @param message
* @param positiveText
* @param negativeText
* @param type
* @param dialogListener
*/
public Dialog(final Context context,
String titleText,
String message,
String positiveText,
String negativeText,
Type type,
final DialogListener dialogListener) {
TextView labelTitle, labelMessage, buttonPositive, buttonNegative;
View view = LayoutInflater.from(context).inflate(resId, null, false);
labelTitle = view.findViewById(R.id.labelTitle);
labelMessage = view.findViewById(R.id.labelMessage);
buttonPositive = view.findViewById(R.id.buttonPositive);
buttonNegative = view.findViewById(R.id.buttonNegative);
if (Utils.isNotEmpty(titleText)) labelTitle.setText(titleText); //(HMI2Utils.isNotEmpty is a null check
if (Utils.isNotEmpty(message)) labelMessage.setText(message);
if (Utils.isNotEmpty(positiveText)) buttonPositive.setText(positiveText);
if (Utils.isNotEmpty(negativeText)) buttonNegative.setText(negativeText);
switch (type) {
case DANGEROUS:
labelTitle.setTextColor(ContextCompat.getColor(context, R.color.white));
}
buttonNegative.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alertDialog.dismiss();
dialogListener.onNegativeButtonClick();
}
});
buttonPositive.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alertDialog.dismiss();
dialogListener.onPositiveButtonClick();
}
});
Utils.hideSoftKeyboard((Activity) context);
ContextThemeWrapper ctw = new ContextThemeWrapper(context, R.style.MyDialogTheme);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(ctw);
alertDialogBuilder.setCancelable(false);
alertDialogBuilder.setView(view);
alertDialog = alertDialogBuilder.create();
Window window = alertDialog.getWindow();
if (window != null) {
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(ContextCompat.getColor(context, R.color.app_theme_color));
}
alertDialog.show();
}
и для xml
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rlRoot"
android:layout_width="736.15px"
android:layout_height="532.66px"
tools:ignore="Overdraw">
<View
android:id="@+id/focus_eater_dummy"
android:layout_width="1px"
android:layout_height="1px"
android:focusable="true" />
<ImageView
android:layout_width="736.15px"
android:layout_height="532.66px"
android:scaleType="fitXY"
android:src="your drawable" />
<RelativeLayout
android:id="@+id/container"
android:layout_width="736.15px"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="67.83px"
android:layout_marginRight="67.83px"
android:layout_marginTop="57.855px"
android:orientation="vertical">
<TextView
android:id="@+id/labelTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:gravity="center_horizontal"
android:maxLines="1"
android:text="@string/delete_message_string"
android:textColor="@color/white"
android:textSize="24sp" />
<TextView
android:id="@+id/labelMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="18px"
android:lineSpacingExtra="18px"
android:gravity="center_horizontal"
android:lineSpacingMultiplier="1"
android:text="@string/delete_message_string"
android:textColor="@color/white"
android:textSize="24sp" />
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:id="@+id/containerButtons"
android:layout_width="736.15px"
android:layout_height="532.66px"
android:weightSum="2"
android:gravity="bottom"
android:layout_gravity="bottom"
android:orientation="horizontal">
<TextView
android:id="@+id/buttonPositive"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="@dimen/margin_button_vertical_dai"
android:background="@drawable/popup_button_selector"
android:gravity="center"
android:textColor="@color/white"
android:textSize="32sp"
tools:text="OK" />
<TextView
android:id="@+id/buttonNegative"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="100dp"
android:background="@drawable/popup_button_selector"
android:gravity="center"
android:text="Cancel"
android:textColor="@color/white"
android:textSize="32sp" />
</LinearLayout>
</FrameLayout>
внесите свои пользовательские изменения и вызовите диалоговое окно, например
new Dialog(context,
"Title 1",
"Message 2",
"OK",
"Cancel",
Dialog.Type.DANGEROUS,
new Dialog.DialogListener() {
@Override
public void onPositiveButtonClick() {
//implement Click here
}
@Override
public void onNegativeButtonClick() {
//implement Click here
}
}
);
dialog.dismiss();