87

I want set icon into ImageView and i downloaded icons from this site : FlatIcon
Now i want set color to this icons but when use setBackground just add color for background and not set to icons!
When use NavigationView i can set color to icons with this code : app:itemIconTint="@color/colorAccent".

How can i set color to icons into ImageView such as itemIconTint ? Thanks all <3

1

11 Answers 11

204

If you are using an icon maybe this can be useful:

android:tint="@color/colorAccent"

Otherwise you can try to modify the class:

ImageView imageViewIcon = (ImageView) listItem.findViewById(R.id.imageViewIcon);
imageViewIcon.setColorFilter(getContext().getResources().getColor(R.color.blue));

More info in this thread: Is it possible to change material design icon color from xml in Android?

Sign up to request clarification or add additional context in comments.

4 Comments

is android 4.0 support android:tint="@color/colorAccent" ?
If you see getColor as deprecated you need to do following imageViewIcon.setColorFilter(ContextCompat.getColor(getContext(), R.color.blue));
In that case, color that has alpha could not be applied. For example, if the color is "#90000000", black color will be applied.
For me "android:tint="@color/colorAccent" was not working so I use "app:tint="@color/colorAccent"
29

Use tint attribute of ImageView.

 android:tint="@color/colorAccent"

3 Comments

is android 4.0 support android:tint="@color/colorAccent" ?
@MohammadNouri No, color state lists and resource references in the android:tint attribute are not supported prior to Android 5.0 (API 21)
Actually, the ColorStateList has been around since API v1 :developer.android.com/reference/android/content/res/…
13
 DrawableCompat.setTint(imageView.getDrawable(), ContextCompat.getColor(getApplicationContext(), R.color.white));

Comments

9

Latest Lint shows a warning using android:tint, recommending to use app:tint, but the tint is not visible until you use it together with app:tintMode. So it looks like this:

app:tint="@color/yourcolor"
app:tintMode="add"

2 Comments

In my case, app:tintMode="add" does nothing. Also the effect of app:tint="@color/yourcolor" is also visible at runtime, i.e. cannot see it applied in the designer.
If "add" doesn't work you can try other modes
9

Use

imageView.setColorFilter(getResources().getColor(R.color.color_white)); 

Comments

4

Use this tint imageview in android XML

 android:tint="@color/yourcolor"

Comments

4

Just add this line in your image view.

In XML File:-

 android:tint="@color/color_name"

           or


app:tint="@color/color_name"

In Java File:-

imageViewIconName.setColorFilter(getContext().getResources().getColor(R.color.color_name));

Comments

3

You can simply use android:tint, but since there were some issues in some versions.

use AppCompatImageView and it will work fine.

<AppCompatImageView 
        android:tint="@color/your_color" />

Comments

2

Just use: ivMyImageView.setColorFilter(ActivityCompat.getColor(context, android.R.color.holo_green_light))

Comments

1
app:tint="@color___"

Comments

0

El0din has answered with "android:tint="@color/colorAccent""

but you will get an error You Have to change "android" to be "app"

3 Comments

This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
This has already been mentioned in the corresponding comments. No need to add one more answer.
Ok I just mentioned there will be an error appeared

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.