How do I make a Dialog Fragment's background invisible?

Viewed 851

I used youtuber Coding in Flow's method to create a custom dialog. I've been trying all day to make the dialog's background transparent. I've used every single method I've found online. Non worked.

Here's how it goes:

  • First here's the Dialog layout layout_dialog.xml :

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
    
        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="50dp"
            android:background="@drawable/dialog_background">
    
            <!-- The contents of the Dialog go here -->
    
        </RelativeLayout>
    
        <ImageView
            android:id="@+id/iconImageView2"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="0dp"
            android:background="?attr/actionBarItemBackground"
            android:scaleType="fitCenter"
            app:srcCompat="@android:mipmap/sym_def_app_icon" />
    
    </RelativeLayout>
    
  • Here's the Dialog class:

     public class DialogBrightness extends AppCompatDialogFragment {
    
         //declare whatever variables here
    
         @Override
         public Dialog onCreateDialog(Bundle savedInstanceState) {
    
             AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    
             LayoutInflater inflater = getActivity().getLayoutInflater();
             View view = inflater.inflate(R.layout.layout_dialog, null);
    
             builder.setView(view)
                     .setTitle("Login")
                     .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                         @Override
                         public void onClick(DialogInterface dialogInterface, int i) {
                         }
                     })
                     .setPositiveButton("ok", new DialogInterface.OnClickListener() {
                         @Override
                         public void onClick(DialogInterface dialogInterface, int i) {
                             //get whatever values
                             listener.apply(//values);
                         }
                     });
    
             //findViewById for your dialog contents here
    
             return builder.create();
         }
    
         public interface DialogBrightnessListener {
             void apply(//values);
         }
     }
    
  • And here's the Dialog being called from the Main activity:

    DialogBrightness dialogBrightness = new DialogBrightness();
    dialogBrightness.show(getSupportFragmentManager(), "Brightness Dialog");
    

This is how the Dialog appears:

enter image description here

I'm trying to make the top white part invisible. Nothing works!

3 Answers

Try this:

put the code below in the onCreateDialog:

    // set the dialog background to transparent
    getDialog().getWindow().setBackgroundDrawable(newColorDrawable(Color.TRANSPARENT));

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

You can design the layout like following. There is an extra layout, but in case of dialogs, it will help

enter image description here

Try this:

set the background color of the parent layout to:

android:background="@android:color/transparent"

like this:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/transparent" >

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="50dp"
    android:background="@drawable/dialog_background">

    <!-- The contents of the Dialog go here -->

</RelativeLayout>

<ImageView
    android:id="@+id/iconImageView2"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="0dp"
    android:background="?attr/actionBarItemBackground"
    android:scaleType="fitCenter"
    app:srcCompat="@android:mipmap/sym_def_app_icon" />

</RelativeLayout>
Related