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.
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.
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.
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 .
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:
ALSO
tested with galaxy s 3 running JellyBean 4.1.2 and ran perfectly as shown in image below :
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 .
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! :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With