Sunday, 20 May 2012

Shape drawable in Android

Hello Guys, Today I am going to post a demo for create a background using drawable xml file. Using xml you can create a background with gradient, round corner, background color and also you can set the padding in the background. I have create a xml file using shape attribute. Here is a example of the shape xml file.


background.xml
    
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <solid android:color="#77000000" />

    <corners android:radius="10dip" />

    <gradient
        android:angle="-90"
        android:endColor="#44FF0000"
        android:startColor="#CCFF0000" />

    <padding
        android:bottom="10dip"
        android:left="10dip"
        android:right="10dip"
        android:top="10dip" />

    <stroke
        android:width="1dip"
        android:color="#000000" >
    </stroke>

</shape>



This is a simple xml file in which there are some tags like shape, solid, corners, gradient, padding and stroke. 


The root tag of the xml file is a shape tag which indicates that you are creating a drawable for the background for some view. 


Solid tag is for paint the color of the shape in this tag there is a color attribute, this is for set the color of the shape.


Corner tag is for set the style of the corner of the shape. In this tag there is a radius attribute which is used to round the corner of the shape. 


Gradient is the tag which is used to set the gradient background of the shape. In this tag there are different types of the attributes like angle which indicates the angle of the gradient and there are two other attributes which are startColor and the endColor which are for set the start and end color of the gradient. 

There is a Padding tag which is used to set the padding of the shape. 

Stroke is used to create the border of the shape. in this tag there are with attribute which indicates the width of the border. There is a color attribute which is used to set the color of the border. If you want to set the dashed border instead of plain border then you can use other two attribute of the stroke.  android:dashWidth and  android:dashGap are used to set the dashed border of the shape. 

Now to use this drawable you just have to set this xml as the background of the view. 


for example.


<LinearLayout
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_margin="10dip"
      android:background="@drawable/background" >
 </LinearLayout>

4 comments:

  1. Hi,

    Have you encountered an issue with a shape drawable where it doesn't show up when the contents of the shape is too long, say a listview. I only encounter the problem in newer versions of Android.

    Thanks!

    ReplyDelete
  2. This comment has been removed by a blog administrator.

    ReplyDelete
  3. Excellent explanation with this and an example provided by this URL http://docs.xamarin.com/recipes/android/resources/general/style_a_button/

    i was able to understand how is this behavior in android.

    ReplyDelete

  4. life is about learning by the base of concept we provide studela. for the learning people it is very useful one. In studela we provide a video for maths coaching, science projects,bank exam, IAS exam,TNPSC group exam,RRB exam, other compatative exam, kids learning videos and some technology videos are availabe. watch more videos get your view smart


    ReplyDelete