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.
Create a drawable xml resource with bitmap as the root say bg. set android:src="@drawable/background" to the image which you want as the background. set android:alpha="0.4" to any value between 0 to 1 as per the required opacity.
"alpha" is used to specify the opacity for an image.
To change the default Button style of the application we can use the android:buttonStyle attribute in the AppTheme style inside the styles. xml.
A selector can be an xml file created inside the drawable folder. Selector for different background colors. The following selector file btn_bg_selector. xml contains the code for setting different background colors on a button for different states.
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.
This works perfectly
yourView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
v.setAlpha(0.5f);
break;
}
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL: {
v.setAlpha(1f);
break;
}
}
return false;
}
});
Adding something to Michael answer, if you are looking for a "softer" click animation. I added this:
btn.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent motionEvent) {
switch (motionEvent.getAction())
{
case MotionEvent.ACTION_DOWN:
v.animate().alpha(0.3f).setDuration(200).start();
break;
case MotionEvent.ACTION_UP:
v.animate().alpha(1f).setDuration(200).start();
default :
v.setAlpha(1f);
}
return false;
}
});
}
with lies on the same idea but with a animation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With