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 ima
In XML use src not background tag in ImageView. In java code-
import android.graphics.PorterDuff.Mode;
final Context context=this;
home1=(ImageView) findViewById(R.id.home1);
Resources res = context1.getResources();
final int newColor = res.getColor(android.R.color.black);
home1.setColorFilter(newColor, Mode.SRC_ATOP);
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.
imageView.setImageResource(R.drawable.ic_person_black_48dp);
imageView.setColorFilter(imageView.getContext().getResources().getColor(R.color.desired_color), PorterDuff.Mode.SRC_ATOP);
Put this in your OnDraw, just before you draw your square.
if (userclicked){
paint.setColor(Color.GREEN);
} else {
paint.setColor(Color.BLACK);
}
Of course that is if you are drawing it with canvas.drawRect(x0,y0,x1,y1,paint) which you would if you were drawing a simple shape like that.
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);
Create resource in drawable folder like
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<bitmap
android:src="@drawable/rect"
android:tint="@color/red"/>
</item>
</layer-list>
android:tint="@color/red"
does it.