1

I'm trying to change the background color of an Android AlertDialog.

Every post I've seen is about changing the color of the AlertDialog, but not the background behind.

Any help would be appreciated.

enter image description here

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Hug
  • 589
  • 2
  • 6
  • 16

3 Answers3

3
<style name="MyDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:background">@color/myColor</item>

and then apply it in the Builder constructor:

AlertDialog alertDialog = new AlertDialog.Builder(getContext(), R.style.MyDialogTheme)
    ...
    .create();

for more info see see this post

Zobair Alam
  • 527
  • 1
  • 5
  • 24
2

you can reduce the dim Amount

dialog.getWindow().setDimAmount(0.5f);

To get a background color you need to make a custom Alert Dialog.

Atendra Singh
  • 396
  • 3
  • 9
1

There is one way to do what you asked for but its not that much of a good solution

First, set custom dialog theme like this.

styles.xml

    <style name="CustomDialogTheme" parent="android:Theme.Dialog">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">false</item>
    <item name="android:windowBackground">@android:color/transparent</item>
</style>

CustomDialog.java

Apply the theme and construct your custom_dialog view as follows.

 public HTCustomDialog(Context context) {
    super(context, R.style.CustomDialogTheme);
    setContentView(R.layout.custom_dialog);
}

custom_dialog.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/main_solid_80">

    <RelativeLayout
        android:id="@+id/dialog_root"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp"
        android:background="@drawable/bg_popup"
        android:padding="16dp">

</RelativeLayout>

Now that CustomDialog view is a full-screen view, set background of your root layout to whatever color you'd like.

enter image description here

I got this answer from here take a look .

Kevin Kurien
  • 812
  • 6
  • 14