user2062024 user2062024 - 1 month ago 8
Android Question

Android - How to lock orientation for different device?

I am trying to lock the phone to portrait and tablet to landscape.
I usually use AndroidManifest.xml - android:screenOrientation="portrait"
But how can I set orientation to tablet / phone differently?
Thanks!

Answer

The best solution is to load one parameter from res folder from specific value. So if Your app is running on tablet, the screen density must be great or equals to 600dp.

In folder: res/values-sw600dp/ create xml with param:

<resources>
    <bool name="isTablet">true</bool>
</resources>

In folder res/values/ create xml with param

<resources>
    <bool name="isTablet">false</bool>
</resources>

After that You will be able check if app is running on tablet or phone by using this code:

boolean tabletSize = getResources().getBoolean(R.bool.isTablet);
if (tabletSize) {
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
} else {
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}

Source from:
http://stackoverflow.com/a/9308284/619673
https://developer.android.com/guide/practices/screens_support.html

Comments