Android ImageButton setColorFilter not working

I am looking for messages that answer this problem, but none of them work for me, so I think I have a fundamental misunderstanding of how it should work. I have an ImageButton to which the png file applies. Png is mostly transparent except for the white arrow. I want the red arrow to be red using setColorFilter:

imageButton.setColorFilter(Color.argb(255, 225, 0, 0)); 

but it does not affect. I tried the version of setColorFilter with various Porter-Duff modes, but none of them worked. Any ideas on what might be the problem or what I might skip would be greatly appreciated.

+6
source share
1 answer

You should get Drawable from the button, since the setColorFilter that you are trying to use (in your setup) is applicable to them.

 ImageButton btn = (ImageButton) myLayout.findViewByID(R.id.my_button); int mycolor = getResources().getColor(R.color.best_color); btn.getDrawable().setColorFilter(mycolor, PorterDuff.Mode.SRC_ATOP); 

If you have the correct reference to the Drawable,

eg textView.getCompoundDrawables()[2].setColorFilter(...);

which in its xml:

 <TextView ... android:drawableLeft="..." ... /> 

you can use myDrawableObject.setColorFilter () as you like.

Edit:

For ImageButton for imageButton.getDrawable() for imageButton.getDrawable() corresponds to android:src="..." , and imageButton.getBackground() corresponds to the property android:background="..." . Make sure you type setColorFilter in the correct path.

+10
source

Source: https://habr.com/ru/post/983020/


All Articles