Marco Masci
Marco Masci

Reputation: 818

How to make this simple dialog fragment semi-transparent

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

Upvotes: 6

Views: 9034

Answers (6)

Dileep P G
Dileep P G

Reputation: 581

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

Upvotes: 1

askilondz
askilondz

Reputation: 3294

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

Upvotes: 1

AmyWuGo
AmyWuGo

Reputation: 2425

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);
    }
}

Upvotes: 0

amafernandes
amafernandes

Reputation: 21

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.

Upvotes: 0

kaushal trivedi
kaushal trivedi

Reputation: 3443

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.

Upvotes: 0

Ivan Skoric
Ivan Skoric

Reputation: 1210

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;

Upvotes: 5

Related Questions