Androider
Androider

Reputation: 451

Android: How to add a line to TextView?

I'm trying to achieve this:

box

I know that using a vertical LinearLayout that constains a TextView and just below a View that works as a line is a possible a solution, but I'd not like to use 3 widgets to achieve that. What I want is to use only a TextView and draw the line using a drawable. I have done an implementation but it does not work (I do not see the line). Find below the code I wrote:

<TextView 
     android:layout_width="100dp"
     android:layout_height="150dp"
     android:gravity="center"
     android:text="Text"
     android:drawableBottom="@drawable/line"/>

line.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="line">
    <stroke android:width="10dp" android:color="#3b50ec"/>
</shape>

If is possible to achieve what I want using my approach, does anyone know what's wrong in my code?

Upvotes: 0

Views: 1408

Answers (2)

Diego Palomar
Diego Palomar

Reputation: 7061

The problem is in your line.xml file, replace its content for this implementation:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <size
        android:height="1dp"
        android:width="100dp" />

    <solid android:color="#3b50ec" />

</shape>

Upvotes: 3

tachyonflux
tachyonflux

Reputation: 20211

I think to do this requires a 9 patch. Here's one from the sdk (ab_transparent_dark_holo.9.png):

enter image description here

And then change it to a background attribute:

<TextView 
     android:layout_width="100dp"
     android:layout_height="150dp"
     android:gravity="center"
     android:text="Text"
     android:background="@drawable/line"/>

Upvotes: 0

Related Questions