How can I rotate display only in landscape mode in android?

大兔子大兔子 提交于 2019-12-03 04:32:48

If you're building your app for Android 2.3 and newer you should set the manifest attribute as

android:screenOrientation="sensorLandscape"

and your app will rotate to either (left or right) landscape position.

If you're building your app for Android 2.2 and older but want to run it on Android 2.3 and newer as a "sensorLandscape" configuration, you could try something like this

public static final int ANDROID_BUILD_GINGERBREAD = 9;
public static final int SCREEN_ORIENTATION_SENSOR_LANDSCAPE = 6;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (Build.VERSION.SDK_INT >= ANDROID_BUILD_GINGERBREAD) {
        setRequestedOrientation(SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
    }
...

This was the best way to handle the landscape orientation changes in my case. I was not able to find any better way to rotate the screen to left or right landscape orientations for Android 2.2 and older. I tried reading sensor orientations and setting the landscape position based on that, but it seems to me that as soon as you call "setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);" sensor turns off (for your app/activity) and you cannot read orientation through sensor.

BTW you don't really need to override the "onConfigurationChanged(Configuration newConfig)" for any of this to work properly.

In your manifest activity declaration, instead of

android:screenOrientation = "landscape"

use

android:screenOrientation = "sensorLandscape"

Have you tried adding this to your manifest inside the activity tag to see if it handles it automatically?

android:screenOrientation = "landscape"

AndroidManifest.xml -> Application -> selected your activity -> Screen orientation = landscape.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!