I know this is a pretty old question, however I don't see the correct answer according to the question regarding alpha change using xml.
To do this, you need to create a selector, which is already shown in the question, and this:
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/inactive_button_background" android:state_enabled="false"/> <item android:drawable="@drawable/active_button_background" android:state_enabled="true"/> <item android:drawable="@drawable/pressed_button_background" android:state_pressed="true"></item> </selector>
So, if you only have a normal state, choosing (suppose it is btn_background.png) and you need 50% alpha in the down state, all you have to do is create the corresponding xml-drawable namedpressed_button_background.xml (in a folder with the option transfer, no -dpi!). So it should look like this:
<?xml version="1.0" encoding="utf-8"?> <bitmap xmlns:android="http://schemas.android.com/apk/res/android" android:src="@drawable/btn_background" android:alpha=".5" />
The same applies to the disconnected state, the only difference, I think, is the alpha level, which for this state can be 30% or whatever the designer says / wants). Thus, the shorter version will look like this (for a button with drawable ic_edit.png):
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_enabled="true" android:state_pressed="true"> <bitmap android:src="@drawable/ic_edit" android:alpha=".5" /> </item> <item android:state_enabled="false"> <bitmap android:src="@drawable/ic_edit" android:alpha=".3" /> </item> <item android:drawable="@drawable/ic_edit" /> </selector>
Stan
source share