Color Drawables

You can create a PaintDrawable object that is a rectangle of color, with optionally rounded corners. This element can be defined in any of the files inside res/values/.

Source file format: XML file requiring a <?xml version="1.0" encoding="utf-8"?> declaration, and a root <resources> element containing one or more <drawable> tags.

Resource source file location: res/values/colors.xml (File name is arbitrary; standard practice is to put the PaintDrawable items in the file along with the numeric color values.)

Compiled resource datatype: Resource pointer to a PaintDrawable.

Resource reference name:

• XML: @[package:]drawable/some_name (where some_name is the name of a specific resource) Syntax

<drawable name=color name>color value</drawable>


A valid color value.

• name - The name used in referring to this drawable. Example XML Declaration

The following code declares several color drawables.





red">#f0 0</drawable>










Example Java code

// Assign a PaintDrawable as the background to // a TextView on the current screen.

Drawable redDrawable = Resources.getDrawable(R.drawable.solid_red); TextView tv = (TextView)findViewByID(; tv.setBackground(redDrawable);

Example XML code

<TextView android:layout_width="fill_parent"

android:layout_height="wrap_content" android:textAlign="center"


0 0

Post a comment