Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android integer xml resource value doesn't change when switching between portrait and landscape

I using the following GradientDrawable as background for a 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 created 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 under res\values-land folder:

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

So when I rotate the device the gradient goes from bottom->top to right->left when running Android 2.3.3 (as expected) but when I test this on Android 4.4.4 the gradient angle does not change.

I've also tried with:

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

but same result.

Any ideas?

UPDATE:

The RelativeLayout that uses the GradientDrawable exists in 2 different files:

  1. res\layout\my_layout
  2. res\layout-land\my_layout

res\layout-land\my_layout

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

UPDATE 2:

The problem happens only when rotating the device. If I start the activity with the device previously rotated then the gradient angle is properly set.

like image 940
Santiago Aceñolaza Avatar asked Aug 23 '14 22:08

Santiago Aceñolaza


1 Answers

A quick workaround would be tu use directly two different shapes:

  1. one for the portrait mode

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

    <shape xmlns:android="http://schemas.android.com/apk/res/android" >
        <gradient
            android:angle="180"
            android:endColor="#0FFF"
            android:startColor="#FFFF" />
    </shape>
    
like image 104
Simon Marquis Avatar answered Sep 19 '22 15:09

Simon Marquis