Yagna
Yagna

Reputation: 433

Activity not picking up the correct orientation

For my App, I need to fix the orientation to portrait on Phones and allow portrait and landscape on Tablets. I have looked at answers here and but I want to try to do the same using xml alone. I tried adding the following line in my manifest

<activity
android:theme="@style/Theme.ActionBarLargeTitle"
android:name="com.work.activities.MyActivity"
android:screenOrientation="@integer/orientation_supported"
android:exported = "false"/>

In res/values/dimens.xml I added the following line (1 corresponds to portrait)

<integer name="orientation_supported">1</integer>

I have created another file, res/values-sw600dp/dimens.xml in which I have added (-1 corresponds to unspecified)

<integer name="orientation_supported">-1</integer>

On phone this code works fine and the Activity is always in portrait mode. On Nexus 10" Tablet(width 800dp) this fails and it is fixed in portrait mode. Is there anything wrong with this approach?

EDIT: I have checked with various values and in all cases(in all devices) the value from res/values/dimens.xml is picked up. If this value is missing there is an error when the app is installed 'Installation error: INSTALL_PARSE_FAILED_UNEXPECTED_EXCEPTION'.

Upvotes: 1

Views: 294

Answers (2)

MaciejG&#243;rski
MaciejG&#243;rski

Reputation: 22232

There are two possible problems here:

  • Make sure your tablet is reading from -sw600dp folder. I know it may sound weird for 800dp device. The easy way to check is to add a string with the same key in both folders and display in TextView.
  • Make sure your tablet understands -1 value. The phone I checked with did. Try changing to 0 and see if it forces landscape.

Upvotes: 0

Karakuri
Karakuri

Reputation: 38585

The issue is you are using -sw600dp qualifier, which doesn't actually check orientation but rather checks that the shortest side is at least 600dp. Try using -land or -port qualifiers instead.

Upvotes: 1

Related Questions