Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set dialog window background to transparent, without affecting its margin

Currently, I have the following dialog, which I will perform expand/ collapse animation on its items.

enter image description here

This dialog is created via the following code

import android.support.v7.app.AlertDialog;

final AlertDialog.Builder builder = new AlertDialog.Builder(activity);
final AlertDialog dialog = builder.setView(view).create();
final ViewTreeObserver vto = view.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

    public void onGlobalLayout() {
        ViewTreeObserver obs = view.getViewTreeObserver();
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
            obs.removeOnGlobalLayoutListener(this);
        } else {
            obs.removeGlobalOnLayoutListener(this);
        }

        // http://stackoverflow.com/questions/19326142/why-listview-expand-collapse-animation-appears-much-slower-in-dialogfragment-tha
        int width = dialog.getWindow().getDecorView().getWidth();
        int height = dialog.getWindow().getDecorView().getHeight();
        dialog.getWindow().setLayout(width, height);
    }
});

However, when animation being performed, here's the side effect.

enter image description here

Note, the unwanted extra white region at the dialog after animation, is not caused by our custom view. It is the system window white background of the dialog itself.

I tend to make the system window background of the dialog, to become transparent.

final AlertDialog.Builder builder = new AlertDialog.Builder(activity);
final AlertDialog dialog = builder.setView(view).create();
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

Although the unwanted white background is no longer seen, the original margin of the dialog is gone too. (The dialog width is now full screen width)

enter image description here

How can I make it transparent, without affecting its margin?

like image 489
Cheok Yan Cheng Avatar asked Mar 21 '16 14:03

Cheok Yan Cheng


People also ask

How do I make alert dialog background transparent?

setBackgroundColor(Color. TRANSPARENT); wv. setPadding(5, 25, 5, 0); ImageView imgcartl=(ImageView)layout.


1 Answers

There's a pretty easy way to do that:

You need to "modify" the Drawable that is being used as a background of the Dialog. Those sort of Dialogs use an InsetDrawable as a background.

API >= 23

Only SDK API 23+ allows you to get the source Drawable wrapped by the InsetDrawable (getDrawable() method). With this, you can do whatever you want - e.g. change color to something completely different (like RED or something). If you use this approach remember that the wrapped Drawable is a GradientDrawable and not a ColorDrawable!

API < 23

For lower APIs your ("elegant") options are very limited.

Fortunately you don't need to change the color to some crazy value, you just need to change it to TRANSPARENT. For this you can use setAlpha(...) method on InsetDrawable.

InsetDrawable background = 
            (InsetDrawable) dialog.getWindow().getDecorView().getBackground();
background.setAlpha(0);

EDIT (as a result of Cheok Yan Cheng's comments):

or you can actually skip casting to InsetDrawable and get the same result. Just remember that doing so will cause the alpha to be changed on the InsetDrawable itself and not on the Drawable that is wrapped by the InsetDrawable.


retaining spacings

like image 60
Bartek Lipinski Avatar answered Sep 21 '22 21:09

Bartek Lipinski