android – Add only top and bottom border on LinearLayout-ThrowExceptions

Exception or error:

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 ?

How to solve:

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.

###

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>

###

I believe this is the simplest way:

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

###

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>

###

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!

###

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?

###

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>

Leave a Reply

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