Darolla asked for some more info about the <selector> xml drawable in the tutorial request thread. So as I like the functionality of the selector, I made a little tutorial about this. As there is no documentation about this xml tag, I hope you get rolling using this tutorial.
First of all, we need to have (at least) one button in the content view of our activity. What is going to be special about this button is, that we are going to define a background drawable. As result this is the main.xml:
Using xml Syntax Highlighting
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <Button
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello"
- android:background="@drawable/my_button"
- />
- </LinearLayout>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4
The Selector xml tag we were talking about is a drawable xml resource. This means we need to make an xml file in the drawable folder with the name we want. For this tutorial we named it 'my_button.xml'. In my_button we define 4 states (as a button has 4 states: not pressed and not focused, pressed and focused, pressed and not focused, not pressed and focused) with each of those states having their own drawable.
Using xml Syntax Highlighting
- <?xml version="1.0" encoding="utf-8"?>
- <selector xmlns:android="http://schemas.android.com/apk/res/android">
- <item android:state_focused="true" android:state_pressed="false" android:drawable="@drawable/focused" />
- <item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/focusedpressed" />
- <item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/pressed" />
- <item android:drawable="@drawable/defaultbutton" />
- </selector>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4
). And the last <item> defines the default drawable.Now, start running this example and you will have your own custom button.




