Jimmy A. León
Jimmy A. León

Reputation: 561

How can I set a background transparency to a Dialog in Android?

How can I add a CustomDialog with a back transparency like this yellow one?

enter image description here

Upvotes: 0

Views: 193

Answers (3)

Tamir Abutbul
Tamir Abutbul

Reputation: 7651

You can simply create your custom dialog class and set the background of your layout (or any other view) for what you would like to achieve,android:background="#66F9B639" for your case.

Here is an example:

This will be your dialog class (or something similar, it's only an example):

public class FullSizeImageDialog extends Dialog {
private ImageView imageView;
private ProgressBar fullImageProgreesBar;
private Context dialogContext;

public FullSizeImageDialog(@NonNull Context context) {
    super(context);
    setContentView(R.layout.full_size_image_dialog);
    dialogContext = context;
    imageView = findViewById(R.id.full_size_image);
    fullImageProgreesBar = findViewById(R.id.fullImageProgreesBar);
    }
}

And this is your layout for the dialog (R.id.full_size_image in my case):

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
  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="#66F9B639">


 <!--Place your views here-->


 </android.support.constraint.ConstraintLayout>

And when you want to show your dialog it's super easy:

FullSizeImageDialog dialog = new FullSizeImageDialog ();
dialog.show();

Upvotes: 0

ziyaddin.ovchiyev
ziyaddin.ovchiyev

Reputation: 85

You can make use of this class. Change the color and transparency here Color.parseColor("#66F9B639")

public class LoadingDialog {

    private static LoadingDialog instance;
    private Dialog dialog;

    public static LoadingDialog getInstance() {
        if (instance == null) {
            synchronized (LoadingDialog.class) {
                if (instance == null) {
                    instance = new LoadingDialog();
                }
            }
        }
        return instance;
    }

    public void show(Context context) {
        if (dialog != null && dialog.isShowing())
            return;
        dialog = new Dialog(context);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setCancelable(false);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#6620314c")));
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.custom_loading);
        ViewGroup.LayoutParams params = dialog.getWindow().getAttributes();
        params.width = ViewGroup.LayoutParams.MATCH_PARENT;
        params.height = ViewGroup.LayoutParams.MATCH_PARENT;
        dialog.getWindow().setLayout(params.width, params.height);
        dialog.show();
    }

    public void dismiss() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }
}

Upvotes: 2

Bilal Naeem
Bilal Naeem

Reputation: 1087

First of all, you need to add some transparent background to your custom dialog layout. The second thing that you will need to do is to set the background of the dialog to be transparent. By default every dialog has a background. Even if you set your layout to have a transparent background it will still show as opaque (due to the default background). You can remove this by using

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

EDIT

Here is what you need to do

  1. Create a custom dialog. This can either be a simple layout inflated on a dialog or a separate dialog fragment.
  2. In your custom dialog layout, you need to set the background to some transparent image (or color).
  3. When showing your dialog, you need to set the default background to be transparent (code given above)

Upvotes: 1

Related Questions