1

I have created a custom DialogFragment like it is described in the developer guide. Now what I am trying to do sounds simple enough, but I cannot get it to work. I have defined: android:background="@android:color/transparent" in my layout xml which I am loading like this (in my onCreateDialog):

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

LayoutInflater inflater = getActivity().getLayoutInflater();
    
final View view = inflater.inflate(R.layout.pausedialog, null);
setStyle(STYLE_NO_FRAME, R.style.CustomDialog);

As you can see I also tried to set a custom style in the DialogFragment which is defined like this:

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

And I also tried getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(0)); which leads to a null pointer exception.

I am using android.support.v4.app.DialogFragment. Can this be the cause? Or am I doing something else wrong?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
puelo
  • 5,464
  • 2
  • 34
  • 62

3 Answers3

3

Try to set style and theme in onCreate method of your dialogFragment class implementation.

@Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
         int style=DialogFragment.STYLE_NO_TITLE;
         int theme=android.R.style.Theme_Translucent;
         setStyle(style, theme);
    }

Or

if you are using Dialog class then you can also set Style and theme on dialog instance.

kaushal trivedi
  • 3,405
  • 3
  • 29
  • 47
2

This worked for me. I created a new style:

<style name="CustomDialog" parent="@android:style/Theme.Dialog">
    <item name="android:windowBackground">@android:color/transparent</item>
</style>

And then set this style in my DialogFragment's onCreate() method like this:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setStyle(DialogFragment.STYLE_NO_TITLE, R.style.CustomDialog);
}
Franco
  • 2,711
  • 2
  • 20
  • 27
1

This is the style I use to totally remove the Dialog`s background.-

<style name="Theme.Dialog" parent="@android:style/Theme.Translucent.NoTitleBar">
    <item name="android:windowFrame">@null</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:backgroundDimEnabled">true</item>
</style>

As for the Dialog creation, you're creating a DialogBuilder but then you manually inflate a view, I guess that's the problem. Try this instead.-

AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.customTheme));
AlertDialog dialog = builder.create();
dialog.show();

EDIT

Another approach is extending AlertDialog.-

public class CustomDialog extends AlertDialog {
    public DialogParent(Context context) {
        super(context, R.style.CustomDialog);
        setContentView(R.layout.pausedialog);

        // More initialization stuff    
    }
}

And then 'manually' instantiate it.-

AlertDialog dialog = new CustomDialog(getActivity());
dialog.show();
ssantos
  • 16,001
  • 7
  • 50
  • 70
  • Didnt't work :/. I am loading the style after i am inflating the layout. final View view = inflater.inflate(R.layout.pausedialog, null); setStyle(STYLE_NO_FRAME, R.style.CustomDialog); – puelo Oct 03 '13 at 19:21
  • My bad, didn't notice the inflate part, I guess that may be the problem. Just edited my answer. – ssantos Oct 03 '13 at 19:30
  • I am inflating the view manually because i need it to find my TextViews: TextView tResume = (TextView) view.findViewById(R.id.tResume); I want to support API level 8 and above, but there is no "new AlertDialog.Builder(this, R.style.CustomDialog);" supported – puelo Oct 03 '13 at 19:36
  • Sorry, bad copypaste :( Just fixed it. As for finding your inner views, you can actually do `dialog.findViewById(R.id.tResume)` – ssantos Oct 03 '13 at 19:40
  • This will give me a null pointer exception. I am kinda confused of why i am loading a style rather than a layout in the ContextThemeWrapper. I am also asking myself where my layout would be inflated then? – puelo Oct 03 '13 at 19:49
  • Mmh maybe the easiest way (at least what works for me), is extend Dialog class and setup style and layout in `onCreate` – ssantos Oct 03 '13 at 19:57
  • The OP use `DialogFragment` not `Dialog`. – Trung Nguyen Nov 04 '15 at 10:23