Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Change image color dynamically in android?

Tags:

android

I am doing such type of project ,In my project change Image color dynamically.

I have a one black shape color image ,when user click on this image change image color dynamically green.

enter image description here

Googling and other document follow but I am not solve my problem .

Please help me , is there any method or document to follow solve my problem ,

like image 296
Hemantvc Avatar asked Jan 08 '13 04:01

Hemantvc


People also ask

How to image color change in android?

Tint color means when we want to change the color of the image while rendering in ImageView. In XML is very easy to change tint color by just setting up the attribute android:tint="" in the ImageView tag, as shown in the following example.


2 Answers

Here's how I do this: It's pulling the color from a resource xml file.

<resources> <color name="new_color">#FFAAAAAA</color> </resources> 

In your activity .java file:

import android.graphics.PorterDuff.Mode;  Resources res = context.getResources(); final ImageView image = (ImageView) findViewById(R.id.imageId); final int newColor = res.getColor(R.color.new_color); image.setColorFilter(newColor, Mode.SRC_ATOP); 

To clear it call:

image.setColorFilter(null); 
like image 55
Chuck D Avatar answered Sep 23 '22 06:09

Chuck D


Set android:tint attribute of image/image button to the color you need.

android:tint="@android:color/black" 

Optionally you can set android:tintMode attribute.

like image 29
Borzh Avatar answered Sep 22 '22 06:09

Borzh