2

Trying to set orientation in manifest file, using a string qualifier

android:screenOrientation="@string/orientation"

Works on an 4.0 asus tablet, but fails on both a 2.1 wildfire and 4.1 Galaxy S. The problem is infact linking to a resource, If portrait is typed in it works.

Ive tried changing the qualifiers used in the tablet to test on the phone. This means the exact same code, resources and layouts are used for all devices. So it cant be spelling mistakes or build issues. But it still fails on the phones and not the tablet. This seems to be yet another bug in android.

Gives the error below, nothing in logcat

Installation error: INSTALL_PARSE_FAILED_UNEXPECTED_EXCEPTION
Please check logcat output for more details.
Launch canceled!

Manifest section

<activity
    android:name=".MainActivity"
        android:screenOrientation="@string/orientation"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

I know I can do it programatically. But theres another issue with the wildfire, not restarting immediately. Instead it waits for views to be setup etc. This doesnt occur in the other devices.

Googled and found a few others using the methods, but they dont seem to have discovered any problems. http://capdroid.wordpress.com/2012/07/21/different-screen-orientation-on-different-screen-size

Thanks

McP
  • 813
  • 1
  • 8
  • 16
  • But why on earth would you want to do this? What are the advantages? – Squonk Jul 28 '12 at 19:11
  • 1
    Cleaner Code. But more so because of the problem Ive discovered on the wildfire, It seems to wait a while before rotating, if called in OnCreate. Its also a case of it should work. And if it doesnt it should be reported. Because as far as I can tell, its an unidentified bug which needs fixing. Thanks for the comment. BTW if it wasnt clear different values are being used in different qualifying folders values-w800 etc – McP Jul 28 '12 at 19:15
  • Wait! Why are you offloading it into a string resource? Just hard code it and it will work... the things some people do in thinking they're being "smart"! To be honest - have never seen this before and don't care if it qualifies as *cleaner code* ...That's like trying to be smarter than the compiler and write up a terse/obfuscated routine in thinking it will run faster than what the compiler emits/generates.. – t0mm13b Jul 28 '12 at 19:27
  • @McP : Sorry, I still don't get why you think your only alternative is to *"do it programatically"*. A string resource is effectively a `final` string and the `strings.xml` file cannot be modified at runtime. Therefore if the string resource represented by `@string/orientation` is effectively `portrait` then why don't you just use `android:screenOrientation="portrait"`? I'm not just being awkward for the sake of it, I really just don't see any advantage. – Squonk Jul 28 '12 at 19:28
  • 1
    @Squonk because, values-w800/strings.xml string/orientation = landscape ||||||||||||||||||||||||| while values/strings.xml string/orientation = portrait. This explains it better http://capdroid.wordpress.com/2012/07/21/different-screen-orientation-on-different-screen-size – McP Jul 28 '12 at 19:43
  • @t0mm13b its not hardcoded, different values are chosen using different folder qualifiers ie values and values-w800dp, so values will return portrait and w800dp will return landscape. This explains it better http://capdroid.wordpress.com/2012/07/21/different-screen-orientation-on-different-screen-size – McP Jul 28 '12 at 19:45
  • http://developer.android.com/guide/topics/manifest/activity-element.html <- read that and identify the notation form, by putting it into a string resource will yield parse error as you've shown. So why are you doing this the wrong way? But -1 from me for trailing and very un-constructive/un-real question! – t0mm13b Jul 28 '12 at 19:49
  • @t0mm13b Thanks. I did see it, but didnt correctly read it, I noticed label allows string resource while my screenOrientation requires a hardcoded string. Yet it works on the transformer, so its a bug that it does work as opposed to a bug that it doesnt work. Thanks for the answer, Could you post this as an answer and ill accept it. – McP Jul 28 '12 at 19:56
  • @McP How did you solve your problem? I am trying to make my activity `android:screenOrientation="@integer/screen_orientation" `--- portrait_only (integer 1) in default values while sensor (integer 4) for sw600 devices ... but it only retreives from the default values folder the integer 1 and nothing from values-sw600 files. This is added to the xml resource file in values-sw600 `4` – ArloGrant Feb 27 '14 at 16:13

3 Answers3

5

I had the same issue, and I've solved it using an integer value instead of a string value, and setting orientation in "onCreate" method:

Example:

values/strings.xml

<resources>

    <integer name="orientation">1</integer>
...

values-sw600dp/strings.xml

<resources>
     <integer name="orientation">0</integer>
</resources>

Then, in your onCreate:

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.setRequestedOrientation(getResources().getInteger(R.integer.orientation));

Every orientation has an integer value, see this page for more details: http://developer.android.com/reference/android/R.attr.html#screenOrientation

3

Trying to set orientation in manifest file, using a string qualifier

While it is a string when you type it into your editor, AFAIK it is actually stored as an integer in the system, and therefore the conversion from a string resource is unlikely to be reliable. Frankly, I'm surprised it works on that one tablet -- the device manufacturer may have added support for this to handle one of their own apps.

While you can use string resources in the manifest, they need to be things that will be stored as strings (e.g., android:label).

You might be able to get this to work with an integer resource, looking up the values used for those constants (probably the same as their Java equivalents).

This seems to be yet another bug in android.

To me, this seems to be yet another place where a developer is going beyond the bounds of documented behavior, then wondering why the undocumented behavior does not work.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I knew the second I wrote "This seems to be yet another bug in android.", I was going to be regretting it. I have came across a few too many documented and accepted bugs. Theyve just worn me done. Thanks for the answer, and the sidenote that its actually an enum. – McP Jul 28 '12 at 20:07
  • @McP: If you try the integer resource approach, and it works, and you think of it, post a comment here. What you want to do is perfectly reasonable (even if I'm not a big fan of `android:screenOrientation` set to a fixed direction). And, it would certainly be useful if the manifest docs described the type and values of resources you can use for different attributes. Attributes that take `true` or `false` work with boolean resources, and free-form text (e.g., `android:label`) work with string resources, but after that, little is known for certain. – CommonsWare Jul 28 '12 at 20:32
  • Unfortunately The folder qualifiers are ignored, so layout-w800dp ends up loading values from layout. So theres no point in doing this. This is what I expected originally when trying to use the string. Ironically its the transformer which wont respect the folder qualifier. Which is strange as it respected it when it was a string, at launch time. Thanks for the help. Just to sum up the integer works, but different values using folder qualifier of w800dp does not. It may work for other qualifiers. – McP Jul 30 '12 at 12:57
  • I got the enum value from http://developer.android.com/reference/android/content/pm/ActivityInfo.html#SCREEN_ORIENTATION_PORTRAIT – McP Jul 30 '12 at 12:57
  • How did you reference the android integer/enum from your folder specific value? – vanleeuwenbram Nov 07 '12 at 10:48
  • 2
    To further bolster this idea, here is the source in PackageParser where Android blatantly ignores device configuration (except platform version) when parsing the manifest...and they know it: https://github.com/android/platform_frameworks_base/blob/android-4.4.2_r1/core/java/android/content/pm/PackageParser.java#L546 – devunwired Mar 11 '14 at 18:06
2

Read the official developer document to identify the notation form which are indicated by strings, by putting it into a string resource will yield the parse error as shown in the OP's question.

And no, I would not be jumping to conclusions in classifying it as a "bug", to each and to their own, some manufacturers are free to implement their own mechanism to handle it and others stick to the Google way of the source code.

t0mm13b
  • 34,087
  • 8
  • 78
  • 110