26

I would like to add only a bottom and a top border on my Linearlayout. I have tried to do this :

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

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

But it add a border around the shape..

Could you help me please ?

mmBs
  • 8,421
  • 6
  • 38
  • 46
wawanopoulos
  • 9,614
  • 31
  • 111
  • 166
  • Better use a 9 patch in this case. just draw the upper and lower borders and leave the right and left borders transparent. Of course, excluding the 9 patch markers. – Phantômaxx Apr 22 '14 at 10:01
  • You can follow [Is there an easy way to add a border to the top and bottom of an Android View?][1] [1]: http://stackoverflow.com/questions/1598119/is-there-an-easy-way-to-add-a-border-to-the-top-and-bottom-of-an-android-view –  Apr 22 '14 at 10:05

8 Answers8

30

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.

Tony Vu
  • 4,251
  • 3
  • 31
  • 38
26

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>
Jaykumar Patel
  • 26,836
  • 12
  • 74
  • 76
22

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>
dzikovskyy
  • 5,027
  • 3
  • 32
  • 43
18

I believe this is the simplest way:

<View
    android:layout_width="match_parent"
    android:layout_height="2dp"
    android:background="#000000" />
doctorram
  • 1,112
  • 14
  • 13
10

This is my version; top border and bottom border are visible, not showing the left or right borders. And the background is transparent.

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

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="1dp">

    <item
        android:left="-1dp"
        android:right="-1dp"
        android:top="-1dp"
        android:bottom="1dp">
        <shape
            android:shape="rectangle">
            <stroke
                android:width="1dp"
                android:color="@color/BlueGrey_colorPrimary" />
            <solid android:color="@android:color/transparent" />
        </shape>
    </item>

</layer-list>
ab_732
  • 3,639
  • 6
  • 45
  • 61
Sercho
  • 109
  • 1
  • 3
6

A quick way to achieve this:

  • Add a Text View to the bottom and/or top of your layout.
  • Set the TextView's width to "match_parent"
  • Set the TextView's height to about "1dp" or find the thickness you would like
  • Set the TextView's background to the color you would like the border to be

I hope this helps!

Null
  • 482
  • 5
  • 3
1

Its simple. Draw 3 shapes like this.

<?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="@color/menu_line_separator_in" />
        </shape>
    </item>
    <item android:bottom="1.5dp">
        <shape android:shape="rectangle" >
            <solid android:color="@color/menu_line_separator_out" />
        </shape>
    </item>
    <item android:top="1.5dp">
        <shape android:shape="rectangle" >
            <solid android:color="@color/menu_line_separator_out" />
        </shape>
    </item>

</layer-list>
Sachin Jain
  • 21,353
  • 33
  • 103
  • 168
extmkv
  • 1,991
  • 1
  • 18
  • 36
  • what is the differences between *menu_line_separator_in* and *menu_line_separator_out*? say that I want to draw black line on top and bottom. I think **menu_line_separator_out** just a black color. But, how about **menu_line_separator_in**? – Sam Jun 21 '17 at 07:21
1

You can follow this link Is there an easy way to add a border to the top and bottom of an Android View?

I expect that you are solve from this link. also you can solve How to add border around linear layout except at the bottom?

Community
  • 1
  • 1