38

In Android when you pop up a dialog the screen behind it dims. Is there any way to control what that looks like? For example making it dim more or less or using some kind of a pattern?

CaseyB
  • 24,780
  • 14
  • 77
  • 112

6 Answers6

56

Yes, it is. You can control it.

After creating dialog:

WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();  
lp.dimAmount = 0.0f; // Dim level. 0.0 - no dim, 1.0 - completely opaque
dialog.getWindow().setAttributes(lp);

Upd: you can even add blur behind the dialog:

dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);

Upd2: Blurring is deprecated since API14:

This constant was deprecated in API level 14.
Blurring is no longer supported.

Sergey Glotov
  • 20,200
  • 11
  • 84
  • 98
36

Or you can do:

dialog.getWindow().setDimAmount(0.5f);
Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
Malvin
  • 692
  • 5
  • 8
19

This solution did not work for me. There is another option, you can cancel the flag that control dimming. This code worked for me:

dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
Tisho
  • 8,320
  • 6
  • 44
  • 52
user1563459
  • 191
  • 1
  • 3
  • 4
    You should set dialog contentView before setting dimAmount, otherwise there is no effect – jjyao Feb 16 '14 at 14:00
18

Answering it quite late but I am sure things get deprecated with time so my answer would definitely help someone. First of all create a dialog:

dialog = new Dialog(ActivityName.this);
dialog .setCancelable(false);
dialog .setContentView(R.layout.dialog_layout);

Then get the window of that dialog and add a flag called FLAG_DIM_BEHIND and finally set the dim amount on the screen.

Window window = dialog.getWindow();
if(window != null){
   window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND); // This flag is required to set otherwise the setDimAmount method will not show any effect
   window.setDimAmount(0.5f); //0 for no dim to 1 for full dim
}

Then show your dialog,

dialog.show();

And before you dismiss your dialog, clear the flags:

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

And then dismiss:

dialog.dismiss();
Namrata Bagerwal
  • 1,202
  • 13
  • 27
1

The following parameters worked for me on Android 5.1

WindowManager.LayoutParams params = new WindowManager.LayoutParams( WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_PHONE,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_DIM_BEHIND,
                PixelFormat.TRANSLUCENT);

params.dimAmount = 0.65f;

params can be assigned to the dialog.

dialog.getWindow().addContentView(view, params)
Pritam Banerjee
  • 17,953
  • 10
  • 93
  • 108
Vikas
  • 4,263
  • 1
  • 34
  • 39
0

I used android:backgroundDimEnabled and android:backgroundDimAmount in the style to enable and modify the dimming amount.

My dialog style looks like this:

<style name="LoadingDialog">
        <item name="android:windowFrame">@null</item>
        <item name="android:windowBackground">@color/transparent</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowTitleStyle">@null</item>
        <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
        <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
        <item name="android:backgroundDimEnabled">true</item>
        <item name="android:backgroundDimAmount">0.88</item>
        <item name="android:background">@color/transparent</item>
    </style>

and here is my dialog class:

public class ProgressView extends Dialog {

public ProgressView(Context context) {
    super(context, R.style.LoadingDialog); 
    setTitle(null);
    setCancelable(false);
    setOnCancelListener(null);
    View view = LayoutInflater.from(context).inflate(R.layout.lottie_loading, null);
    setContentView(view); 
} 

}

and I use it like this in a fragment of an activity:

    progressDialog = ProgressView(requireContext())
    progressDialog.show()
fullmoon
  • 8,030
  • 5
  • 43
  • 58