Search code examples
androidscreen-orientationcustomdialog

Fix orientation of screen on Dialog view


I am using a custom dialog in my activity. I want to set the screen orientation only if the dialog is shown elsewhere the screen orientation can change to portrait to landscape vice versa. Is there any way to fix the orientation for such particular case specifically in java code.


Solution

  • setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    //Show dialog here
    //...
    //Hide dialog here
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);