0

This is my first time am creating apps in eclipse. I have a TextView, I need to style it with borders. Something like we do in CSS border, but how will I do it in eclipse. I was searching on the Internet but can't really get to their point it was so confusing.

Here's the TextView code I want to style border too

<TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="18dp"
        android:clickable="false"
        android:linksClickable="false"
        android:text="Send Vmail to:"
        tools:ignore="HardCodedText"
        android:textAllCaps="false"
        android:textAppearance="?android:attr/textAppearanceMedium" />
user3140617
  • 115
  • 1
  • 12
  • Refer this link http://stackoverflow.com/questions/1598119/is-there-an-easy-way-to-add-a-border-to-the-top-and-bottom-of-an-android-view – Jigar Shekh Apr 05 '14 at 09:34

4 Answers4

2

Create an XML file like below in res/drawable. If there is no "drawable" folder, then create one.

Name the file a text_view_border.xml :

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

    <solid android:color="@android:color/transparent" />

    <stroke
        android:width="2dp"
        android:color="YOUR COLOR RESOURCE" />

</shape>

Then, in your layout files, to whichever textviews you want to give border style, apply that XML file as background

e.g

<TextView
      android:background="@drawable/text_view_border" />

This will do it.

Dhaval
  • 1,597
  • 11
  • 15
  • I have followed your steps but am getting this error. ` Description Resource Path Location Type error: Error: No resource found that matches the given name (at 'background' with value '@drawable/text_view_border'). fragment_main.xml /SampleProject/res/layout line 27 Android AAPT Problem ` – user3140617 Apr 05 '14 at 10:13
  • 1
    Try cleaning your project and make sure all the resources are rightly named. – Dhaval Apr 05 '14 at 10:57
  • 1
    Make "drawable" folder in "res" directory, not in layout. Sorry if my mistake cost you your valuable time. I've updated my answer. – Dhaval Apr 05 '14 at 11:00
2

It's Very simple u can give border to any Layout or view directly eg: Linear layout or TextView ..etc

set attribute android:drawable="@drawable/myborder"

<TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="18dp"
        android:clickable="false"
        android:linksClickable="false"
        android:text="Send Vmail to:"
        tools:ignore="HardCodedText"
        android:textAllCaps="false"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:background="@drawable/myborder" />

then create myborder.xml as:

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

        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:padding="10dp"
        android:shape="rectangle" >


     <!-- this one is ths color of the Border -->


        <stroke android:width="3px" android:color="#FFFFFF" />

    </shape>

put this file in res/drawable folder if not then create a drawable folder, and u are ready, to change color and width of border u can change it in xml: color="@color/mycolor" and width ="Npx" or ur choice. N=1,2,3...

Jayant Arora
  • 1,241
  • 2
  • 15
  • 24
0

Create a xml in drawable :

border_bg.xml

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

    <gradient
        android:angle="270"
        android:endColor="#00FF00"
        android:startColor="#FFFFFF" />

    <corners android:radius="3dp" />

    <stroke
        android:width="5px"
        android:color="#000000" />

</shape>

Use this xml as background in your TextView.

android:background="@drawable/border_bg"

You can also change color (using color code) and width of border.

Thank you.

Darshak
  • 2,298
  • 4
  • 23
  • 45
Yogendra
  • 4,817
  • 1
  • 28
  • 21
0

in drwawable create a folder name as border_text.xml

Refer this in textview with background as this.

<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#ffffff" />
<stroke android:width="1dip" android:color="#BDBDBD"/>
 </shape>

    <TextView
    android:id="@+id/textView1"
    android:background="@drawable/border_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"/>
Shadow
  • 6,864
  • 6
  • 44
  • 93