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);
How to programmatically change tint color of imageView?
Thanks for Hardik, orginal answer
You can change the tint, quite easily in code via:
imageView.setColorFilter(Color.argb(255, 255, 255, 255));
If you want color tint then:
imageView.setColorFilter(ContextCompat.getColor(context,
R.color.COLOR_YOUR_COLOR));
with mode:
imageView.setColorFilter(ContextCompat.getColor(context,
R.color.COLOR_YOUR_COLOR), android.graphics.PorterDuff.Mode.MULTIPLY);
in my case:
imgBottomDivider.setColorFilter(ContextCompat.getColor(getContext(),
mPreferences.isNightMode() ? R.color.colorWhite : R.color.colorBlack));
How to give tint color to imageview android?
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.
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 text view programmatically in android?
myTextView.setTextColor(myColorStateList)
was the appropriate API to be used for the TextView in this case
How to set color for imageview in Android
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?
How to change the tint color programmatically after setting it in imageview
You can use the following to set tint of your ImageView:
ImageViewCompat.setImageTintList(imageview, ColorStateList.valueOf(resources.getColor(R.color.red)));
Programmatically change backgroundTint of ImageView with Vector Asset for background
Instead of using ImageView
you can use AppCompatImageView
, Because setBackgroundTintList
is supported from API level 21, If you use AppCompatImageView
you can change the tint color using setSupportBackgroundTintList
.
so change you ImageView like this,
<android.support.v7.widget.AppCompatImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="@color/colorRed"
android:background="@drawable/ic_delete"/>
So that you can call setSupportBackgroundTintList
to set the tint color like this,
imageView.setSupportBackgroundTintList(ContextCompat.getColorStateList(this, R.color.colorBlue));
Related Topics
Refreshing Oauth Token Using Retrofit Without Modifying All Calls
Android Paint: .Measuretext() VS .Gettextbounds()
What Is the Size Limit for Logcat and How to Change Its Capacity
How to Serialize an Object and Save It to a File in Android
How to Use Addr2Line in Android
How to Build an APK File in Eclipse
How to Add a Custom Button State
Foreground Service Being Killed by Android
How to Remove Application from App Listings on Android Developer Console
Appcompat V7 R21 Returning Error in Values.Xml
How to Handle an Asynctask During Screen Rotation
Accessing Localhost of Pc from Usb Connected Android Mobile Device
Google Sign in Failed Com.Google.Android.Gms.Common.Api.Apiexception: 10:
Getting View's Coordinates Relative to the Root Layout
How to Set Onclicklistener on a Radiobutton in Android