I have been working on a Android project, that is intended to be compatible with both the Android phones and tablets. On the tablet the app is working great and looks good too. However, on the phone we have been running into issues both programmatically and layout wise (working with a 10.1 inch screen is a lot easier than a 4 inch screen). To solve some of these problems we have decided to deactivate screen orientation but only for the phone version, atleast temporarily.
The question is simple, how do I deactivate the screen orientation for Android phone, while keeping it active for the Android tablets? I know I can do this in the manifest file, however, this will also lock the orientation for the tablet I believe.
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.
来源:https://stackoverflow.com/questions/8180764/how-do-i-lock-screen-orientation-for-phone-but-not-for-tablet-android