Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable landscape orientation for phones but allow for tablets

Tags:

java

android

So the general consensus is this; most people use their phones in portrait and most people use their tablet in landscape. Depending on which activity it is my app's layout goes crazy when you rotate on the phone to landscape and it just wouldn't be worth the time to fix considering users are unlikely to rotate here and have no reason to do so. I'm aware of the ole orientation="portrait" trick in the Manifest in the activity element, however this locks tablet users into portrait which wouldn't be appropriate. I would like to disable portrait on all my activities for tablet users yet simultaneously disable landscape on most all my activites for phone users. I tried to pull a fast one by making a layout-large-land folder and no layout-large folder, but that doesn't prevent the orientation from changing on tablets.

like image 796
Tristen Edwin Avatar asked May 30 '18 04:05

Tristen Edwin


People also ask

How can I disable landscape mode in Android?

If you want to disable Landscape mode for your Android app (or a single activity) all you need to do is add: android:screenOrientation="portrait" to the activity tag in AndroidManifest.

How do I force an app to landscape on my Android tablet?

When on the main screen, under the orientation section, you will see a number of options like 'Auto-rotate OFF', 'Auto-rotate ON', 'Forced Portrait' and 'Forced Landscape'. As the names suggest, you can use these buttons as one-tap shortcuts to toggle the orientation of your device.

How do I force an app to stay in portrait mode?

Android Settings Start by going to Settings => Display and locate the “Device rotation” setting. On my personal cell phone, tapping this will reveal two options: “Rotate the contents of the screen,” and “Stay in portrait view.”


3 Answers

If you just use setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) from code you will catch interesting effect on Android 26+. If the system autorotation option is enabled and you hold phone in landscape orientation and start new Activity it will appears in landscape orientation and then rotate to portrait in a few seconds. You doesn't catch such effect if set android:screenOrientation="portrait" option in the AndroidManifest. But there are not way to have different rotation option into AndroidManifest for phone and tablet.

There's way to solve that if you wish lock portrait orientation on phone and unlock autorotation on tablet.

Set option android:screenOrientation="locked" in the AndroidManifest for each Activity in you project.

<activity android:name=".SomeActivity"
    android:screenOrientation="locked" />

where "locked" – Locks the orientation to its current rotation, whatever that is. Added in API level 18 from Android docs

Then set in parent BaseActivity such code

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState)
    int orientation = getResources().getConfiguration().orientation;
    if (isTablet()) {   
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
    } else if(orientation != Configuration.ORIENTATION_PORTRAIT) {   
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
}

There are a few ways to detect that current device is Tablet. Choose implementation of isTablet() method yourself.

like image 127
cosic Avatar answered Oct 13 '22 01:10

cosic


I guess you can use a code like this in onCreate() method:

int screenLayoutSize = getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK;
if (screenLayoutSize == Configuration.SCREENLAYOUT_SIZE_SMALL || screenLayoutSize == Configuration.SCREENLAYOUT_SIZE_NORMAL) {
    setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
}

And don't specify any orientation in xml, so by default it switches in both mode.

like image 28
Afshin Avatar answered Oct 13 '22 00:10

Afshin


My suggestion would be to first find the way to know at run-time whether the activity is being executed in a Tablet by invoking a resource as explained in this answer. Then set the orientation as explained in this answer.

like image 30
Facundo Larrosa Avatar answered Oct 13 '22 01:10

Facundo Larrosa