13

XML file saved at res/drawable/gradient_box.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:startColor="#FFFF0000"
        android:endColor="#80FF00FF"
        android:angle="45"/>
    <padding android:left="7dp"
        android:top="7dp"
        android:right="7dp"
        android:bottom="7dp" />
    <corners android:radius="8dp" />
</shape>

(The above shape definition is is taken from then Android developer guide. There's no errors in it.).

Let's try to use it together with a TextView:

<TextView 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="Text with some crazy rectangle shape below it."
    android:drawableBottom="@drawable/gradient_box"/>   

The TextView displays as if the drawableBottom attribute wasn't there! However, setting the shape as the background works just fine:

<TextView 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="Text with crazy background"
    android:background="@drawable/gradient_box"/>

Setting an actual image (e.g. a *.png) to the android:drawableBottom also works fine.

Any ideas?

Håvard Geithus
  • 5,544
  • 7
  • 36
  • 51

2 Answers2

34

Solved it! The problem seems to be that a shape does not necessarily have intrinsic bounds. That is, the resulting drawable doesn't know how to draw itself!

To solve this problem, simply specify the size of the shape, like this:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:startColor="#FFFF0000"
        android:endColor="#80FF00FF"
        android:angle="45"/>
    <padding android:left="7dp"
        android:top="7dp"
        android:right="7dp"
        android:bottom="7dp" />
    <corners android:radius="8dp" />
    <size android:width="xxdp"
          android:height="xxdp"/>
</shape>

When the shape was specified as a background drawable for the TextView, its dimensions was known to be the same as the TextView dimensions. When telling the shape to go to the right or above the TextView, the shape dimensions could not be determined automatically.

Håvard Geithus
  • 5,544
  • 7
  • 36
  • 51
  • 4
    It is not possible to set match_parent width in such case. – oleg.semen May 06 '14 at 17:12
  • 1
    For me setting the gradient drawable worked when I've set it programmatically, i.e such as here https://stackoverflow.com/questions/8481322/create-a-radial-gradient-programmatically – foo Jan 18 '18 at 03:57
  • 1
    I had the same problem even though I used a `shape="ring"`and set its `thickness`value. However, as you mentioned, that does not seem to be enough to tell the shape how to draw itself. I've been at this for hours, experimenting with paddings, margins, background colors, images, and many other things, nothing worked. Your solution did the trick. Thanks! – Benjamin Basmaci May 13 '19 at 09:51
2

If you are using an ImageView to host the line shape as part of the "android:src" attribute, you will also run into the same problem unless you specify the width and height as part of the shape xml. One workaround is to host the line shape as part of the "android:background" attribute of ImageView. This way, you can make use of the size attributes of the ImageView for the line shape to "show" through.

Yaojin
  • 341
  • 2
  • 6
  • In my case that didn't work either. Maybe I did something wrong but generally speaking Id say trying for the size tag within the xml is the safest bet. – Benjamin Basmaci May 14 '19 at 07:52