1

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'.

Community
  • 1
  • 1
Yagna
  • 423
  • 1
  • 5
  • 15
  • have you added support screens in manifest http://developer.android.com/guide/topics/manifest/supports-screens-element.html – Raghunandan May 30 '13 at 19:20
  • also check this. http://developer.android.com/guide/practices/screens_support.html. i don't understand what you are doing with res/values-sw600dp/dimens.xml . The doc mentions you need to provide different drawables and layouts. – Raghunandan May 30 '13 at 19:27
  • There is no need to have different layouts in all cases. My UI has same layout on tablet and phone but with different dimensions. – Yagna May 30 '13 at 19:32
  • did you check the link i posted in the comment. I think you misunderstood. Pls check the link for clarity. Read it thorougly – Raghunandan May 30 '13 at 19:33
  • check this res/layout/main_activity.xml # For handsets res/layout-sw600dp/main_activity.xml # For tablets. You need to have different layouts in the doc. Also check this link https://www.youtube.com/watch?v=amZM8oZBgfk – Raghunandan May 30 '13 at 19:35

2 Answers2

1

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.

Karakuri
  • 38,365
  • 12
  • 84
  • 104
0

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.
MaciejGórski
  • 22,187
  • 7
  • 70
  • 94
  • The tablet is reading strings from -sw600dp. Also the tablet understands the value '-1' correctly. – Yagna May 30 '13 at 19:21