Can't make the custom DialogFragment transparent over the Fragment
L

11

122

I need to create a dialog over a fragment (that takes up the whole screen). The dialog needs to be a floating dialog that will be positioned over the fragment with the fragment darkened out outside of the fragment..

For the custom Dialog, i have a linearLayout that has curved edges, no matter what i do, the dialog has a black bordering on all sides (very small). I've tried everything to make it transparent and go away (so that all of the dialog is just the linear layout - curved box)

For the DialogFragment, this is what I have for onCreateView

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    LinearLayout layout =(LinearLayout)inflater.inflate(R.layout.custom_dialog, null);
    LinearLayout item = (LinearLayout)layout.findViewById(R.id.display_item);
    populateItemData(item, inflater);
    return layout;
}

custom_dialog is just a LinearLayout that has android:backgroung set to #000000

This is my style for the custom Dialog

<style name="CustomDialog" parent="android:style/Theme.Dialog">
    <item name="android:windowBackground">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowIsTranslucent">true</item> 
    <item name="android:alwaysDrawnWithCache">false</item>
    <item name="android:windowContentOverlay">@null</item>
</style>

I tried all kinds of combinations in this style (from what I've seen online) and I can't get rid of that annoying black bordering, I can paint it white or any other color if i set that LinearLayout background to anything other than #000000...

I've spent literally 3-4 hours on this, i hope someone else can help out...

Lovins answered 8/11, 2011 at 3:20 Comment(0)
R
355

Try

getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

in your DialogFragment's onCreateView

Rheo answered 16/2, 2012 at 16:43 Comment(8)
you might also want to remove half transparent black background (dimming), check this answer : https://mcmap.net/q/182453/-can-39-t-get-transparent-dialogfragmentOnus
Tends to make more sense when written as: getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));Incardinate
Removes all margins as well.. Dialog is expanded to full width.Suborn
And it will cause an exception: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window android.app.Dialog.getWindow()' on a null object reference.Nace
Also you can call getDialog().getWindow().setBackgroundDrawableResource(android.R.color.transparent); instead. In order this not to invoke an exception you should call a DialogFragment from Activity or Fragment through dialogFragment.show(...); method, not FragmentTransaction's add.Nace
In case someone is looking for the Kotlin snippet, here it is: dialog?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT)).Fra
@UdayadityaBarua encounter dailog expended problem, is there workaround just to make dialog background transparent but not touch its margin?Threadgill
Finally, getDialog().getWindow().getDecorView().setBackgroundColor(Color.TRANSPARENT) play the trick I needed. But notice that not to call on onCreateDialog(), maybe on onResume(), getDecorView() maybe null when too earlyThreadgill
N
25

Try this (How to I create a 100% custom DialogFragment) this work for dialog

    Dialog dialog = new Dialog(getActivity());

    dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);

    dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);      

        // layout to display
    dialog.setContentView(R.layout.add_edit);

    // set color transpartent
    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

    dialog.show();
Novelia answered 12/2, 2013 at 10:37 Comment(0)
S
17

Set your theme like this worked for me

<style name="MyDialog" parent="Base.Theme.AppCompat.Light.Dialog">
    <item name="android:windowBackground">@android:color/transparent</item>
</style>

And in your dialog fragment set like this

public class Progress extends DialogFragment {


int style = DialogFragment.STYLE_NO_TITLE;
int theme = R.style.MyDialog;

public Progress() {
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setStyle(style, theme);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.progress, container, false);
}
}
Styliform answered 1/3, 2016 at 5:41 Comment(0)
T
14

You can achieve by adding this in Dialog Fragment or BottomSheetDialogFragment

In onCreateDialog Method

@Override
   public Dialog onCreateDialog(Bundle savedInstanceState) {
       Dialog dialog = super.onCreateDialog(savedInstanceState);
       dialog.getWindow().setGravity(Gravity.BOTTOM);
       dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
       dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
       return dialog;
   }
Tion answered 11/4, 2018 at 14:20 Comment(1)
setBackgroundDrawableResource and clearFlags works for me (kotlin, android api v28)Dvina
I
12

In onActivityCreated

getDialog().getWindow().getAttributes().alpha = 0.9f; // An alpha value to apply to this entire window. An alpha of 1.0 means fully opaque and 0.0 means fully transparent

for DialogFragment transparent

Inspired answered 27/11, 2014 at 7:1 Comment(0)
N
8

For completely transparent use: setStyle(DialogFragment.STYLE_NO_FRAME, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);

For custom background -create a style file in you values folder (values/style.xml) and use it: setStyle(DialogFragment.STYLE_NO_FRAME, yourpackagename.R.style.YOURE_CUSTOM_STYLE);

in your style file override the atribute: android:windowBackground to be @color/DialogBackgroundBlackSemiTransparent

Nadya answered 15/6, 2015 at 13:25 Comment(0)
G
6
<style name="BaseDialogTheme" parent="Base.Theme.AppCompat.Light.Dialog">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="colorControlNormal">@color/colorAccent</item>
    <item name="colorControlActivated">@color/colorAccent</item>

    <item name="android:windowFullscreen">true</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:colorBackgroundCacheHint">@null</item>
    <item name="android:colorBackground">@android:color/transparent</item>
    <item name="android:windowIsTranslucent">true</item>


    <item name="android:windowIsFloating">true</item>
    <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
    <item name="android:windowActionModeOverlay">false</item>
    <item name="android:windowCloseOnTouchOutside">true</item>
    <item name="android:backgroundDimAmount">.00</item>//this line is changed alpha from 1.0 to 0.0(full transparent) 

</style>



@Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(STYLE_NO_FRAME, R.style.BaseDialogTheme);
    }
Garfish answered 14/1, 2018 at 16:7 Comment(0)
M
4

Those who are using AlertDialog builder in onCreateDialog instead of onCreateView can assign theme like following code. Complete set of themes can be found from R.style. Don't forget that some of them supported recently and are not available on old device phones.

@Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), android.R.style.Theme_Translucent);
        View view = getActivity().getLayoutInflater().inflate(R.layout.dialog_album, null);
        builder.setView(view);

        return builder.create();
    }
Malar answered 16/10, 2013 at 6:37 Comment(1)
Thanks,This is what I was searching for.Libertarian
B
3

Try this if you like to:

public TransparentDialog()
{
    super();
    setStyle(STYLE_NO_FRAME, R.style.AppTheme);
}
Bort answered 28/12, 2012 at 20:16 Comment(0)
T
3

Per accepted answer, in Kotlin

override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    var v = super.onCreateView(inflater, container, savedInstanceState)
    dialog?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
    return v
}
Temperamental answered 5/8, 2020 at 2:16 Comment(0)
G
0

The accepted answer did not work for me. Then I tried this and worked

Created a style

<style name="CustomBottomSheetDialogTheme" parent="Theme.Design.Light.BottomSheetDialog">
    <item name="bottomSheetStyle">@style/CustomBottomSheetStyle</item>
</style>

<style name="CustomBottomSheetStyle" parent="Widget.Design.BottomSheet.Modal">
    <item name="android:background">@android:color/transparent</item>
</style>

Applied this style from onCreate:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setStyle(DialogFragment.STYLE_NORMAL, R.style.CustomBottomSheetDialogTheme)
}
Grotto answered 17/5, 2023 at 7:42 Comment(2)
question is about dialog, not bottomSheetWorkbag
Yes, though I named here bottomSheet, but also works with dialog. Behind the scene is, before, this theme I was using in my bottom sheet.Grotto

© 2022 - 2024 — McMap. All rights reserved.