I have a lot of button with a lot of different background colors. I want to know if there is a way to apply some color filter on click. For example, i want that all my buttons become darker on click. They keep the original color, but it's darker.
Is there an easy way to do it, or i have to define the darker color for each button?
Thanks.
I assume you want the button to darken on touch, and revert to normal when the user releases the button.
I would suggest creating a custom button which does the work for you:
import android.content.Context;
import android.graphics.LightingColorFilter;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.Button;
public class DarkenButton extends Button {
public DarkenButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public DarkenButton(Context context) {
super(context);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// darken background
getBackground().setColorFilter(
new LightingColorFilter(0xff888888, 0x000000));
break;
case MotionEvent.ACTION_UP:
// clear color filter
getBackground().setColorFilter(null);
break;
}
return super.onTouchEvent(event);
}
}
Then use DarkenButton anywhere you would normally use a Button.
Try this-
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
v.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0xFFAA0000));
}
}
and for clear filter do this
button.getBackground().clearColorFilter();
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