Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 462
Noelia Avatar asked Oct 25 '19 10:10

Noelia


People also ask

How do you change the gradient of an angle in Android?

The value of the centerX and centerY attributes is a relative position defined as a Float value ranging from 0 to 1.0. The angle of the gradient can be set using the android:angle attribute using the Integer value representing the angle in degrees. This value must be a multiple of 45.

What is angle in gradient?

Gradient basically represents the variation in space(in a direction) of any quantity. With color it represents the variation of color intensity in a direction represented by angle. Here are some diagrams to represent this concept: Here the figure shows the color variation in horizontal direction (angle is set 0).

How do you do a linear gradient on Android?

type="linear"Set the angle for a linear type. It must be a multiple of 45 degrees.


1 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.

like image 92
Noelia Avatar answered Nov 15 '22 04:11

Noelia