50

I'm building a custom DialogFragment. The dialog layout is set to my_dialog.xml, but how can I modify the color around the dialog (the transparent grey)?

enter image description here

my_dialog.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center" >

    <TextView
        android:id="@+id/hello"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@android:color/holo_orange_light"
        android:gravity="center"
        android:text="hello" />

</RelativeLayout>

MyDialogFragment.java

public class MyDialogFragment extends DialogFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.my_dialog, container);

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

        return view;
    }
}
Jonik
  • 80,077
  • 70
  • 264
  • 372
jul
  • 36,404
  • 64
  • 191
  • 318

8 Answers8

62

I had to set android:windowIsFloating to false and android:windowBackground to my custom color in the dialog style:

styles.xml

<resources xmlns:android="http://schemas.android.com/apk/res/android">

    <style name="MyDialog" parent="@android:style/Theme.Dialog">
        <item name="android:windowFrame">@null</item>
        <item name="android:windowBackground">@color/orange_transparent</item>
        <item name="android:windowIsFloating">false</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowTitleStyle">@null</item>
        <item name="android:colorBackgroundCacheHint">@null</item>
        <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
        <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
        <item name="android:gravity">center</item>
    </style>

</resources>

MyDialogFragment

public class MyDialogFragment extends DialogFragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_TITLE, R.style.MyDialog);
    }
}
jul
  • 36,404
  • 64
  • 191
  • 318
  • 1
    It works, thank you a lot. This Dialog with styles behavior is really weird and unclear though. – ar-g Jul 08 '15 at 08:01
  • 2
    Note that if you change the `windowBackground` to a color, you'll lose the default DialogFragment window styling with rounded corners. To preserve this you'll have to create a rect drawable with the color as background (i.e. the solid parameter) and use corners with a radius value. – r3flss ExlUtr Aug 05 '16 at 12:31
  • how to do this in Dialog class ? I mean I extend Dialog ,not DialogFragment and dont have setStyle() ... – sasan Jun 28 '17 at 08:21
  • 2
    screws up the basic "onClick"-stuff for the background though, if you tap on the background, the dialog is no longer dismissed – TormundThunderfist Jul 12 '17 at 08:38
  • It is no need to set `android:windowFrame` to @null https://stackoverflow.com/a/8053718/1621111 – Konstantin Konopko Feb 26 '18 at 11:08
41

To get a fully transparent dialog, you can set in onCreateView the following

  • setBackgroundDrawable to Color.TRANSPARENT
  • setDimAmount to 0

See code example here:

public class TextEditor extends DialogFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_text_editor, container);

        // make dialog itself transparent
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        // remove background dim 
        getDialog().getWindow().setDimAmount(0);

        //[add more custom code here...]

        return view;
    }
}
StefMa
  • 3,344
  • 4
  • 27
  • 48
Dirk
  • 2,011
  • 1
  • 20
  • 25
37

I wanted a transparent background for my DialogFragment, and, in code, this works fine:

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

    Window window = getDialog().getWindow();
    window.setBackgroundDrawableResource(android.R.color.transparent);
}

Of course, you can specify any color or Drawable using setBackgroundDrawable() or setBackgroundDrawableResource().

This works at least in onStart(), but not in onCreate(), and not necessarily in onCreateView(), it seems.

That said, in most cases it's probably cleaner to do this in XML, using styles, along these lines:

<style name="MyDialogStyle" parent="@android:style/Theme.Holo.Light.Dialog">
    <item name="android:windowBackground">@android:color/transparent</item>
</style>
Community
  • 1
  • 1
Jonik
  • 80,077
  • 70
  • 264
  • 372
16

I found I just needed to do this:

<style name="MyDialog" parent="@android:style/Theme.Dialog">
    <!--  other attributes  -->
    <item name="android:backgroundDimEnabled">false</item>
</style>
Blundell
  • 75,855
  • 30
  • 208
  • 233
  • Seems like this should be the accepted answer. The xml from the accepted answer changes the color of the orange part of the screenshot in addition to removing the gray part. – Peter Ajtai Feb 19 '14 at 01:41
3

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
2

Overriding onCreate and setting the style there should work.

@Override
public void onCreate(Bundle savedInstance){
    super.onCreate(savedInstance);
    setStyle(DialogFragment.STYLE_NO_FRAME, android.R.style.Theme_Translucent);
}
Jared Rummler
  • 37,824
  • 19
  • 133
  • 148
0

change your Text Background on the XML i just edited your code replace it with yours

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" >

<TextView
    android:id="@+id/hello"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:gravity="center"
    android:text="hello" />

because you gave the TextView height and width of 100dp. and set a background for it that will fill the whole dialog. since your main layout is wrap_content. please do accept the answer if that helped you.
Edit : to change the background just add to your layout or to your textview android:background="#232323" you can change these number to any color you like. or you can set a background from the drawable like android:background="@drawable/yourpicturename"

Kosh
  • 6,140
  • 3
  • 36
  • 67
  • I already set background a background to my TextView. My problem is the color *around* the TextView. – jul Feb 21 '13 at 17:28
  • the color is caused by setting the background in your textView as this android:background="@android:color/holo_orange_light" i have deleted this from your layout. could you copy and paste my above code and see how it will work! – Kosh Feb 21 '13 at 17:35
  • Yes, I know, and I want it this way. I want to modify the grey color around it. – jul Feb 21 '13 at 18:32
  • well to modify the grey color . set the background to the linearLayout. or in your dialog add Android.Theme.Holo or whatever theme you like, just add it after you defined on the dialog constructor. – Kosh Feb 22 '13 at 05:09
  • I guess you mean the RelativeLayout. Since it wraps the content of the TextView, changing its background won't change the transparent grey of the window. – jul Feb 22 '13 at 09:23
0

Jul's answer was almost good for me. But it seems it doesn't work when using an AlertDialog.Builder.

I had to set the style here:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    // Use the Builder class for convenient dialog construction
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.MyDialog );
Jared Rummler
  • 37,824
  • 19
  • 133
  • 148