Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 961
Marco Masci Avatar asked Jul 07 '13 18:07

Marco Masci


People also ask

Is DialogFragment deprecated?

This class was deprecated in API level 28. Use the Support Library DialogFragment for consistent behavior across all devices and access to Lifecycle.

What is the difference between dialog and DialogFragment?

Dialog: A dialog is a small window that prompts the user to make a decision or enter additional information. DialogFragment: A DialogFragment is a special fragment subclass that is designed for creating and hosting dialogs.

How do you show DialogFragment?

Showing the DialogFragment It is not necessary to manually create a FragmentTransaction to display your DialogFragment . Instead, use the show() method to display your dialog. You can pass a reference to a FragmentManager and a String to use as a FragmentTransaction tag.


3 Answers

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;
like image 57
Ivan Skoric Avatar answered Oct 13 '22 23:10

Ivan Skoric


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

like image 26
askilondz Avatar answered Oct 13 '22 21:10

askilondz


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

like image 31
Dileep P G Avatar answered Oct 13 '22 21:10

Dileep P G