Reputation: 224
im trying to Lock screen orientation
in android application.I used following code to lock screen orientation when specific button click fire.
// Inside button click
Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
if (display.getOrientation() == 1) {
setRequestedOrientation(0);
} else if (display.getOrientation() == 0) {
setRequestedOrientation(1);
} else if (display.getOrientation() == 3) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
}
Above code working for both landscape and portrait screen orientations but its not working for reverse landscape mode.In that case always my activity change it's orientation
to default landscape
mode.Also i notice,when device in reverse landscape mode , display.getOrientation()
always return 3.
How can i find solution for this problem?
Upvotes: 2
Views: 9599
Reputation: 1138
I tried autremoi's answer but it killed my UI. I found an answer that does work, however I found that a mix of this: https://www.captechconsulting.com/blogs/programmatically-locking-android-screen-orientation
and this: getSize() giving me errors works, and should on all Android versions.
Use it with setRequestedOrientation(lockOrientation());
Happy coding!
private int lockOrientation(){
Display display = getWindowManager().getDefaultDisplay();
int rotation = display.getRotation();
Point size = new Point();
size.x = display.getWidth();
size.y = display.getHeight();
int lock = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
if (rotation == Surface.ROTATION_0
|| rotation == Surface.ROTATION_180) {
// if rotation is 0 or 180 and width is greater than height, we have
// a tablet
if (size.x > size.y) {
if (rotation == Surface.ROTATION_0) {
lock = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
} else {
lock = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
}
} else {
// we have a phone
if (rotation == Surface.ROTATION_0) {
lock = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
} else {
lock = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
}
}
} else {
// if rotation is 90 or 270 and width is greater than height, we
// have a phone
if (size.x > size.y) {
if (rotation == Surface.ROTATION_90) {
lock = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
} else {
lock = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
}
} else {
// we have a tablet
if (rotation == Surface.ROTATION_90) {
lock = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
} else {
lock = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
}
}
}
return lock;
}
Upvotes: 0
Reputation: 81
public void lockScreenOrientation() {
switch (((WindowManager) getSystemService(WINDOW_SERVICE))
.getDefaultDisplay().getRotation()) {
case Surface.ROTATION_90:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
break;
case Surface.ROTATION_180:
setRequestedOrientation(9/* reversePortait */);
break;
case Surface.ROTATION_270:
setRequestedOrientation(8/* reverseLandscape */);
break;
default :
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
The answer of autremoi worked for me, I created a new answer because my reputation does not let me vote him up yet.
The only difference in my code is that I recover the sensor with this other one
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
Upvotes: 3
Reputation: 3
In your AndroidManifest file, just simply type in the following codes:
android:configChanges="keyboard|orientation|keyboardHidden|screenSize"
android:screenOrientation="reverseLandscape"
example:
<activity
android:name="com.gaspar.slinfilipino.Quiz"
android:label="@string/title_activity_quiz"
android:configChanges="keyboard|orientation|keyboardHidden|screenSize"
android:screenOrientation="reverseLandscape"
android:parentActivityName="com.gaspar.slinfilipino.SignLanguageMenu" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.gaspar.slinfilipino.SignLanguageMenu" />
</activity>
It works on me. Hope it helps!
Upvotes: 0
Reputation: 261
I've been dealing with the same problem using API 8. Looks like while in reserseLandscape mode, if you call
getResources().getConfiguration().orientation
Android will return 2 (SCREEN_ORIENTATION_USER), which won't lock the orientation at all. In fact the constant SCREEN_ORIENTATION_REVERSE_LANDSCAPE is not even defined for the ActivityInfo class, even though the value actually exists. For my app I ended up creating this method:
public void lockScreenOrientation() {
switch (((WindowManager) getSystemService(WINDOW_SERVICE))
.getDefaultDisplay().getRotation()) {
case Surface.ROTATION_90:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
break;
case Surface.ROTATION_180:
setRequestedOrientation(9/* reversePortait */);
break;
case Surface.ROTATION_270:
setRequestedOrientation(8/* reverseLandscape */);
break;
default :
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
I call this method whenever I need to lock the orientation, and then I can simply release it calling
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Hope it helps.
Upvotes: 15
Reputation: 2250
try this
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
if u want portrait
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Upvotes: -2
Reputation: 2250
hi if u want lock screen orientation u have to set manifest file
<activity android:configChanges="orientation|keyboardHidden"
android:screenOrientation="portrait" android:name=".activities.HomeActivity"
android:label="@string/app_name" android:theme="@style/Theme.Translucent">
Upvotes: -1