Add only top and bottom border on LinearLayout

Make this two file and put this code. you can set border top and bottom border,

main.xml

<TextView
      android:text="This is textline"
      android:background="@drawable/border_set"
/>

border_set.xml

This file located into full path project_root/res/drawable/border_set.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
      <shape 
        android:shape="rectangle">
            <stroke android:width="1dp" android:color="#FF000000" />
            <solid android:color="#FFDDDDDD" />

        </shape>
   </item>

   <item android:top="1dp" android:bottom="1dp"> 
      <shape 
        android:shape="rectangle">
            <stroke android:width="1dp" android:color="#000" />
            <solid android:color="#FFFFFF" />
        </shape>
   </item>

</layer-list>

I think you can create this drawable and use it as background:

<?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="#000"/>
        </shape>
    </item>
    <item android:bottom="1dp" android:top="1dp">
        <shape android:shape="rectangle" >
            <solid android:color="#FFFFFF" />
        </shape>
    </item>
</layer-list>

Think of is as drawing a rectangle with border color first and then lay on top of it a rectangle with your background color leaving out 1dp on top and at the bottom.


I believe this is the simplest way:

<View
    android:layout_width="match_parent"
    android:layout_height="2dp"
    android:background="#000000" />

Here is the solution. It works even with transparent background.

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:left="-2dp"  android:right="-2dp">
        <shape android:shape="rectangle">
            <stroke android:width="2dp" android:color="@color/borderColor" />
            <solid android:color="@color/backgroundColor" />
        </shape>
    </item>

</layer-list>