ImageView remove tint color? - android

Before I decided to ask it, I search some Remove Tint Color Programmatically, but it marked as duplicate so I didn't know if this is also a duplicate or not? because it didn't answer my question, I think.
Because I am asking also if is it possible to RESET or REMOVE the tint of ImageView after changing the drawable.
This is the normal image
This is the selected image
and this is the normal image again after deselecting the image
and this is my code.
if (isSelected) {
// Reset the ImageView to normal
isSelected = false
imgHeart.setImageDrawable(itemView.context.getDrawable(R.drawable.baseline_favorite_border_24))
} else {
// Tint ImageView to Red
isSelected = true
imgHeart.setImageDrawable(itemView.context.getDrawable(R.drawable.baseline_favorite_24))
imgHeart.imageTintList = ColorStateList.valueOf(ContextCompat.getColor(itemView.context, R.color.colorRedHeart))
}
I also try the imgHeart.imageTintList = null but the image is turning into white or disappear.
I also try the imgHeart.colorFilter = null but the image is do nothing and turning into the last image.
I am using the Material Design Icon.
What I want to bring back the image into normal drawable as you see in the first image.
Thank you.

The selected drawable is a completely different drawable than the other 2. You can't tint baseline_favorite_border_24 to look like the selected one, you can tint only the borders and can't fill it with color. By the way imageTintList requires API 21+. So use 2 drawables(you don't need to download them, they exist in Android Studio), name the selected one as baseline_favorite_filled_24 and toggle between them.
if (isSelected) {
isSelected = false
imgHeart.setImageDrawable(itemView.context.getDrawable(R.drawable.baseline_favorite_border_24))
} else {
isSelected = true
imgHeart.setImageDrawable(itemView.context.getDrawable(R.drawable.baseline_favorite_filled_24))
}

Related

How to reset CardView background?

I have a CardView which is getting colored after a button click ("reveal right answer" functionality). The card needs to get back to it's original color for the next question. I am using the default Android color, but I couldn't find the value I need to reset to (R.attr.cardBackgroundColor turns my card blue...), so I used tint like so:
#BindingAdapter("isRight", "showResult")
fun CardView.setCardTint(
isRight: Boolean,
showResult: Boolean,
) {
background.setTintList(null)
if (showResult) {
val colorRes = if (isRight) R.color.result_right
else R.color.result_wrong
background.setTint(ContextCompat.getColor(context, colorRes))
}
}
This works great, but now I need to change the colors to gradients. For that I need to go back to the original solution and change the background itself (to the gradient drawables) - and to my original problem: How to reset the CardView background to it's Android default value?

Color set by setColorFilter disappears on prelollipop devices

I have a TabLayout which has icons. The idea is to change the colors runtime.
I have and xml drawable file with states: state_pressed, state_selected and default with the same white picture so I can put color later.
I take the drawables for different states:
Drawable[] drawables = stateListDrawable.getConstantState();
and the for every drawable state I put color from another array:
drawables[i].setColorFilter(colors[i], PorterDuff.Mode.MULTIPLY);
The issue is that the color is visible in the beginning, but when I start to click on the icons all the icons becomes white again and I lose the tint.
Everything is working as expected on lollipop and above.
Use the tint method from the v4 Support library
drawables[i] = DrawableCompat.wrap(drawables[i])
DrawableCompat.setTint(drawables[i], colors[i])
I have found my solution, which does not look clean at all, but at least it is working :)
I have created CustomStateListDrawable which extends from StateListDrawable and added the drawables for the different states. Then I have overridden all the methods in the class to see which ones are called and tried to change the colors there. The called late enough(my changes will not be overridden after I make them) was getState(). I have created also a ColorStateList object to hold my colors so the code will look like this:
private ColorStateList colorStateList;
public int[] getState() {
if (colorStateList != null) {
// Resolve the color for the current state
int color = colorStateList.getColorForState(super.getState(), 0);
// Get the current drawable and changed its color.
if (getCurrent() != null) {
getCurrent().setColorFilter(color, PorterDuff.Mode.MULTIPLY);
}
}
return super.getState();
}
Basically every time when there is a change in the state I get the current drawable and change its color.

Drawable ColorFilter kept through Activity Transitions for the same resource

I have an Activity with some menu items, for which I change the icon colors like this:
private void colorMenuItem(MenuItem item) {
if (item != null) {
Drawable icon = item.getIcon();
if (icon != null) {
icon.setColorFilter(getResources().getColor(R.color.some_color), PorterDuff.Mode.SRC_ATOP);
}
}
}
The icons are white drawable PNG files from the material icon set. This works as expected and the menu items are colored as desired.
However, when I start another activity which makes use of the same drawable resource which was already colored, but in another view (e.g a FAB instead of the menu), the ColorFilter for this view remains. Why is that?
Shouldn't the FAB load the resource file again and shouldn't it be white (or unchanged)?
However, when I start another activity which makes use of the same drawable resource which was already colored, but in another view (e.g a FAB instead of the menu), the ColorFilter for this view remains. Why is that?
That is because the drawable or bitmap has been cached and therefore the next call to the same drawable id will be the same drawable that was already tinted before.
To fix it is to mutate first the drawable before using it to have another instance of it, to prevent it from being cached.
Drawable icon = item.getIcon().mutate(); //mutate it to prevent caching
I had same problem before, as I remember filter is being applied every image resource referenced. You can use
icon.setColorFilter(null)
To reset your color filter on resource on your next usage. By this way any filter was applied before this image will be removed.

Disable a color tint in NavigationView only for specified icons

I need to disable a tint color for some icons in NavigationView because their color define category type. How can I do it?
Below picture shows my problem:
navview.setItemIconTintList(null);
Good luck!
If you want change color of icon on seletion the below is the possible answer:
Change Navigation View Item Color Dynamicly Android
Otherwise you can set
navview.setItemIconTintList(null);
this will give the original colors of icons. and you can use colored and grey icons as per your requirements.
For those who are using Kotlin, That's how it is done
val bottomNavigationView: BottomNavigationView = findViewById(R.id.bottomnavigationhome)
// * THIS ONE
bottomNavigationView.itemIconTintList = null
In case this is still relevant for somebody, we found a solution for a similar issue recently.
Although it is not possible (at least on API levels < 26) to set a custom tint list on individual items, you can set the tint mode individually. This worked for us:
val itemsWithoutTint: List<Int> = listOf(12345)
for (i in 0 until getMenu().size()) {
val item = getMenu().getItem(i)
if (item.itemId in itemsWithoutTint) {
MenuItemCompat.setIconTintMode(item, PorterDuff.Mode.DST)
}
}
By setting the TintMode to DST (https://developer.android.com/reference/android/graphics/PorterDuff.Mode), the source (in this case the tint color) is ignored and the destination (the icon to be tinted) is left untouched.
Menu menuNav = navigationView.getMenu();
MenuItem menuItem = menuNav.findItem(R.id.nav_subjects);
// Disable a tint color
menuItem.setChecked(false);
I hope it answers your question.

Verify if a background is equal to another for Edit Text in Android

In my project I have to verify if a backround is equal to another for an edit text (the edit text has a custom drawable). I have tried this code but it's not working:
if (editText.getBackground().equals(getResources().getDrawable(R.drawable.edit_text_box_red)))
{
editText.setBackgroundResource(R.drawable.edit_text_box_white);
}
I need this, because when I press a button and my editText background is red I have to make it white, to it's previous state. How can I do this?
I think it would make sense to store the background information when you change the background in the first place. One way to do this would be setTag(). For example, when you change the view's background to red, you also perform editText.setTag("red"). Then later, you can do
if (editText.getTag().equals("red")) {
editText.setBackgroundResource(R.drawable.edit_text_box_white);
editText.setTag("white");
}
How about "Flagging"?
Its like you create a static bool red = false
Then when you change the color, change the red value to true
In the selection :
if(red == true){
editText.setBackgroundResource(R.drawable.edit_text_box_white);
red = false;}
Hope this help :D

Categories

Resources