Portrait for Phone, Landscape for Tablet (Android-Layout)

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

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.

Portrait for phone, landscape for Tablet (Android-Layout)

Setting a particular orientation based on device density may not work because there are phones which have higher densities than tablets.

What I did was to disable the device's orientation sensor by setting the attribute in the activity tag in the manifest file like this:

android:screenOrientation="nosensor"

When you run your app, by default portrait orientation is set for phones and landscape for tablets(and hence it'll select the xml file from layout-xlarge-land). And since you've set an orientation lock, it remains in this orientation.

Android Studio: multiple layouts, specifically landscape tablet

Click Create Other.. Select Orientation from Available Qualifiers box. Click >> button. In Screen Orientation select Landscape. In directory name paste this : layout-sw600dp-land

A new folder will be created with the same name having activity_main.xml in sw600dp(tablet 7" size) in landscape mode. Configure as per your requirement.

Portrait mode for Phones and Portrait+Landscape for Tablets

This way should work:Define boolean value inside values-sw600dp boolean.xml file.

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

Define similar file inside the regular values folder and chnage the value to false:

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

Put this in your onCreate:

boolean tablet = getResources().getBoolean(R.bool.tablet);
if (!tablet)
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);

Android mobile in landscape is picking up tablet layout

The correct solution in this case was using layout-sw600dp instead of layout-w600dp.

The layout from layout-w600dp is picked everytime the screenwidth is more than 600dp (including mobile in landscape mode)

Layout from layout-sw600dp is only used when the shortest width is more than 600dp, orientation dosent matter, hence works only for tablets.

How can I set a landscape layout just for tablets?

Put the tablet layout in your res/layout-w600dp or whatever smallest width you want to support for the two pane layout.

Android - Lock phone to portrait but allow tablet to be both landscape and portrait

If your landscape layout is just for tablets, I would suggest you put it in the tablet layout folders ie: layout-sw600dp-land (for 7in tablet landscape) and layout-sw720dp-land (for 10in tablet landscape). This way, the tablet layout will only be used in tablets. If you do not declare a landscape layout for the phone, it will use the regular portrait layout. Separating layout folders for the tablet and phone layouts helps for organization.

How do I make it only landscape for tablets and only portrait for phones?

Here is my suggestion:
First try to determine the type of device using screen dimensions. You could refer to this post.
On the second step you can change the screen orientation using setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);. There is another post here.



Related Topics



Leave a reply



Submit