7

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.

Santiago Aceñolaza
  • 276
  • 1
  • 2
  • 11
  • Are you using them the same way? Inside an xml layout, used by an Activity? – Simon Marquis Aug 23 '14 at 22:36
  • There's just one little thing I forgot to mention. The RelativeLayout that I mentioned exists in 2 layout files. One for portrait and another one for landscape. – Santiago Aceñolaza Aug 23 '14 at 22:45
  • Is this really needed to have this dimension in the resources? – Simon Marquis Aug 23 '14 at 22:46
  • Not really but I wanted to avoid having to create 2 different drawables. It works fine in the emulator running API 10 but fails to change on device running API 19 – Santiago Aceñolaza Aug 23 '14 at 22:50
  • How do you define the activity(from where you are calling this) in your manifest? Do you allow it to redraw on orientation change? – Shobhit Puri Aug 23 '14 at 23:02
  • 2 drawable vs 2 resources item... But its weired indeed – Simon Marquis Aug 23 '14 at 23:02
  • @ShobhitPuri in parent activity I've only added android:launchMode="singleTop" then the usual name, label and logo attributes. – Santiago Aceñolaza Aug 23 '14 at 23:17
  • @SimonMarquis I'm starting to think it's some kind of optimization in newer android versions where it is probably reusing drawables or something. – Santiago Aceñolaza Aug 23 '14 at 23:18
  • @SantiagoAceñolaza have you tried putting the values in the `values-v19-land` – Rod_Algonquin Aug 24 '14 at 02:56
  • @Rod_Algonquin just tried but same behavior. Good thinking anyways! I might be thinking this the wrong way if nobody has faced this problem before... – Santiago Aceñolaza Aug 24 '14 at 16:01
  • I am facing the same problem, but I guess I get it because I declared configuration changes in manifest file. I realized this possible cause after reading http://stackoverflow.com/questions/7818717/why-not-use-always-androidconfigchanges-keyboardhiddenorientation/7990543#7990543 – Qianqian Nov 13 '14 at 14:14

1 Answers1

1

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>
    
Simon Marquis
  • 7,248
  • 1
  • 28
  • 43