How to add border around linear layout except at the bottom?
Asked Answered
S

4

67

How to add border around linear layout except at the bottom ? LinearLayout needs to have border at left, top and right side but not at the bottom.

Seascape answered 4/5, 2012 at 22:56 Comment(0)
T
171

Create an XML file named border.xml in the drawable folder and put the following code in it.

 <?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="#FF0000" /> 
    </shape>
  </item>   
    <item android:left="5dp" android:right="5dp"  android:top="5dp" >  
     <shape android:shape="rectangle"> 
      <solid android:color="#000000" />
    </shape>
   </item>    
 </layer-list> 

Then add a background to your linear layout like this:

         android:background="@drawable/border"

EDIT :

This XML was tested with a galaxy s running GingerBread 2.3.3 and ran perfectly as shown in image below:

enter image description here

ALSO

tested with galaxy s 3 running JellyBean 4.1.2 and ran perfectly as shown in image below :

enter image description here

Finally its works perfectly with all APIs

EDIT 2 :

It can also be done using a stroke to keep the background as transparent while still keeping a border except at the bottom with the following code.

<?xml version="1.0" encoding="utf-8"?>
 <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:left="0dp" android:right="0dp"  android:top="0dp"  
        android:bottom="-10dp"> 
    <shape android:shape="rectangle">
     <stroke android:width="10dp" android:color="#B22222" />
    </shape>
   </item>  
 </layer-list> 

hope this help .

Tyrus answered 5/5, 2012 at 13:12 Comment(10)
does it work with you, this tested and work with me , check it pleaseTyrus
Is there anything else to add? Didn't work for me. The other answer below worked though.Silassilastic
@Noman its work perfectlly with all APIs, check update answer pleaseTyrus
@AndroidStack.. i will surely try this next time... Thanks for your reply buddy :)Horror
did not work at all, dont know why ... lower shape post did work ... android 2.3.3Bunker
What about transparent rectangle instead of black with red border?Blondie
@hanry sorry i don't get you what do you mean ?Tyrus
@AndroidStack Here in your answer there are two layer one is black and other is red, suppose the layer is transparent instead of black then full red is shown instead of just left-right-top border.Blondie
@hanry you can do it in another way ,check EDIT 2Tyrus
This method works, but what if I want to save the default background drawable ( android:background="?android:attr/windowBackground") and add border to it? Following such approach we loose the ability to easily switch styles afterwards, hard-coding the background colors.Inseparable
T
57

Save this xml and add as a background for the linear layout....

<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
    <stroke android:width="4dp" android:color="#FF00FF00" /> 
    <solid android:color="#ffffff" /> 
    <padding android:left="7dp" android:top="7dp" 
            android:right="7dp" android:bottom="0dp" /> 
    <corners android:radius="4dp" /> 
</shape>

Hope this helps! :)

Tanishatanitansy answered 4/5, 2012 at 23:3 Comment(2)
This must be Accepted Answer.Work fine with Nexus 5 5.1.1Donofrio
I've tried this solution but the bottom border is still there?Loess
R
22

Kenny is right, just want to clear some things out.

  1. Create the file border.xml and put it in the folder res/drawable/
  2. add the code

    <shape xmlns:android="http://schemas.android.com/apk/res/android"> 
       <stroke android:width="4dp" android:color="#FF00FF00" /> 
       <solid android:color="#ffffff" /> 
       <padding android:left="7dp" android:top="7dp" 
            android:right="7dp" android:bottom="0dp" /> 
       <corners android:radius="4dp" /> 
    </shape>
    
  3. set back ground like android:background="@drawable/border" wherever you want the border

Mine first didn't work cause i put the border.xml in the wrong folder!

Repudiation answered 1/3, 2013 at 9:37 Comment(0)
K
2

Here is a Github link to a lightweight and very easy to integrate library that enables you to play with borders as you want for any widget you want, simply based on a FrameLayout widget.

Here is a quick sample code for you to see how easy it is, but you will find more information on the link.

<com.khandelwal.library.view.BorderFrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:leftBorderColor="#00F0F0"
            app:leftBorderWidth="10dp"
            app:topBorderColor="#F0F000"
            app:topBorderWidth="15dp"
            app:rightBorderColor="#F000F0"
            app:rightBorderWidth="20dp"
            app:bottomBorderColor="#000000"
            app:bottomBorderWidth="25dp" >
    </com.khandelwal.library.view.BorderFrameLayout>

So, if you don't want borders on bottom, delete the two lines about bottom in this custom widget, and that's done.

And no, I'm neither the author of this library nor one of his friend ;-)

Kwakiutl answered 12/6, 2015 at 11:35 Comment(1)
This is what i've been looking for :)Daiseydaisi

© 2022 - 2024 — McMap. All rights reserved.