Drawables in Android

Posted By : Rahul Baboria | 17-Sep-2017

Android UI provides customization such that we can play with parent UI widgets as well. There are customized ImageViews e.g: CircularImageview which inherits ImageView class but in order to customize backgrounds or images android has special files i.e. drawables files where we can create customized backgrounds.Drawables can be created by simply adding the new drawable file in res directory. In this code snippet, i've given an example to create drawable. We can use layers by using a layer-list tag and we can also use images in drawable files.
 

 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item  android:top="5dp">
        <shape>
            <corners android:radius="1dp" />
            <solid android:color="#D6D6D6" />
        </shape>
    </item>
    <item android:bottom="2dp" >
        <shape>
            <gradient android:angle="270"
                android:endColor="#e11515" android:startColor="#e11515"/>
            <stroke android:width="1dp" android:color="#e11515" />
            <padding android:bottom="10dp"
                android:top="10dp" />
        </shape>
    </item>
</layer-list>

 

 

If we are creating a custom toolbar or giving some background to toolbar it will look like this:

 

 

MainActivity.xml

 

 

Drawables can be defined in following types by using tags:

 

  • Bitmap: an Image .jpg or .png
     
  • Nine Patch: png image that can be stretched.
     
  • Vector: a drawable outlined in associate XML file as a collection of points, lines, and curves together with its associated color data. this sort of drawable is scaled while not a loss of show quality.
     
  • Shape: simple drawable file.
     
  • Layers: (layer-list) a drawable file with multiple layers for drawable.
     
  • States: (selector) a compound drawable that can act according to state.
     
  • Levels: a compound drawable that selects one of a set of drawables based on its level.
     
  • Scale: a compound drawable with a single child drawable, whose overall size is modified based on the current level

 

Thanks.

 

About Author

Author Image
Rahul Baboria

Rahul Baboria is having good knowledge over Android Application.

Request for Proposal

Name is required

Comment is required

Sending message..