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.
Asked
Active
Viewed 5,424 times
3 Answers
4
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//Show dialog here
//...
//Hide dialog here
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);

user1049280
- 5,176
- 8
- 35
- 52
4
We had to do this in a fullscreen dialog launched from a service.
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
params.copyFrom(window.getAttributes());
params.width = WindowManager.LayoutParams.MATCH_PARENT;
params.height = WindowManager.LayoutParams.MATCH_PARENT;
params.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
window.setAttributes(params);

sent1nel
- 1,580
- 1
- 15
- 31
-
Did you try the accepted answer? I haven't written Android code since 2014. – sent1nel Jun 20 '17 at 18:41
0
use android:configChanges="orientation"
then Android doesn't re-create the activity when the orientation is changed. If you don't want the dialog to be dismissed, just use the Activity.showDialog()
method. And if you still want to use android:configChanges="orientation"
then you have to change drawables manually in the Activity.onConfigurationChanged()
method.

Ry-
- 218,210
- 55
- 464
- 476

Anand M Joseph
- 787
- 7
- 7