android – Set a border in corners of layout-ThrowExceptions

Exception or error:

I want to add border like the one in the image below. I tried achieve this using xml drawables’ layer-list component, but I couldn’t.

How to solve:

create new drawable resource file into the drawable folder and add below code

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <solid android:color="#FFFFFF" />
   <corners android:radius="@dimen/_10sdp"
</shape>

and set this drawable as the background of your parent layout

###

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"

Finally its works perfectly with all APIs

Let me know if it was usefull Behzad Fartash

Leave a Reply

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