119

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

Tolga E
  • 12,188
  • 15
  • 49
  • 61

11 Answers11

353

Try

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

in your DialogFragment's onCreateView

IcyFlame
  • 5,059
  • 21
  • 50
  • 74
C.d.
  • 9,932
  • 6
  • 41
  • 51
  • 7
    you might also want to remove half transparent black background (dimming), check this answer : http://stackoverflow.com/a/33800422/2115904 – Andrii Bas Apr 18 '16 at 07:09
  • Tends to make more sense when written as: `getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));` – IcyFlame Jun 20 '16 at 11:11
  • 7
    Removes all margins as well.. Dialog is expanded to full width. – Udayaditya Barua Jun 23 '16 at 13:56
  • 2
    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`. – CoolMind Aug 16 '16 at 14:10
  • 1
    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`. – CoolMind Aug 16 '16 at 15:32
  • 2
    In case someone is looking for the Kotlin snippet, here it is: `dialog?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))`. – Francis Laclé Sep 07 '19 at 15:35
  • @UdayadityaBarua encounter dailog expended problem, is there workaround just to make dialog background transparent but not touch its margin? – Yeung May 31 '21 at 10:38
  • 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 early – Yeung Jun 01 '21 at 05:10
24

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();
Community
  • 1
  • 1
sahar
  • 341
  • 2
  • 4
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);
}
}
Ameen Maheen
  • 2,719
  • 1
  • 28
  • 28
13

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;
   }
Nanda Gopal
  • 2,519
  • 1
  • 17
  • 25
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

Sanghyun Byun
  • 129
  • 1
  • 3
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

user3193413
  • 575
  • 7
  • 10
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);
    }
NickUnuchek
  • 11,794
  • 12
  • 98
  • 138
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();
    }
Hesam
  • 52,260
  • 74
  • 224
  • 365
3

Try this if you like to:

public TransparentDialog()
{
    super();
    setStyle(STYLE_NO_FRAME, R.style.AppTheme);
}
Kitesurfer
  • 3,438
  • 2
  • 29
  • 47
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
}
the_prole
  • 8,275
  • 16
  • 78
  • 163
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)
}
Rizwan Ahmed
  • 105
  • 2
  • 11