Android integer xml value does not change when switching between portrait and landscape

I use the following GradientDrawable as the background for RelativeLayout:

<shape xmlns:android="http://schemas.android.com/apk/res/android" > <gradient android:angle="@integer/my_value" android:endColor="#0FFF" android:startColor="#FFFF" /> </shape> 

Then create a file called integers.xml in the res \ values folder :

 <resources> <integer name="my_value">90</integer> </resources> 

And another file with the same name in the res \ values-land folder:

 <resources> <integer name="my_value">180</integer> </resources> 

Therefore, when I rotate the device, the gradient goes from the bottom → from top to bottom - to the left when starting Android 2.3.3 (as expected), but when I test it on Android 4.4.4, the angle of the gradient does not change.

I also tried:

 <item name="my_value" format="float" type="dimen">90</integer> 

but the same result.

Any ideas?

UPDATE:

RelativeLayout using GradientDrawable exists in two different files:

  • Res \ Location \ my_layout
  • Res \ Layout-Earth \ my_layout

Res \ Layout-Earth \ my_layout

 <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/my_drawable" > 

UPDATE 2:

The problem only occurs when the device is turned. If I start with a previously rotated device, then the gradient angle will be set correctly.

+5
source share
1 answer

A quick workaround will directly use two different forms:

  • one for portrait mode

     <shape xmlns:android="http://schemas.android.com/apk/res/android" > <gradient android:angle="90" android:endColor="#0FFF" android:startColor="#FFFF" /> </shape> 
  • one for landscape mode

     <shape xmlns:android="http://schemas.android.com/apk/res/android" > <gradient android:angle="180" android:endColor="#0FFF" android:startColor="#FFFF" /> </shape> 
+1
source

Source: https://habr.com/ru/post/1200999/


All Articles