How to Add One Side Left Border to TextView in Android using XML

One side border is most probably used to show text into Flag view. Using the one side border we could make the TextView more good looking. So here is the complete step by step tutorial for How to Add One Side Left Border to TextView in Android using XML.

How to Add One Side Left Border to TextView in Android :

Code for MainActivity.java file.

package com.android_examples.onesidebordertextview_android_examplescom;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

Code for activity_main.xml layout file.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fff"
    tools:context="com.android_examples.onesidebordertextview_android_examplescom.MainActivity">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="100dp"
        android:text="This is Sample TextView Text."
        android:background="@drawable/text_style"
        android:gravity="center"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_margin="12dp"
        android:textSize="20dp"
        android:textColor="#000"
         />

</RelativeLayout>

Code for text_style.xml file.

<?xml version="1.0" encoding="utf-8"?>

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item>
        <shape android:shape="rectangle">
            <solid android:color="#F44336"/>
        </shape>
    </item>

    <item android:left="4dp">
        <shape android:shape="rectangle">
            <solid android:color="#FFEB3B"/>
        </shape>
    </item>

</layer-list>

Screenshot :

Leave a Reply

Your email address will not be published. Required fields are marked *