Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<size> attribute not useful when using layer-list?

Tags:

android

Given I have a background drawable to create bulletpoints for TextViews like this:

Example of what I want

Then my XML code looks like this:

<?xml version="1.0" encoding="utf-8"?>

<layer-list
        xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:right="235dp">
        <shape android:shape="oval">
            <padding android:left="10dp" />
            <size android:height="5dp" android:width="5dp"/>
            <solid android:color="@color/my_pink"/>
        </shape>
    </item>
    <item android:left="10dp">
        <shape android:shape="rectangle">
            <solid android:color="#ffffff"/>
            <padding android:left="10dp" />
        </shape>
    </item>
</layer-list>

But once I use the code shown above, my bullet points look like this:

Result of the XML shown above

It seems the <size> tag is ignored completely.

How would you solve this problem? Using a 9patch, yes I know.. perhaps that's the easiest to do.. but in fact I was hoping to find a XML solution as it's more flexible in the future.

Custom drawing is also out of the question.

like image 1000
Sebastian Roth Avatar asked Jun 28 '11 05:06

Sebastian Roth


2 Answers

<size> tag certainly works in layer-list and to show the bullet points for textviews you can use the xml attribute android:drawableLeft -> link

With this approach, there is no need of 9-patch and custom drawing.

Posting code & screenshot here for the reference.

res/drawable/bullet_point_layer.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:right="3dp">
    <shape android:shape="oval">
        <padding android:left="10dp" />
        <size android:height="10dp" android:width="10dp"/>
        <solid android:color="#ff0080"/>
    </shape>
</item>

res/layout/main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView one"
    android:textColor="@android:color/black"
    android:drawableLeft="@drawable/bullet_point_layer"
    android:background="@android:color/white" />
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView Two"
    android:textColor="@android:color/black"
    android:drawableLeft="@drawable/bullet_point_layer"
    android:background="@android:color/white" />

</LinearLayout> 

How it looks

like image 176
Vijay C Avatar answered Oct 06 '22 14:10

Vijay C


This is an old post but I thought I would add something very important which is missing on this post.

From my experience the last item of the <layer-list> will be the item which the size value is taken from.

So in this example I create a two line divider line. I specify an empty item at the end to specify the divider drawable height.

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#535353" />
        </shape>
    </item>

    <item android:top="@dimen/common_divider_line_half_size">
        <shape android:shape="rectangle">
            <solid android:color="#737373" />
        </shape>
    </item>

    <!-- Last item empty to specify divider height -->
    <item>
        <shape android:shape="rectangle">
            <size android:height="@dimen/common_divider_line_size"/>
            <solid android:color="@android:color/transparent" />
        </shape>
    </item>
</layer-list>
like image 45
Jona Avatar answered Oct 06 '22 12:10

Jona