Reputation: 1483
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.
Upvotes: 0
Views: 5809
Reputation: 885
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.
Upvotes: 0
Reputation: 1780
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);
Upvotes: 4
Reputation: 5236
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//Show dialog here
//...
//Hide dialog here
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
Upvotes: 4