How to use selector to tint ImageView?
In reference to my solution at https://stackoverflow.com/a/18724834/2136792, there are a few things you're missing:
TintableImageView.java
@Override
protected void drawableStateChanged() {
super.drawableStateChanged();
if (tint != null && tint.isStateful())
updateTintColor();
}
public void setColorFilter(ColorStateList tint) {
this.tint = tint;
super.setColorFilter(tint.getColorForState(getDrawableState(), 0));
}
private void updateTintColor() {
int color = tint.getColorForState(getDrawableState(), 0);
setColorFilter(color);
}
drawableStateChanged() must be overridden for the tint to be updated when the element's state changes.
I'm not sure if referencing a drawable from a drawable might cause an issue, but you can simply move your selector.xml into a folder "/res/color" to reference it with "@color/selector.xml" (aapt merges both /res/values/colors.xml and the /res/color folder).
Set a Stateful Tint for an ImageView Programatically
You are using getColor
method in every tried line, but this isn't proper, that's not color, that's selector
(color_red_stateful
and color_green_stateful
files). use Resources
or preferably ContextCompat
class and its useful method getColorStateList
ColorStateList colorStateList = ContextCompat.getColorStateList(this, R.color.your_color_selector);
this colorStateList
should be set as imageTintList
for desired ImageView
s
btw. after setting above you may use view.invalidate()
method for forcing immediate apply of changes (force redraw), but in most cases, also probably yours, this won't be needed
How to set tint for an image view programmatically in android?
UPDATE:
@ADev has newer solution in his answer here, but his solution requires newer support library - 25.4.0 or above.
You can change the tint, quite easily in code via:
imageView.setColorFilter(Color.argb(255, 255, 255, 255));
// White Tint
If you want color tint then
imageView.setColorFilter(ContextCompat.getColor(context, R.color.COLOR_YOUR_COLOR), android.graphics.PorterDuff.Mode.MULTIPLY);
For Vector Drawable
imageView.setColorFilter(ContextCompat.getColor(context, R.color.COLOR_YOUR_COLOR), android.graphics.PorterDuff.Mode.SRC_IN);
Changing color of drawable in selector
Using tint on bitmap (** API 21+**):
Selector: drawable/selector_add
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<bitmap android:src="@drawable/ic_plus_circle_grey600_48dp"
android:tint="@color/blue_android_pressed" />
</item>
<item>
<bitmap android:src="@drawable/ic_plus_circle_grey600_48dp"
android:tint="@color/colorAccent" />
</item>
</selector>
In the ImageView
<ImageView
android:id="@+id/btAdd"
android:layout_width="wrap_content"
android:src="@drawable/selector_add"
android:layout_height="wrap_content"
/>
Change selector assigned to tint programmatically
Best way I found so far is to make a new color state list programmatically and assign it to the button, yikes, the goal was to avoid setting visual attributes like colors programmatically ...
ColorStateList buttonStates = new ColorStateList(
new int[][] {
{ -android.R.attr.state_enabled },
{}
},
new int[] {
Color.RED,
Color.BLUE
}
);
buttonMinus.setImageTintList(buttonStates);
Related Topics
How to Import a .Aar File into Android Studio 1.1.0 and Use It in My Code
No Notification Sound When Sending Notification from Firebase in Android
Button in Custom Android Toast
Passing JSONobject into Another Activity
How to Load HTML String in a Webview
How to Open Navigation Drawer with No Actionbar, Open with Just a Button
How to Change the Generated Filename for App Bundles with Gradle
Javamail API in Android Using Xoauth
Back to Main Activity from Notification-Created Activity
Sending Pictures to a Web Server
Genymotion Error After Windows 10 Upgrade
Android Camera: Onactivityresult() Intent Is Null If It Had Extras
Issues with Android Tabhost Example
Mediaplayer.Setdatasource() and Prepare() Not Working - Android
How to Create a New Avd in Eclipse
How to Import a .Aar File into Android Studio 1.1.0 and Use It in My Code