Skip to content Skip to sidebar Skip to footer

Android Button Setcolorfilter Behaviour

(I changed the question a bit, because the problem is a bit clearer now) I have 4 buttons on my application, and when a user clickes certain button I change that button color. when

Solution 1:

I seem to remember having issues when creating too many ColorFilters before. It doesn't sound for certain like that's what's at fault here, since it's happening right away. Still, what you might try is having the filter as a class variable, and then using it within the if/else block. Also, as Trev mentioned, since you're just wanting to remove the green filter, you can just pass null to setColorFilter and avoid making the transparent filter, so you'd end up with something like this:

//in main classPorterDuffColorFiltergreenFilter=newPorterDuffColorFilter(Color.GREEN, PorterDuff.Mode.SRC_ATOP);

//in CheckAnswer()Drawabled= findViewById(R.id.ans2).getBackground();

if(answer.equals("1") d.setColorFilter(greenFilter)
else d.setColorFilter(null);

Solution 2:

The default behavior when calling setColorFilter(ColorFilter) on a Drawable does not automatically invalidate the Drawable, meaning it will not redraw itself solely as a result of the method call.

Try calling d.invalidateSelf() after setting the ColorFilter.

Solution 3:

Yesterday I posted a suggestion to a very similar problem that you asked here:

Android button setColorFilter behaviour

It appears that you have edited the code you originally posted there in order to incorporate the suggestions you were given (without acknowledging the answers) and then posted exactly the same code in this question.

Solution 4:

The Drawable documentation regarding setColorFilter(ColorFilter cf) states that 'null' may be passed to remove any existing filters. So, perhaps it could be that once the TRANSPARENT filter has been applied, then your subsequent GREEN filter can't be seen? I don't know enough about .setColorFilter and PorterDuff to know for sure, but it's worth a shot. Perhaps try:

d.setColorFilter(null); 
d.setColorFilter(filter); 

Also you could instead use this method:

setColorFilter(int color, PorterDuff.Mode mode) 

Solution 5:

You just need to mutate each drawable before setColorFilter.

Drawabled= findViewById(R.id.ans2).getBackground();

d = d.mutate();
d.setColorFilter

Post a Comment for "Android Button Setcolorfilter Behaviour"