Android: make everything around Dialog darker than default

14,278

Solution 1

All you need to do is play around with the dimAmount field in the WindowManager.LayoutParams:

WindowManager.LayoutParams lp = myDialog.getWindow().getAttributes();
lp.dimAmount = 0.7f

Solution 2

If you are creating custom dialog with theme translucent, you have to add below line as well. and you can control dim amount using above answer's code.

myDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND); 

For me it looks like below:

WindowManager.LayoutParams lp = myDialog.getWindow().getAttributes();
lp.dimAmount = 0.7f
myDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND); 

Solution 3

WindowManager.LayoutParams lp=getWindow().getAttributes();
//set transparency of background
lp.dimAmount=0.6f;  // dimAmount between 0.0f and 1.0f, 1.0f is completely dark
//lp.width = 200; 
//lp.height =  300; 
myDialog.getWindow().setAttributes(lp);
myDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND); 
Share:
14,278
iseeall
Author by

iseeall

Updated on June 04, 2022

Comments

  • iseeall
    iseeall almost 2 years

    i have a custom dialog with following style:

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

    It shows a borderless dialog, and anything behind gets (slightly) darker. My designer wants that everything behind got ever more dark than Android's default, but not completely black.

    Is there a setting for this at all?

    The only workaround I can think of is to use a full-screen activity instead of a dialog and just fill up the whole screen with semitransparent color (e.g. #99000000) and then draw my dialog over it. Is there an easier way?

    Thanks!

    • Blundell
      Blundell almost 13 years
      Your way sounds pretty easy. I think overriding dialog and creating a custom one would take more effort
  • Andrey
    Andrey over 10 years
    I would like to add that myDialog.show() must be first called.
  • Risadinha
    Risadinha over 10 years
    For me it only worked after adding: window.setAttributes(lp); (That method fires an event.)