Fiiling an image with color in android - android

how to fill an image with a color in android? Like the image given below

You can set color to the icon by using the below code
ImageView imageView = (ImageView) findViewById(R.id.your_imageview);
imageView.setColorFilter(Color.RED);

Option 1: use backgroundTint property in xml file of ImageView
Option 2:
ImageView imageView = ...;
Drawable drawable = imageView.getDrawable();
ColorFilter colorFilter = ColorFilterGenerator.from(drawable).to(Color.RED);
imageView.setColorFilter(colorFilter);

Since,Android Drawable Tinting is supported in Android 5.0+ (API 21+) only. (not 100% sure ).
u can provide two different images in drawable folder and set it problematically when user clicks on it which will work in every android version.
public boolean enable = false;
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(enable)
{
imageView.setImageResource(R.drawable.ic_home_enable);
}
else
{
imageView.setImageResource(R.drawable.ic_home_disable);
}
}
});

Related

Android ImageView's drawable tint not working

I'm having problems with my ImageView. Btw, I'm using CircularImageView which has a property app:selector_color.
This is the scenario. ImageView has a drawable (src). What I want to happen is when I click on the ImageView, I want to add tint to it.
This is what I've tried so far:
viewHolder.ivProfile.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result = data.get(position);
int highlightColor = context.getResources().getColor(R.color.main_red_transparent);
PorterDuffColorFilter colorFilter = new PorterDuffColorFilter(highlightColor, PorterDuff.Mode.SRC_ATOP);
CircularImageView ci = (CircularImageView) v;
if (thumbnailsselection[position]) {
ci.setSelected(false);
thumbnailsselection[position] = false;
viewHolder.tvTitle.setVisibility(View.GONE);
} else {
ci.setSelected(true);
ci.getDrawable().setColorFilter(highlightColor, PorterDuff.Mode.SRC_ATOP);
thumbnailsselection[position] = true;
viewHolder.tvTitle.setVisibility(View.VISIBLE);
viewHolder.tvTitle.setText(result.get(NAME));
}
}
});
But this solution does not work. Anybody here tried this before? I would gladly appreciate any help. Thanks in advance!
Try using selector color instead of settings filter as following:
CircularImageView ci = ...;
ci.setSelected(true);
ci.setSelectorColor(highlightColor);

setColorFilter does not work after setting background via setBackgroundResource

I crated a small app, which cycles through tinted images of an ImageView on clicks.
It works well with the image set in the layout file, but it does not work when setting the image from the code like below.
Any help appreciated.
public class MainActivity extends Activity {
private ImageView mPic;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPic = (ImageView) findViewById(R.id.pic);
mPic.setBackgroundResource(R.drawable.msh);
mPic.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Random random=new Random();
ColorFilter cf = new PorterDuffColorFilter(Color.argb(192, random.nextInt(255), random.nextInt(255), random.nextInt(255)),Mode.SRC_ATOP);
mPic.setColorFilter(cf);
}
});
}
}
The color filter is applied to the ImageView content, not its background. Use ImageView#setImageResource(int resId) to set the content and color filter will be applied.
If you need to add the ColoFilter to ImageView's background, you can try something like mPic.getBackground().setColorFilter() (assuming that getBackground() returns non-null value).

android change image onclick imageviev

How can I change Image of an Imageview?
I want to get the image associated; if it is img1 I want set the image to img2, if it is img2 I want to set the image to img2.
first set the tag of imageview in xml to 1
final ImageView imageview = (ImageView) findViewById(R.id.imageView1);
imageview.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (Integer.parseInt(imageview.getTag().toString()) == 1) {
imageview.setBackgroundResource(R.drawable.image2);
imageview.setTag(2);
} else {
imageview.setBackgroundResource(R.drawable.image1);
imageview.setTag(1);
}
}
});
I think you are looking for ImageView.setTag() and ImageView.getTag().It checks if the image is associated with the Image view as #ρяσѕρєя K mentioned in comment.
There are two version of setTag one which takes object as an argument and other takes key and object as an argument

Android View.getBackgroundColor #XXXXXXXX

The background color of my View is #FFFFFFFF. I want to get this through code. I do not want to just put #FFFFFFFF into the method because I will be changing the background through code, so this value will change all the time.
public void toggleEraser() {
ImageView btnEraser = (ImageView) this.findViewById(R.id.imgEraser);
ImageView btnBrush = (ImageView) this.findViewById(R.id.imgBrush);
if (erase) {
btnEraser.setImageResource(R.drawable.greyeraser);
btnBrush.setImageResource(R.drawable.brush);
} else {
btnEraser.setImageResource(R.drawable.eraser);
btnBrush.setImageResource(R.drawable.greybrush);
}
erase = !erase;
if (erase){
//Here is the problem
drawView.setColor(//drawView.getBackgroundColor());
}
else
drawView.setColor(brushColor);
drawView.setErase(erase);
}
store the color you want 0xFFFFFFFF in somevariable and then
change drawView.setColor( to drawView.setBackgroundColor(somevariable);
Check out this answer: Get the background color of a button in android. it is a little different but can lead you on the right path.

replace image on ImageButton

I'm new to andorid, and I'm trying to do a simple thing:
when button A is clicked, I want to replace the image displayed
on ImageButton B.
I've tried all sort of things like:
msortByButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Test change image
mdefineLocationButton.setBackgroundResource(0);
mdefineLocationButton.invalidate();
mdefineLocationButton.setBackgroundResource(R.drawable.notdefined);
mdefineLocationButton.invalidate();
mdefineLocationButton.refreshDrawableState();
}
});
But it seems that the new image is painted, but on top of it the old image
is painted also. (I can see the old image, below it I can see edges of the new image).
Any idea how to do this right?
Thanks,
Omer
Use setImageResource (int resId)
(http://developer.android.com/reference/android/widget/ImageView.html#setImageResource(int))
ImageButtons can have a background and an actual image src. My guess is initially you set the image src and now in code you are setting the background. So both show.
#Override
public void onClick(View v) {
mdefineLocationButton.setImageResource(R.drawable.notdefined);
}

Categories

Resources