How to add border around text as shown in image using xml layout
I have tried adding border to layout but its overlapping with text.
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.
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.
You can try this layout, its reflecting as per your requirement
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="15dp" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/border" android:layout_marginTop="10dp" android:orientation="vertical" android:padding="15dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textStyle="bold" android:text="Label 1: Value 1"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textStyle="bold" android:text="Label 2: Value 2"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textStyle="bold" android:text="Label 3: Value 3"/> </LinearLayout> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:text=" Details " android:layout_marginLeft="15dp" android:background="#ffffff" android:textSize="17sp" /> </RelativeLayout>
xml of border.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <stroke android:width="2dp" android:color="#cdcdcd" /> </shape>
Hope this helps you somehow.
To add a border to Android TextView we need to create an xml containing shape as rectangle file under drawable's folder and set it as background to the TextView.
<stroke> tag is used to set the border width and color.
border.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <stroke android:width="2dp" android:color="#000000" /> </shape>
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="10dp" xmlns:tools="http://schemas.android.com/tools" > <TextView android:id="@+id/textView2" android:layout_width="match_parent" android:layout_height="30dp" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:background="@drawable/border" android:gravity="center" android:text="Android Programming is fun!!" /> </RelativeLayout>
If you want to put a border to any layout instead of textview , make layout background as
**android:background="@drawable/border"**
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With