How do I lock screen orientation for phone, but not for tablet? (Android)

核能气质少年 提交于 2019-11-30 17:24:58
Marek Sebera

You can do it from application as well.

Lock screen orientation (Android)
http://developer.android.com/reference/android/app/Activity.html#setRequestedOrientation(int)

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

And attributes can be found here:

http://developer.android.com/reference/android/R.attr.html#screenOrientation


To detect, if Android device is Tablet or Phone, you should use this solution (different SO Q&A), https://stackoverflow.com/a/9308284/492624

First off all, try to prevent doing this. I chose todo it because it was an easy fix for a complex problem. But really, try to prevent having todo this.

If you really want todo it, use the following code.

In your activity create a method:

    private boolean isTablet() {
        return (this.getResources().getConfiguration().screenLayout
            & Configuration.SCREENLAYOUT_SIZE_MASK)
            >= Configuration.SCREENLAYOUT_SIZE_LARGE;
    }

And then in your onCreate do the following:

    if (!isTablet()) {
        // stop screen rotation on phones because <explain>
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    }

Its seem like, not impossible if you handling this at runtime, by getting screen size and then make RunTimeConfigurationChanges, Then may it will be help you.

Try this Handling Runtime Changes.

And let me know if you get success on it..

Thanks.

One method is , create separate activities for phone version and tablet version. And then fix their orientations in the android manifest file. Another method is to check the device that's being currently used and fix the orientation in the activity itself using the code ,

      setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

Creating separate activities is the better way as its easy to maintain the tab and phone versions.

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