
Each drawable is represented by an element inside a single element. Each drawable in the list is drawn in the order of the list-the last drawable in the list is drawn on top. Creating a Layer ListĪ LayerDrawable is a drawable object that manages an array of other drawables. Using state lists allows us to easily define dynamic views that respond to pressed, checked, enabled or other related states. There are also color state selectors which allow a color to be chosen based on view state such as in a file named res/color/button_text.xml: Īnd applied to any field that accepts a color value such as the textColor property of a button in a layout file: Note that any view can have a state selector, but the most common uses are with buttons and listview items. Now, when the view (i.e button) is pressed or focused, the drawable used for the view will change accordingly. The illustration below shows all the major states that can be represented:įor example, a state list XML for a button background might look like the following in a file such as res/drawable/selector_button_bg:

The state list supports different view states such as android:state_pressed, android:state_focused, android:state_enabled, android:state_selected, among many others. For example, a Button widget can exist in one of several different states (pressed, focused, or neither) and, using a state list drawable, you can provide a different background image for each state. State ListĪ StateListDrawable is a drawable object defined in XML that uses several different images to represent the same graphic, depending on the state of the object. Note that custom shapes can be created at runtime using other shape drawable types using PathShape and ArcShape.

Using solid color shapes and gradients we can customize the appearance of buttons, layouts and other views without requiring the use of any images. You can also setup radial-type gradients with: Īnd applied to a TextView, this looks like: Here's an example of a simple linear gradient shape specified in res/layout/drawable/gradient_shape.xml: Īpplied to a button and the resulting view looks like: Different gradients such as radial, linear or sweep can be selected using the type property. Shapes also support gradients backgrounds as well as supporting properties such as startColor, centerColor, endColor, angle. Note that drawables can be applied to any view and are usually set with the background property referencing the drawable resource. Here's an example of drawing a rounded rectangle with a border in res/layout/drawable/solid_color_shape.xml: Īnd then applied to a TextView using the background property: The shape can be described with properties such as corners for rounding, gradient for backgrounds, padding for spacing, solid for background colors, and stroke for border. For example, you can use a shape drawable to change the shape, border, and gradient of a button background.Ī shape is simply a collection of properties that are combined to describe a background. This is used to create a complex shape that can then be attached as the background of a layout or a view on screen. The Shape Drawable is an XML file that defines a geometric shape, including colors and gradients. Let's explore these drawable file types one by one and take a look at examples of usage.

There are at least 17 types of drawables but there are five that are most important to understand:

Usageĭrawables can be an initially overwhelming topic because there are many drawable types used in different situations such as drawing shapes, setting state behaviors for buttons, creating stretchable button backgrounds and creating compound drawable layers. Drawables tend to be defined in XML and can then be applied to a view via XML or Java.įor a list of the default drawables for every version of Android, see the androiddrawables site as an excellent reference. This is typically used for customizing the view graphics that are displayed within a particular view or context. which can then be applied to views within an Activity. Drawables are used to define shapes, colors, borders, gradients, etc. A drawable resource is a general concept for a graphic that can be drawn to the screen.
