Search code examples
androidandroid-layoutandroid-alertdialogandroid-orientation

Android AlertDialog Default Orientation


I would like to change my Alert dialog default orientation to Landscape by default. I am not willing to handle the dialog orientation when the device orientation is changed.

AlertDialog.Builder builder = new AlertDialog.Builder(MenuActivity.this);
    builder.setMessage("Are you sure you want to exit?")
            .setCancelable(false)
            .setTitle("EXIT")
            .setPositiveButton("Yes",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            MenuActivity.this.finish();
                        }
                    })
            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });

    AlertDialog alert = builder.create();
    alert.setIcon(R.drawable.ic_launcher);
    alert.show();

I am unable to find what specifically handle the default orientation of this dialog? Any help will be highly appreciated. Thanks in advance.


Solution

  • You may use this method in your activity.

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);