Add only top and bottom border on LinearLayout [duplicate]
Asked Answered
P

8

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 ?

Prague answered 22/4, 2014 at 10:0 Comment(2)
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.Neumark
You can follow [Is there an easy way to add a border to the top and bottom of an Android View?][1] [1]: #1598619Soneson
C
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>
Chapnick answered 22/4, 2014 at 10:8 Comment(6)
It seems that it doesn't work. There is a border all around the shape. I have updated my postPrague
@Prague This does not work for me too. I think you should un-accept the answer. This is mis-leadingPrimaveria
He also took his answer from here: #1598619Matty
This is a version that works for bottom-border: https://mcmap.net/q/165517/-style-bottom-line-in-androidCheapskate
What if I wanted border top to be different color than border bottom?Clout
@sachinjain024 It would work after remove the stroke.Helotry
H
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.

Hertfordshire answered 20/1, 2015 at 9:27 Comment(1)
Won't it have an overhead in painting?Rotative
C
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>
Chapnick answered 22/4, 2014 at 10:8 Comment(6)
It seems that it doesn't work. There is a border all around the shape. I have updated my postPrague
@Prague This does not work for me too. I think you should un-accept the answer. This is mis-leadingPrimaveria
He also took his answer from here: #1598619Matty
This is a version that works for bottom-border: https://mcmap.net/q/165517/-style-bottom-line-in-androidCheapskate
What if I wanted border top to be different color than border bottom?Clout
@sachinjain024 It would work after remove the stroke.Helotry
G
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>
Gayl answered 26/8, 2015 at 10:16 Comment(1)
<solid/> is used to set background colorGayl
C
19

I believe this is the simplest way:

<View
    android:layout_width="match_parent"
    android:layout_height="2dp"
    android:background="#000000" />
Chemiluminescence answered 19/12, 2017 at 3:39 Comment(1)
The normal color of the divider is gray and it's thinner so this would be better. <View android:layout_width="match_parent" android:layout_height="1dp" android:background="#efefef" />Temekatemerity
B
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>
Blakeley answered 9/12, 2016 at 17:10 Comment(0)
L
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!

Lycaonia answered 28/7, 2016 at 15:52 Comment(0)
L
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>
Lowbred answered 22/4, 2014 at 10:6 Comment(1)
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?Enshroud
S
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?

Soneson answered 22/4, 2014 at 10:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.