4

Hi I have to chage the dim color of dialog in my theme to white, so that by setting layoutParams.dimAmount = 0.5f; i can get blur white in dialog background.
I am using

<style name="MyDialog" parent="@android:style/Theme.Holo.Light.Dialog">
            <item name="android:windowBackground">@color/dialog_dim_background</item>
    </style>

and following below references
How do I create a transparent Activity on Android?
Custom screen dim with Dialog

Community
  • 1
  • 1
umesh
  • 1,148
  • 1
  • 12
  • 25
  • I have answered a similar question, you can find my answer [here](http://stackoverflow.com/a/21201869/1841194) – frogatto Jun 02 '14 at 10:37
  • mmm... setting the android:windowBackground property doesn't change the dim color of the Dialog – drublik Feb 19 '15 at 10:29

3 Answers3

5

It is a very old question, but i'd like to add n answer to it. I faced the same problem and googled a lot. Then came up with my own solution.

First of all, I removed the dim at all with this

dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);

And then I opened my layout(it was RelativeLayout) and put this view in it as the last element

<View
        android:id="@+id/shade"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/primaryShadow"
        android:visibility="invisible"/>

The background color is the color you want to use as your dim color. As you can see, it is invisible when created. So you'll need to find it in you Activity and set it's visibility before you launch the dialog to View.VISIBLE. When you cancel dialog, set it to View.INVISIBLE again. And that's it.

RexSplode
  • 1,475
  • 1
  • 16
  • 24
1

Ok here is how I do that "I don't know how will these behave with the blur flag though"

  1. I create a custom layout for the dialog with a background color of my choosing .
  2. set the dialog to fill screen
  3. remove the dim behind flag

Code snippet

Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dailog_layout);
dialog.getWindow().setBackgroundDrawable(
new ColorDrawable(0));
dialog.getWindow().setLayout(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT);
dialog.getWindow().clearFlags(
WindowManager.LayoutParams.FLAG_DIM_BEHIND);

Best of luck

Hala.M
  • 766
  • 6
  • 15
1

I know it's very old question but this will help out surely to someone who is using AppCompatActivity or ActionBarActivity. If you are setting a dialog to your activity or say DialogActivity then also the below will work!

    dialog = new Dialog(ActivityName.this);
    dialog .setCancelable(false);
    dialog .setContentView(R.layout.dialog_layout);
    Window window = dialog.getWindow();
    if(window != null){
       window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
       window.setDimAmount(0.5f);
       //If you are setting a dialog activity
            DisplayMetrics displayMetrics = new DisplayMetrics();
          getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
            Double dialogWidth = displayMetrics.widthPixels * 0.50;
            window.setLayout(dialogWidth.intValue(), 
                 WindowManager.LayoutParams.WRAP_CONTENT); //Setting it cover half of the screen width
   }
   dialog.show();

Just before you dismiss the dialog,

dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
dialog.dismiss();

//If you are setting a dialog activity, create a style in styles.xml:

<style name="MyTheme" parent="@style/Theme.AppCompat.Light.Dialog">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:backgroundDimEnabled">false</item>
</style>

And in your Manifest, set the theme:

<activity android:name=".view.POSSelectCustomerDialogActivity"
  android:theme="@style/MyTheme"
  android:windowSoftInputMode="stateAlwaysHidden|adjustResize"/>
Namrata Bagerwal
  • 1,202
  • 13
  • 27
  • this answer does not in any way explain how to change the dim color, it only explains how to enable dimming which is not what the original question asked for. – Shadow May 04 '23 at 14:21