Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to border only left, top and bottom for TextView

Tags:

android

Here is my coding. Here is design blue color for TextView's 4 border. What I want is to design only for 3 borders (top, left and bottom).

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" >
        <shape>
            <solid
                android:color="#449def" />
            <stroke
                android:width="1dp"
                android:color="#2f6699" />
            <corners
                android:radius="0dp" />
            <padding
                android:left="5dp"
                android:top="5dp"
                android:right="5dp"
                android:bottom="5dp" />
        </shape>
    </item>
    <item>
        <shape>
            <gradient
                android:startColor="#ffffff"
                android:endColor="#ffffff"
                android:angle="270" />
            <stroke
                android:width="1dp"
                android:color="#2f6699" />
            <corners
                android:radius="0dp" />
            <padding
                android:left="5dp"
                android:top="5dp"
                android:right="5dp"
                android:bottom="5dp" />
        </shape>
    </item>
</selector>
like image 662
PPShein Avatar asked Dec 11 '12 05:12

PPShein


People also ask

How do you add a border to TextView?

To add a border to Android TextView we need to create an XML containing shape as a rectangle file under the drawable's folder and set it as background to the TextView. <stroke> tag is used to set the border width and color.

How do you add a line to the bottom of a TextView?

setBackgroundResource(R. layout. border); This will make a black line on top and bottom of the TextView.

What is Layer list in Android?

Layer list. A LayerDrawable is a drawable object that manages an array of other drawables. Each drawable in the list is drawn in the order of the list—the last drawable in the list is drawn on top. Each drawable is represented by an <item> element inside a single <layer-list> element.


2 Answers

Below code placed at the bottom of height of 1dp, you have to play with this for other sides

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

<!-- This is the line -->
<item>
  <shape>
        <solid android:color="#535353" />
  </shape>
</item>

<!-- This is the main color -->
<item android:bottom="1dp">
 <shape>
       <solid android:color="#252525" />
 </shape>
</item>
</layer-list>

Refer this discussion Open-sided Android stroke?

like image 56
kumar_android Avatar answered Oct 28 '22 02:10

kumar_android


This is a quick solution (probably dumb), add android:translationX="2dp" on your TextView and you can add android:paddingRight="2dp" to make up for lost space.

like image 29
Lawrence Gimenez Avatar answered Oct 28 '22 01:10

Lawrence Gimenez