84

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

Mohammad Nouri
  • 2,245
  • 4
  • 17
  • 34
  • Possible duplicate of [How to set tint for an image view programmatically in android?](http://stackoverflow.com/questions/20121938/how-to-set-tint-for-an-image-view-programmatically-in-android) – Bö macht Blau Jul 29 '16 at 08:00

11 Answers11

200

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?

Alan Kavanagh
  • 9,425
  • 7
  • 41
  • 65
El0din
  • 3,208
  • 3
  • 20
  • 31
  • is android 4.0 support android:tint="@color/colorAccent" ? – Mohammad Nouri Jul 29 '16 at 08:13
  • 11
    If you see getColor as deprecated you need to do following ```imageViewIcon.setColorFilter(ContextCompat.getColor(getContext(), R.color.blue));``` – reku Sep 03 '17 at 15:37
  • 1
    In that case, color that has alpha could not be applied. For example, if the color is "#90000000", black color will be applied. – Gary Chen Aug 10 '20 at 14:38
  • 1
    For me "android:tint="@color/colorAccent" was not working so I use "app:tint="@color/colorAccent" – Pavlos Mavris Apr 27 '23 at 08:10
27

Use tint attribute of ImageView.

 android:tint="@color/colorAccent"
Sujay
  • 3,417
  • 1
  • 23
  • 25
  • is android 4.0 support android:tint="@color/colorAccent" ? – Mohammad Nouri Jul 29 '16 at 09:02
  • @MohammadNouri No, color state lists and resource references in the `android:tint` attribute are not supported prior to Android 5.0 (API 21) – Louis CAD Apr 03 '17 at 18:54
  • 1
    Actually, the ColorStateList has been around since API v1 :https://developer.android.com/reference/android/content/res/ColorStateList.html – FractalBob Jul 24 '17 at 04:49
13
 DrawableCompat.setTint(imageView.getDrawable(), ContextCompat.getColor(getApplicationContext(), R.color.white));
Dhara Jani
  • 461
  • 3
  • 10
8

Use

imageView.setColorFilter(getResources().getColor(R.color.color_white)); 
chancyWu
  • 14,073
  • 11
  • 62
  • 81
Sharanjeet Kaur
  • 796
  • 13
  • 18
8

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"
Ignacio Tomas Crespo
  • 3,401
  • 1
  • 20
  • 13
  • 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. – Hashim Akhtar Apr 15 '21 at 14:07
  • 1
    If "add" doesn't work you can try other [modes](https://developer.android.com/reference/android/widget/ImageView#attr_android:tintMode) – 6rchid Jan 23 '23 at 01:15
4

Use this tint imageview in android XML

 android:tint="@color/yourcolor"
Shubhamhackz
  • 7,333
  • 7
  • 50
  • 71
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));
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" />
2

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

1
app:tint="@color___"
DavidUps
  • 366
  • 3
  • 9
0

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

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

  • 1
    This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/32110418) – Abhishek Dutt Jul 01 '22 at 13:48
  • 1
    This has already been mentioned in the corresponding comments. No need to add one more answer. – Abhishek Dutt Jul 01 '22 at 13:49
  • Ok I just mentioned there will be an error appeared – mohamed hesen Jul 11 '22 at 17:30