Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 757
Damir Avatar asked May 04 '12 22:05

Damir


People also ask

How do you put a border on a linear layout?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above code we have taken one text view with background as border so we need to create a file in drawable as boarder.

How do you add a border on Kotlin?

Create an Android Application with Kotlin support and Empty Activity. Right click on res/drawable folder and click on New -> Drawable Resource File. Give file name as border(or any other name) and Root element as shape. border.

What is layout explain linear layout with an example?

In Android, LinearLayout is a common layout that arranges “component” in vertical or horizontal order, via orientation attribute. In additional, the highest “ weight ” component will fill up the remaining space in LinearLayout .


2 Answers

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 .

like image 54
Android Stack Avatar answered Sep 29 '22 20:09

Android Stack


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! :)

like image 36
Kenny Avatar answered Sep 29 '22 18:09

Kenny