Can't make the custom DialogFragment transparent over the Fragment

AndroidAndroid LayoutAndroid FragmentsAndroid DialogAndroid Dialogfragment

Android Problem Overview


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...

Android Solutions


Solution 1 - Android

Try

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

in your DialogFragment's onCreateView

Solution 2 - Android

Try this (https://stackoverflow.com/questions/12849719/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();

Solution 3 - Android

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);
}
}

Solution 4 - Android

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

Solution 5 - Android

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;
   }

Solution 6 - Android

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

Solution 7 - Android

<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);
    }

Solution 8 - Android

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();
    }

Solution 9 - Android

Try this if you like to:

public TransparentDialog()
{
	super();
	setStyle(STYLE_NO_FRAME, R.style.AppTheme);
}

Solution 10 - Android

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
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTolga EView Question on Stackoverflow
Solution 1 - AndroidC.d.View Answer on Stackoverflow
Solution 2 - AndroidsaharView Answer on Stackoverflow
Solution 3 - AndroidAmeen MaheenView Answer on Stackoverflow
Solution 4 - AndroidSanghyun ByunView Answer on Stackoverflow
Solution 5 - AndroidNanda GopalView Answer on Stackoverflow
Solution 6 - Androiduser3193413View Answer on Stackoverflow
Solution 7 - AndroidNickUnuchekView Answer on Stackoverflow
Solution 8 - AndroidHesamView Answer on Stackoverflow
Solution 9 - AndroidKitesurferView Answer on Stackoverflow
Solution 10 - Androidthe_proleView Answer on Stackoverflow