Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: allow portrait and landscape for tablets, but force portrait on phone?

I would like tablets to be able to display in portrait and landscape (sw600dp or greater), but phones to be restricted to portrait only. I can't find any way to conditionally choose an orientation. Any suggestions?

like image 473
Kenny Wyland Avatar asked Mar 09 '12 01:03

Kenny Wyland


People also ask

How do you force portrait on Android?

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.


1 Answers

Here's a good way using resources and size qualifiers.

Put this bool resource in res/values as bools.xml or whatever (file names don't matter here):

    <?xml version="1.0" encoding="utf-8"?>     <resources>         <bool name="portrait_only">true</bool>     </resources> 

Put this one in res/values-sw600dp and res/values-xlarge:

    <?xml version="1.0" encoding="utf-8"?>     <resources>         <bool name="portrait_only">false</bool>     </resources> 

See this supplemental answer for help adding these directories and files in Android Studio.

Then, in the onCreate method of your Activities you can do this:

    if(getResources().getBoolean(R.bool.portrait_only)){         setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);     } 

Devices that are more than 600 dp in the smallest width direction, or x-large on pre-Android 3.2 devices (tablets, basically) will behave like normal, based on sensor and user-locked rotation, etc. Everything else (phones, pretty much) will be portrait only.

like image 89
Brian Christensen Avatar answered Oct 04 '22 15:10

Brian Christensen