1

I have an AlertDialog in my application, for which I want to have a background for the whole dialog, and a background and text color for the 2 buttons. I cannot seem to customise the alertDialog. How can I do it? I want the dialog to look like the image below.

enter image description here

Ian Bell
  • 533
  • 5
  • 18

2 Answers2

1

Hmm, basic step are:

  • Create your custom layout
  • Create dialog builder and set the custom view. (builder.setView(your_custom_view))

There are a ton of tutorials out there, even here in SOF. Please do a search first.

https://suragch.medium.com/creating-a-custom-alertdialog-bae919d2efa5 https://www.journaldev.com/22153/android-custom-alert-dialog

Lạng Hoàng
  • 1,790
  • 3
  • 17
  • 32
1

First, create your layout.xml for the dialog, and then for the example codes to call your dialog is like

AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.AlertDialog);
View view = LayoutInflater.from(context).inflate(R.layout.yourlayout, null);
builder.setView(view);
final androidx.appcompat.app.AlertDialog alertDialog = builder.create();
if (alertDialog.getWindow() != null) {
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
}
alertDialog.setCancelable(true);
alertDialog.show();

and if you want to declare your view inside your layout.xml

TextView txexample = view.findViewById(R.id.txexample);
itsmebil
  • 155
  • 1
  • 9