Sunday, May 13, 2012

XML based Drawables


Android gives you the ability to define shape drawables using xml.Basically these are simple shapes that are great for using as backgrounds or buttons.  The sample below will create a white oval that I use for backgrounds of linear layouts, say for a product description with an image and price. More information is at the link. To add a drawable, pick one of res/drawable directories and save the xml file right to it. For example, I saved a file called shape_white_four_corners.xml to my drawables-hdpi directory.  It'll appear as part of your resource and you can access it just like any other drawable.

http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape


 <?xml version="1.0" encoding="utf-8"?> 

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

 android:background="@color/white"> 

   <gradient android:startColor="@color/white" android:endColor="@color/white" 

       android:angle="270"/> 

   <stroke android:width="0.5dp" android:color="@color/white"/>     

   <padding android:left="7dp" android:top="7dp" 

       android:right="7dp" android:bottom="7dp" /> 

   <corners android:bottomRightRadius="8dp"  

           android:bottomLeftRadius="8dp" 

           android:topLeftRadius="8dp"  

           android:topRightRadius="8dp"/> 

 </shape>