Aashutosh Sharma
Aashutosh Sharma

Reputation: 1483

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.

Upvotes: 0

Views: 5809

Answers (3)

Anand M Joseph
Anand M Joseph

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

sent1nel
sent1nel

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

user1049280
user1049280

Reputation: 5236

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

Upvotes: 4

Related Questions