Android: Set alpha on button on click. Preferably xml only
alpha, android, button, layout
Solution
I am bit late to answer this question but i just did this by changing the alpha value using View.onTouchListner
You can do this by implementing onTouch in this way
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
v.setAlpha(0.5f);
break;
case MotionEvent.ACTION_UP:
v.setAlpha(1f);
default :
v.setAlpha(1f)
}
return false;
}
I guess you cant change alpha value from drawable so you will have to do this in this way only.
Problem
I want to introduce a 'down' style on some buttons. I've created a style, and a state list. ``` <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> ``` The problem is that I want to be able to change just the alpha of the background when the button gets clicked (I will have variable backgrounds, so setting a sold color with alpha channel is not a solution). And I want to do this from the declarative xml only (don't want to polute my code with layout stuff). Problem is I don't know how to apply this alpha blending to the button from a xml drawable. I am pretty sure there's a way though.