Search code examples
androidtextviewborder

Android : adding border around textview


How to add border around text as shown in image using xml layout

image 1

I have tried adding border to layout but its overlapping with text.

image 2


Solution

  • 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.