AlertDialog Displays Landscape-Sized View with Orientation in Landscape

Mxyk picture Mxyk · Feb 24, 2012 · Viewed 10.3k times · Source

My app's main activity is set in the manifest to always be in portrait view. This works fine when I load the app onto the tablet.

However, when I use my menu button and click "Setup", which opens an AlertDialog that inflates an xml layout, the tablet will display the left 2/3 or so of the entire dialog. The rest of it goes offscreen to the right. This case only occurs if I install my app on the tablet while holding it in landscape mode or if I turn the tablet to landscape mode while the app is not running (even if I go back to Portrait and click on the app). The dialog even calls this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); to make sure it stays in portrait (even though the problem persists with this call or not).

In other words, the way the AlertDialog is being displayed in portrait view is that it is blowing up a landscape view (and thus some of it goes offscreen), even though the actual orientation of the AlertDialog is portrait, like it should be.

(I apologize if the wording above was confusing - ask me to clarify anything if needed.)

So why would the tablet do this? I've tested it on a few android mobile phones and this doesn't happen, so I don't understand why the tablet will do this.

SIDE NOTES:

  • This isn't even occurring for just this AlertDialog either.. my EULA that displays at the start of the app (another AlertDialog) also appears this way if I start the app in landscape mode.

  • I've even allowed the usability of landscape by getting rid of all calls to specify portrait/landscape mode, and the tablet is still expending the dialog off-screen when held in portrait view on just the tablet, but not the phones.

EDIT:

This code works well on the phone, but the tablet problem still persists. If I uncomment dialog.show(); below, the tablet and phone display the dimensions I want, but on blackness instead of the dimmed main screen. Any ideas?

Calling function does this:

showDialog(EXAMPLE_CASE);

Function that gets called by the calling function:

protected Dialog onCreateDialog(final int id) {
        Dialog dialog;
        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        Display display = getWindowManager().getDefaultDisplay(); 
        int mwidth = display.getWidth();
        int mheight = display.getHeight();

        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();

        switch(id) {
        // Example of what all my cases look like (there were way too many to copy)
        case EXAMPLE_CASE:

            builder.setTitle("Example")
            .setMessage("Example message")
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialoginterface,int i) {
                    dialoginterface.dismiss();
                    showDialog(DIALOG_CHOICE);
                }
            })
           .setCancelable(false);

            dialog = builder.create();

            break;
        }


        if (dialog != null) {

            lp.copyFrom(dialog.getWindow().getAttributes());
            lp.width = mwidth;
            lp.height = mheight;
            lp.x = mwidth;
            //lp.y = mheight;
            lp.dimAmount=0.0f;
            //dialog.show();
            dialog.getWindow().setAttributes(lp);
            dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);

            dialog.setOnDismissListener(new OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialog) {
                    removeDialog(id);
                }
            });
        }

    return dialog;
}

Answer

koopaking3 picture koopaking3 · Feb 24, 2012

You could try getting the screen dimensions like this:

Display display = getWindowManager().getDefaultDisplay(); 
int mwidth = display.getWidth();
int mheight = display.getHeight();

And then alter the Dialog like this:

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

d.show();

WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(d.getWindow().getAttributes());
lp.width = mwidth;
lp.height = myheight;

//change position of window on screen
lp.x = mwidth/2; //set these values to what work for you; probably like I have here at
lp.y = mheight/2;        //half the screen width and height so it is in center

//set the dim level of the background
lp.dimAmount=0.1f; //change this value for more or less dimming

d.getWindow().setAttributes(lp);

//add a blur/dim flags
d.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND | WindowManager.LayoutParams.FLAG_DIM_BEHIND);

Also, you say you are inflating a custom layout. Have you tried tinkering with the layout_height and layout_width of the views in that layout to see if that makes a difference?