You can use dialogBuiler.setView(your_custom_layout)
for instance
custom_dialog_layout.xml
you can beautify as per your chioce
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="7dp"
android:orientation="horizontal">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:id="@+id/custom_diag_img_view"
android:src="@android:drawable/ic_dialog_alert"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/custom_diag_title_tv"
style="@style/TextAppearance.AppCompat.Headline"/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/custom_diag_msg_tv"
android:layout_marginTop="15dp"
style="@style/TextAppearance.AppCompat.Subhead"/>
</LinearLayout>
Utils.java
public static void showAlertDialog(Context activityContext, Integer iconResource, String title, String message){
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(activityContext, R.style.Theme_MaterialComponents_DayNight_Dialog_Alert);
LinearLayout customLL = (LinearLayout) LayoutInflater.from(activityContext).inflate(R.layout.custom_dialog_layout, null);
ImageView icon = customLL.findViewById(R.id.custom_diag_img_view);
if(iconResource != null)
icon.setImageResource(iconResource);
android.widget.TextView titleTV = customLL.findViewById(R.id.custom_diag_title_tv);
titleTV.setText(title);
TextView msgTV= customLL.findViewById(R.id.custom_diag_msg_tv);
msgTV.setText(message);
dialogBuilder
.setView(customLL)
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//some code
}
})
.show();
}
MyActivity.java
//without title
Utils.showAlertDialog(UploadActivity.this,R.drawable.ic_error, null, getString(R.string.allowed_images));
//with title
Utils.showAlertDialog(UploadActivity.this,R.drawable.ic_error, "Some title, getString(R.string.allowed_images));
You can add checkbox or other actions