Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 276
Sjieke Avatar asked Nov 18 '11 10:11

Sjieke


People also ask

How do I lock my phone orientation?

If you don't want the screen to switch between portrait and landscape when you move the device, you can lock the screen orientation. To do this, swipe down from the right side of the top panel. Hold the device in the orientation in which you want it locked. On the drop-down menu, touch the “Auto Rotate” button.

How do I set Android apps to portrait only?

If you want to develop an application in portrait mode, add screenOrientation inside application tag. In the above result it is showing only portrait mode. now turn your device it not going to change the view according to orientation.


3 Answers

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

like image 118
Marek Sebera Avatar answered Oct 05 '22 23:10

Marek Sebera


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);
    }
like image 23
TjerkW Avatar answered Oct 06 '22 01:10

TjerkW


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.

like image 45
user370305 Avatar answered Oct 05 '22 23:10

user370305