32

I'm trying to get the background of a DialogFragment to be completely clear.

With setting the style item android:windowIsFloating to true (the default), the DialogFragment displays exactly how I want it to, but has a very dimmed background.

By setting android:windowIsFloating to false, I get the clear background I want, but the DialogFragment blows up to about 95% of the screen, leaving only a tiny gap around it where you can see through to the view it overlays.

I've tried ton's of tweaks and cannot seem to override this behavior.

Do I need to use a PopupWindow to achieve the desired effects, or are there some style items that I can override ?

Jonik
  • 80,077
  • 70
  • 264
  • 372
samus
  • 6,102
  • 6
  • 31
  • 69
  • I just came across the LayoutParameter flag WindowManager.LayoutParams.FLAG_DIM_BEHIND. Hopefully this will do it... – samus Dec 11 '12 at 15:44

4 Answers4

85

What works for me is to adjust the WinowManager.LayoutParams in onStart() of the DialogFragment:

@Override public void onStart() {
    super.onStart();

    Window window = getDialog().getWindow();
    WindowManager.LayoutParams windowParams = window.getAttributes();
    windowParams.dimAmount = 0.90f;
    windowParams.flags |= WindowManager.LayoutParams.FLAG_DIM_BEHIND;
    window.setAttributes(windowParams);
}
Christopher Perry
  • 38,891
  • 43
  • 145
  • 187
7

Create your own Customized dialog extends with FragmentDailog and override this method

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Dialog dialog = super.onCreateDialog(savedInstanceState);

    //set the dialog to non-modal and disable dim out fragment behind
    Window window = dialog.getWindow();
    window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
            WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
    window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
    return dialog;
}

NOTE: This answer works for me in case of DialogFragment and BottomSheetDialogFragment

chotemotelog
  • 233
  • 1
  • 4
  • 13
3

You need to get a handle to your DialogFrament (sometime after .show is called), and do this in a Posted Runnable:

DialogFragment dialog;

...

WindowManagerLayoutParams wlp = dialog.Dialog.Window.Attributes;
wlp.Flags &= ~WindowManagerFlags.DimBehind;
dialog.Dialog.Window.Attributes = wlp;

I got it from Aleks G's answer to Changing position of the Dialog on screen android .

Community
  • 1
  • 1
samus
  • 6,102
  • 6
  • 31
  • 69
1

Even a simpler solution is to change the style in the onCreate of the DialogFragment:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setStyle(STYLE_NO_FRAME, getTheme());
}
Andrey Petrov
  • 2,291
  • 2
  • 15
  • 12