Gradient color drawable not working properly on Android 10 (rotated 90 degrees)

Viewed 1632

I applied a gradient drawable resource as a background for a view.

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="10dp" />
    <gradient
        android:startColor="#cf2aff"
        android:endColor="#5409ff"
        android:type="linear" />
</shape>

In devices with Android version < 10 it is shown as expected:

gradient background 1

But in devices with Android 10 it is rotated 90 degrees:

gradient background 2

Did anyone have the same problem and know how to fix it?

3 Answers

Add android:angle="0"

I got a fix for this. You need to set android:angle attribute even if it is 0 to make it work on Android 10.

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="10dp" />
    <gradient
        android:angle="0"
        android:startColor="#cf2aff"
        android:endColor="#5409ff"
        android:type="linear" />
</shape>

I suppose for Android 10, angle is set to 90 degrees by default.

Just an addition: I noticed that a negative angles get ignored on some devices. So always use positive numbers between 0 and 360 when setting the angle!

e.g.: instead of

android:angle="-45"

use

android:angle="315"

Have you tried setting the angle to 180 degrees? Like this:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="10dp" />
    <gradient
        android:angle="180"
        android:startColor="#cf2aff"
        android:endColor="#5409ff"
        android:type="linear" />
</shape>
Related