Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the background color around a DialogFragment?

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;     } } 
like image 511
jul Avatar asked Feb 21 '13 16:02

jul


People also ask

Is DialogFragment deprecated?

This class was deprecated in API level 28. Use the Support Library DialogFragment for consistent behavior across all devices and access to Lifecycle.

How do you finish DialogFragment?

Show activity on this post. tl;dr: The correct way to close a DialogFragment is to use dismiss() directly on the DialogFragment. Control of the dialog (deciding when to show, hide, dismiss it) should be done through the API here, not with direct calls on the dialog.

What is the difference between dialog & DialogFragment?

Dialog: A dialog is a small window that prompts the user to make a decision or enter additional information. DialogFragment: A DialogFragment is a special fragment subclass that is designed for creating and hosting dialogs.

How do you show DialogFragment?

Showing the DialogFragment It is not necessary to manually create a FragmentTransaction to display your DialogFragment . Instead, use the show() method to display your dialog. You can pass a reference to a FragmentManager and a String to use as a FragmentTransaction tag.


2 Answers

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);     } } 
like image 106
jul Avatar answered Sep 23 '22 18:09

jul


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> 
like image 29
Jonik Avatar answered Sep 23 '22 18:09

Jonik