Reputation: 35
I have to change tint color of imageview but programmatically. I will be getting a string value from server for eg:
"color":"#fff"
I have to set the same color as tint to an imageview.
The following code will not work =, as the image present in imageview is not set as background of it, but as src.
imageView.getBackground().setColorFilter(Color.parseColor("#ff8800"), PorterDuff.Mode.SRC_ATOP);
Also, these are following i tried with no luck. :( Attempt1:
ImageViewCompat.setImageTintList(imageView, ColorStateList.valueOf(Integer.parseInt("#ffffff")));
Attempt2:
imageView.setColorFilter(ContextCompat.getColor(context, Color.parseColor("#ffffff")), android.graphics.PorterDuff.Mode.SRC_IN);
Attempt3:
imageView.setColorFilter(ContextCompat.getColor(context, R.color.______), android.graphics.PorterDuff.Mode.MULTIPLY);
Kindly Note, I cannot use R.color.SOMETHING ,as i dont have that color in my file.
Thanking in advance.
Upvotes: 3
Views: 3572
Reputation: 62831
Try
imageView.setColorFilter(Color.parseColor("#ffffff"), PorterDuff.Mode.SRC_IN)
The valid color formats for Color.parseColor() are
#RRGGBB and #AARRGGBB
I think you know the following, but I will state it anyway: If the "#fff" value is a CSS hex color code then it is a shortened version of the real code "#FFFFFFFF". If you are passing in just "#fff", it will fail since Color.parseColor doesn't know about shortened 3-digit CSS color codes. See CSS HEX Colors.
Upvotes: 4