How to make an alert dialog fill 90% of screen size? How to make an alert dialog fill 90% of screen size? android android

How to make an alert dialog fill 90% of screen size?


According to Android platform developer Dianne Hackborn in this discussion group post, Dialogs set their Window's top level layout width and height to WRAP_CONTENT. To make the Dialog bigger, you can set those parameters to MATCH_PARENT.

Demo code:

    AlertDialog.Builder adb = new AlertDialog.Builder(this);    Dialog d = adb.setView(new View(this)).create();    // (That new View is just there to have something inside the dialog that can grow big enough to cover the whole screen.)    WindowManager.LayoutParams lp = new WindowManager.LayoutParams();    lp.copyFrom(d.getWindow().getAttributes());    lp.width = WindowManager.LayoutParams.MATCH_PARENT;    lp.height = WindowManager.LayoutParams.MATCH_PARENT;    d.show();    d.getWindow().setAttributes(lp);

Note that the attributes are set after the Dialog is shown. The system is finicky about when they are set. (I guess that the layout engine must set them the first time the dialog is shown, or something.)

It would be better to do this by extending Theme.Dialog, then you wouldn't have to play a guessing game about when to call setAttributes. (Although it's a bit more work to have the dialog automatically adopt an appropriate light or dark theme, or the Honeycomb Holo theme. That can be done according to http://developer.android.com/guide/topics/ui/themes.html#SelectATheme )


Try wrapping your custom dialog layout into RelativeLayout instead of LinearLayout. That worked for me.


Even simpler just do this:

int width = (int)(getResources().getDisplayMetrics().widthPixels*0.90);int height = (int)(getResources().getDisplayMetrics().heightPixels*0.90);alertDialog.getWindow().setLayout(width, height);