Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawable shape not showing when used in combination with android:drawableBottom attribute.

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?

like image 836
Håvard Geithus Avatar asked Jul 28 '11 13:07

Håvard Geithus


People also ask

Which attribute is used to draw a drawable?

For example, when creating a state list drawable, you can reference a color resource for the android:drawable attribute ( android:drawable="@color/green" ).

What is drawable shape android?

A ShapeDrawable takes a Shape object and manages its presence on the screen. If no Shape is given, then the ShapeDrawable will default to a RectShape . This object can be defined in an XML file with the <shape> element.

How do I add an image to a drawable XML file?

Step 1: In this method first of all in your system find your required images and copy the image as we do normally. Step 2: Then open the Android Studio go to the app > res > drawable > right-click > Paste as shown in the below figure. Step 3: Then a pop-up screen will arise like below.

What is stroke in shape android?

Sometimes you want an outline around your shape and to do that you can use the stroke tag. You can specify the width and color of the outline using android:width and android:color.


2 Answers

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.

like image 125
Håvard Geithus Avatar answered Sep 18 '22 12:09

Håvard Geithus


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.

like image 44
Yaojin Avatar answered Sep 19 '22 12:09

Yaojin