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)?
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;
}
}
Overriding onCreate and setting the style there should work.
change your Text
Background
on theXML
i just edited your code replace it with yoursbecause 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 textviewandroid:background="#232323"
you can change these number to any color you like. or you can set a background from thedrawable
likeandroid:background="@drawable/yourpicturename"
I had to set
android:windowIsFloating
to false andandroid:windowBackground
to my custom color in the dialog style:styles.xml
MyDialogFragment
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:
I found I just needed to do this:
I wanted a transparent background for my
DialogFragment
, and, in code, this works fine:Of course, you can specify any color or
Drawable
usingsetBackgroundDrawable()
orsetBackgroundDrawableResource()
.This works at least in
onStart()
, but not inonCreate()
, and not necessarily inonCreateView()
, it seems.That said, in most cases it's probably cleaner to do this in XML, using styles, along these lines: