10

How to get android:tint="" color from ImageView via code?

Fabricio
  • 7,705
  • 9
  • 52
  • 87
  • I think `setImageTintList()` is what you want : [here](https://developer.android.com/reference/android/widget/ImageView.html#setImageTintList(android.content.res.ColorStateList)) – mt0s Nov 01 '16 at 18:17
  • Oh sorry my mistake. Check this [answer](http://stackoverflow.com/a/20121975/423980) – mt0s Nov 01 '16 at 18:18
  • @mt0s not to **set**, but to **get** – Fabricio Nov 01 '16 at 18:19
  • Right, I missed the get. Then `getColorFilter()` should work for you – mt0s Nov 01 '16 at 18:21
  • @mt0s I don't know how to get the color from ColorFilter object. – Fabricio Nov 01 '16 at 18:22
  • Does `getImageTintList()` not work for ya? It'll return a `ColorStateList`, but you can just call `getDefaultColor()` on that. – Mike M. Nov 01 '16 at 23:15
  • @MikeM. "call requires api 23" for `getImageTintList`. Weird since tint is appcompat. – Fabricio Nov 03 '16 at 09:06
  • Yeah, I thought you were dealing with API>=23, since you specified a straight `ImageView`. Anyhoo, the `tint` attribute does work with `AppCompatImageView`, but I can't find any simple way of getting it after it's set. They probably omitted `getImageTintList()` in appcompat 'cause it's a convoluted mess of varying helper classes just to get it set correctly on all supported versions. I'll let ya know if I find anything. – Mike M. Nov 03 '16 at 09:51

1 Answers1

9

To get the tint on all Android versions, use the ImageViewCompat:

int color = ImageViewCompat.getImageTintList(yourImageView).getDefaultColor();
Rafael Chagas
  • 774
  • 7
  • 13