Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

So I'm making an application for Android and I want to force Landscape orientation for tablets and Portrait orientation for phones. However, it seems as though I can only do an orientation lock from what I've seen which defeats the purpose of wanting two separate orientations for devices.

Tablets: Landscape Phones: Portrait

To put it more technical.

I have a layout in "res/layout-xlarge-land" for landscaping on the tablet and I have the original layout in "res/layout" and I just want to explicitly use layout-xlarge-land for the tablet. Nothing else, essentially ONLY using landscape for xlarge devices.

Thanks!

like image 464
rwarner Avatar asked Oct 05 '11 22:10

rwarner


People also ask

How do I specify different layouts for portrait and landscape orientations in Android?

Android App Development for Beginners xml. Step 3 – Create a layout file by right-clicking on the resources, name the file, from the 'Available qualifiers, select Orientation. Click >> option. Select Landscape from UI mode.


2 Answers

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.

like image 71
Bhavya Avatar answered Nov 15 '22 12:11

Bhavya


You can measure the actual size (in inches) of the device and then programmatically set the orientation using:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

or

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

To get the physical size of the device you can use the code published here.

It is not 100% accurate, but it is good enough to decide what king of device is running the app.

like image 44
Mr. Messy Avatar answered Nov 15 '22 13:11

Mr. Messy