Along with being able to provide views (and many widgets) with a single background image, Android also allows us to add more than one image to certain views so that we can represent various states (such as pressed or focused) graphically. To create a widget with three states: pressed, focused, and normal, we will need three image files which can then be defined in XML as a single resource.
Select three different image files (JPG, PNG, BMP, or GIF) of the same size, but no wider than 200 pixels:
Start up a new Android project in Eclipse and locate the
res/drawable-mdpi
folder.Drag the image files into the
drawable-mdpi
folder and name them as follows:button_pressed.png
button_normal.png
button_focused.png
Create a new XML file in the same folder called
my_button.xml
.Fill out the
my_button.xml
file as follows:<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item ...