<TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView1" android:layout_marginTop="30dp" android:layout_alignParentStart="true" android:textSize="20sp" android:padding="8dp" android:text="@string/ciao_mondo" android:background="@drawable/myborder2" />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" > <!-- The radius makes the corners rounded --> <corners android:radius="2dp" > </corners> <gradient > </gradient> <padding > </padding> <size > </size> <!-- 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></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:
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:
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:
You can just make a 9-patch image with a single border. Everything else is the same as discussed above.
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: