How to Set Tint for an Image View Programmatically in Android

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));

in night mode
in day mode

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 ImageViews

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



Leave a reply



Submit