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

Viewed 6099

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?

6 Answers
Related