Setting Android Activity screen orientation with .xml values

I am trying to adjust the orientation of the action screen with values ​​from an XML file in res / values. I would like to do this because more or less I need the same activity for a tablet (landscape) and a smartphone (portrait).

Try first

manifest:

<activity android:name="..." android:screenOrientation="@string/defaultOrientation"/> 

config.xml:

 <string name="defaultOrientation">portrait</string> 

But with this parameter, the application will not appear on the device, and it will return this error:

java.lang.NumberFormatException: invalid int: "portrait"

Second

Ok so i just changed it to

manifest:

 <activity android:name="..." android:screenOrientation="@integer/defaultOrientation"/> 

config.xml:

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

I used 1 because ActivityInfo.SCREEN_ORIENTATION_PORTRAIT == 1.

But that doesn't work either. It seems that I can change some values, such as application / activity name, but not screen orientation?

I know that I can get around this with code, but for some reason, it feels that it should also be accessible using an XML values ​​file.

How can this be achieved through XML values?

+4
source share
1 answer

The same problem for me with your second adaptation, and I used a workaround for the code that you are not looking for.

I added 4 value folders under the res folder. "values", "values-v11", "values-v14" and "values-sw720dp"

Folders of all values ​​have "integers.xml".

"values" and "values-v14" have a value of 1, which is a portrait orientation,
<integer name="portrait_if_not_tablet">1</integer> .

"values-v11" and "values-sw720dp" have a value of 2, which is the user's orientation;
<integer name="portrait_if_not_tablet">2</integer> .

And in the manifest file, activity has a property like:
android:screenOrientation="@integer/portrait_if_not_tablet" .

All "values", "values-v11", "values-v14" work as expected, but "values-sw720dp"!

During debugging, I realized that the portrait_if_not_tablet value occurs as expected on the sw720dp device (with API 16) using getResources (). getInteger (R.integer.portrait_if_not_tablet), but when I checked the current orientation value with getRequestedOrientation () I got a different value.

 int requestedOrientation = getResources().getInteger(R.integer.portrait_if_not_tablet); int currentOrientation = getRequestedOrientation(); if (currentOrientation != requestedOrientation) { setRequestedOrientation(requestedOrientation); } 

So, I used the code block for the onCreate method for my actions to solve this problem.

+4
source

All Articles