Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I put a border around an Android textview?

Is it possible to draw a border around a textview?

like image 312
yamspog Avatar asked Aug 16 '10 18:08

yamspog


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 border in xml?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above code we have taken one text view with background as border so we need to create a file in drawable as boarder.

How do I add a newline to a TextView in Android?

for the new line in TextView just add \n in middle of your text it works..


2 Answers

You can set a shape drawable (a rectangle) as background for the view.

<TextView android:text="Some text" android:background="@drawable/back"/> 

And rectangle drawable back.xml (put into res/drawable folder):

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >    <solid android:color="@android:color/white" />    <stroke android:width="1dip" android:color="#4fa5d5"/> </shape> 

You can use @android:color/transparent for the solid color to have a transparent background. You can also use padding to separate the text from the border. for more information see: http://developer.android.com/guide/topics/resources/drawable-resource.html

like image 198
Konstantin Burov Avatar answered Oct 06 '22 10:10

Konstantin Burov


Let me summarize a few different (non-programmatic) methods.

Using a shape drawable

Save the following as an XML file in your drawable folder (for example, my_border.xml):

<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"     android:shape="rectangle" >      <!-- View background color -->     <solid         android:color="@color/background_color" >     </solid>      <!-- View border color and width -->     <stroke         android:width="1dp"         android:color="@color/border_color" >     </stroke>      <!-- The radius makes the corners rounded -->     <corners         android:radius="2dp"   >     </corners>  </shape> 

Then just set it as the background to your TextView:

<TextView     android:id="@+id/textview1"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:background="@drawable/my_border" /> 

More help:

  • Shape Drawable (Android docs)
  • Android Developer Tips & Tricks: XML Drawables (Part I)

Using a 9-patch

A 9-patch is a stretchable background image. If you make an image with a border then it will give your TextView a border. All you need to do is make the image and then set it to the background in your TextView.

<TextView     android:id="@+id/textview1"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:background="@drawable/my_ninepatch_image" /> 

Here are some links that will show how to make a 9-patch image:

  • Draw 9-patch
  • Simple Nine-patch Generator
  • A simple guide to 9-patch for Android UI
  • Creating & Using 9-patch images in Android

What if I just want the top border?

Using a layer-list

You can use a layer list to stack two rectangles on top of each other. By making the second rectangle just a little smaller than the first rectangle, you can make a border effect. The first (lower) rectangle is the border color and the second rectangle is the background color.

<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android">      <!-- Lower rectangle (border color) -->     <item>         <shape android:shape="rectangle">             <solid android:color="@color/border_color" />         </shape>     </item>      <!-- Upper rectangle (background color) -->     <item android:top="2dp">         <shape android:shape="rectangle">             <solid android:color="@color/background_color" />         </shape>     </item> </layer-list> 

Setting android:top="2dp" offsets the top (makes it smaller) by 2dp. This allows the first (lower) rectangle to show through, giving a border effect. You can apply this to the TextView background the same way that the shape drawable was done above.

Here are some more links about layer lists:

  • Understanding Android's <layer-list>
  • How to make bottom border in drawable shape XML selector?
  • Create borders on a android view in drawable xml, on 3 sides?

Using a 9-patch

You can just make a 9-patch image with a single border. Everything else is the same as discussed above.

Using a View

This is kind of a trick but it works well if you need to add a seperator between two views or a border to a single TextView.

<LinearLayout     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical" >      <TextView         android:id="@+id/textview1"         android:layout_width="match_parent"         android:layout_height="wrap_content" />      <!-- This adds a border between the TextViews -->     <View         android:layout_width="match_parent"         android:layout_height="2dp"         android:background="@android:color/black" />      <TextView         android:id="@+id/textview2"         android:layout_width="match_parent"         android:layout_height="wrap_content" />  </LinearLayout> 

Here are some more links:

  • How to draw a line in Android
  • How to put a horizontal divisor line between edit text's in a activity
  • How to add a horizontal 1px line above image view in a relative layout?
like image 37
Suragch Avatar answered Oct 06 '22 09:10

Suragch