Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using attr in shape XML causes crash in Android

Tags:

android

attr

xml

I have a drawable in this XML:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:endColor="@color/transparent"
        android:gradientRadius="200dp"
        android:startColor="?attr/primaryDarkTransparent"
        android:type="radial" />
</shape>

The XML causes a crash when startColor uses ?attr/primaryDarkTransparent saying:

Caused by: java.lang.RuntimeException: org.xmlpull.v1.XmlPullParserException: <internal><gradient> tag requires 'gradientRadius' attribute with radial type Caused by: org.xmlpull.v1.XmlPullParserException: <internal><gradient> tag requires 'gradientRadius' attribute with radial type

The dramatic story is that it works very well when I use attr in solid and stroke but I don't know what the hell is going on in gradient.

Any advices will be appreciated.

like image 790
Egemen Hamutçu Avatar asked Nov 06 '22 08:11

Egemen Hamutçu


1 Answers

There are two problems in your shape.

  1. Below Android L (API 21) you can't use attributes in custom drawables, so you should replace ?attr/primaryDarkTransparent with a color reference.

  2. The gradientRadius should be a float. E.g. 200

like image 138
Giorgio Antonioli Avatar answered Nov 14 '22 23:11

Giorgio Antonioli