6

I am trying to make this simple Dialog semi-transparent:

class TestDialog extends SherlockDialogFragment
{


    public TestDialog()
    {
    super();        
    }




    @Override
    public Dialog onCreateDialog(Bundle savedInstanceStateBundle ) 
    {

    AlertDialog.Builder builder1 = new AlertDialog.Builder(getActivity());
    // Get the layout inflater
    LayoutInflater inflater1 = getActivity().getLayoutInflater();

    // Inflate and set the layout for the dialog
    // Pass null as the parent view because its going in the dialog layout
    builder1.setView(inflater1.inflate(R.layout.dlg_test, null))
    // Add action buttons
    .setPositiveButton( "Ok", new DialogInterface.OnClickListener() 
     {

        @Override       
        public void onClick(DialogInterface dialog, int id)         
        {           
            // sign in the user ...                    
        }


     })
     .setNegativeButton( "Cancel", new DialogInterface.OnClickListener() 
      {      
                 @Override
         public void onClick(DialogInterface dialog, int id)             
         {           

         }           
      });

        return builder1.create();

    }        


}

The Layout is the following:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/test"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="4dp"
        android:layout_marginBottom="4dp"
        android:hint="User name" />

 </LinearLayout>

I have tried everything, from using a transparent .png as the background drawable of the linear layout, to setting the alpha field to 0.5, to using as a drawable a color equal to 0. I am not able to make that dialog semi-transparent. I don't know even if it is possible. What i would like to create is a dialog like the panel in the following image: semi-transparent control panel. Thanks. Note1: the min sdk required is version 8, the target is the latest (actually, v17).

Marco Masci
  • 818
  • 10
  • 22

6 Answers6

5

Instead of:

return builder1.create();

Try putting this:

Dialog dialog = builder1.create();
Drawable d = new ColorDrawable(Color.BLACK);
d.setAlpha(130);
dialog.getWindow().setBackgroundDrawable(d);
return dialog;
Ivan Skoric
  • 1,200
  • 8
  • 18
  • It didn't work. The dialog is always black as hell! Look at my post update to see what i would like to be the dialog. – Marco Masci Jul 07 '13 at 20:12
1
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Dialog dialog = super.onCreateDialog(savedInstanceState);
    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    return dialog;
}

You could put

getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); 

in your dialog's onCreateView as well.

askilondz
  • 3,264
  • 2
  • 28
  • 40
1

This is what I did to achieve translucency with AlertDialog.

Created a custom style:

<style name="TranslucentDialog" parent="@android:style/Theme.DeviceDefault.Dialog.Alert">
    <item name="android:colorBackground">#32FFFFFF</item>
</style>

And then create the dialog with:

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.TranslucentDialog);
AlertDialog dialog = builder.create();

Link to my answer to a related question: https://stackoverflow.com/a/36311627/4344057

Community
  • 1
  • 1
Dileep P G
  • 571
  • 5
  • 13
0

You can try any of these two themes on your dialog fragment reference.

yourdialogFragment.setStyle(style,android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);

or

yourdialogFragment.setStyle(style,android.R.style.Theme_Holo_Light_Dialog);

Hope this will help.

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

You can try making the background transparent, use this if you super class is a Dialog:

getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

or this if your super class is a DialogFragment:

getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

My other solution was similar to "kaushal trivedi" one. Using DialogFragments:

public class MyDialogFragment extends DialogFragment {

...

  @Override
  public void onCreate(Bundle savedInstance){
    super.onCreate(savedInstance);
    setStyle(DialogFragment.STYLE_NO_FRAME, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
  }
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.my_dialog, container,false);
  }
}

This makes your dialog be fullscreen and without background, so you can put what ever you want inside it.

  • This is not a viable solution if you want a margin between the dialog and the window. –  Jun 06 '16 at 15:50
0

Try this

public class Dialogs extends DialogFragment {

    public Dialogs() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_TITLE, android.R.style. Theme_WallpaperSettings);
    }
}
AmyWuGo
  • 2,315
  • 4
  • 22
  • 26