Marcus
Marcus

Reputation: 6717

Android transparent DialogFragment

I have a dialog fragment which is shown when the WidgetConfig activity is running. The dialog shows a list of which the user can choose some items. I want this dialog to be transparent so that you can see the home screen in the background of the dialog. This is what I currently do inside WidgetConfig activity:

    DialogFragment dialog = new myChooserDialog();  
    getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));        
    dialog.show(getFragmentManager(), "dialog");    

EDIT: The code of myChooserDialog:

public class MyChooserDialog extends DialogFragment{

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        choices = getResources().getStringArray(R.array.city_choices);
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());       
        builder.setTitle(getResources().getString(R.string.widget_dialog_chooser_title));
        builder.setPositiveButton(getResources().getString(R.string.widget_dialog_chooser_posBtn), this);
        builder.setNegativeButton(getResources().getString(R.string.widget_dialog_chooser_negBtn), this);   
        builder.setSingleChoiceItems(choices, -1, this);
        return builder.create();
    }


    @Override
    public void onClick(DialogInterface dialog, int which) {
        switch (which) {
        case DialogInterface.BUTTON_POSITIVE:   
            //doing magic tricks
            break;

        case DialogInterface.BUTTON_NEGATIVE:
            dialog.dismiss();
            break;

        default:
            //more magic
            break;
        }   
    }
}

Currently, the background is all black. What am I doing wrong here?

Marcus

Upvotes: 3

Views: 9948

Answers (4)

Mohit Charadva
Mohit Charadva

Reputation: 2593

Add below lines to your onCreateDialog

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Dialog dialog = super.onCreateDialog(savedInstanceState);
    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color
            .TRANSPARENT));
    return dialog;
}

Upvotes: 15

Badr Yousfi
Badr Yousfi

Reputation: 215

Try this snippet:

getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(0));

Upvotes: 1

GrIsHu
GrIsHu

Reputation: 23638

write below code in your style.xml file.

<style name="Theme.Transparent" parent="android:Theme">
       <item name="android:windowIsTranslucent">true</item>
       <item name="android:windowBackground">@android:color/transparent</item>
       <item name="android:windowContentOverlay">@null</item>
       <item name="android:windowNoTitle">true</item>
       <item name="android:windowIsFloating">true</item>
       <item name="android:backgroundDimEnabled">false</item>
   </style>

Set the style to your dialog in your MyChooserDialog() class.

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    choices = getResources().getStringArray(R.array.city_choices);
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), android.R.style.Theme.Transparent);

Upvotes: 3

Himanshu Agarwal
Himanshu Agarwal

Reputation: 4683

Try this in your DialogFragment

getWindow().setBackgroundDrawable(new ColorDrawable(0));

Upvotes: 2

Related Questions