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);
Related
How to create a fade in ImageView to change it to other image (i.e. from Image 1 to Image 2) when imageView is clicked and vica versa
I have created another click method but it is not working because i think as I have already put image-2 aplha to 0.
public void fade(View view)
{
ImageView mickey=findViewById(R.id.mickey);
ImageView mouse = findViewById(R.id.imageView2);
mickey.animate().alpha(0f).setDuration(2000);
mouse.animate().alpha(1f).setDuration(2000);
}
Here is a simple method to play around with: :)
boolean firstImage = false;
private void fadeFlip(View view) {
AlphaAnimation aa = new AlphaAnimation(1f,0f);
aa.setDuration(1*1000);
view.startAnimation(aa);
aa.start();
if(firstImage) {
view.setBackgroundResource(R.drawable.ic_launcher);
} else {
view.setBackgroundResource(R.drawable.image_2);
}
AlphaAnimation bb = new AlphaAnimation(0f,1f);
bb.setDuration(1*1000);
view.startAnimation(bb);
bb.start();
firstImage = !firstImage;
}
I need to use text view as CheckBox background and set different color for each state. Some how I managed to get it with Java. But I don't think it is the
proper way. Is there any other method to achieve this?
Implement onClickListener to your textview, something like:
int ispressed = 0;
TextView txtview = (TextView) findViewbyId(R.id.textview1);
txtview.setOnClickListener(new new View.OnClickListener() {
#Override
public void onClick(View v) {
if(ispressed=0){
//CHANGE BACKGROUND COLOR
ispressed=1;
}else{
//RESTORE BACKGROUND COLOR
}
}
});
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);
}
}
});
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).
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.