Change color of an image in android - android

I wonder if there is a way to change the color of my image on the button easily,
my simple example is like this,
CircleButton.setImageResource(android.R.drawable.ic_media_play);
I have a play audio icon (white) and want to change the icon color to red, like this
CircleButton.setImageResource(ColorChange(Red, android.R.drawable.ic_media_play));
What seems to be the easiest way for doing ColorChange() ?

Use ColorFilter to change the button image color. Your button should be ImageButton
Set the image resource to the ImageButton
CircleButton.setImageResource(android.R.drawable.ic_media_play);
And, change the image color when required like this
CircleButton.setColorFilter(getResources().getColor(R.color.any_color));
Refer : http://developer.android.com/reference/android/graphics/drawable/Drawable.html#setColorFilter(android.graphics.ColorFilter)

Related

Fill the transparent part of the drawable image a specific color

Currently, I have this back arrow that I'm going to use in my toolbar
What I want is to fill the middle part which a specific color which is the middle part is a transparent part.
currently what i have is this :
Drawable backArrow = getResources().getDrawable(R.drawable.ic_back);
backArrow.setColorFilter(getResources().getColor(R.color.aub_red), PorterDuff.Mode.SRC_ATOP);
getSupportActionBar().setHomeAsUpIndicator(backArrow);
it only changes the color of the white color part into the red color which is wrong. what I want to fill the transparent part a color not the non - transparent part.
Used this
Drawable backArrow = getResources().getDrawable(R.drawable.ic_back);
backArrow.setColorFilter(getResources().getColor(R.color.colorAccent), PorterDuff.Mode.LIGHTEN);
getSupportActionBar().setHomeAsUpIndicator(backArrow);
for more information click hear
you can use :
DrawableCompat.setTint(yourdrawable,getResources().getColor(R.color.primary));
in java code, or in xml tag :
android:backgroundTint="#color/primary"

How to clear color for TopDrawable of Textview?

In my application I change color of Top drawable of Textview using below code.
tvTopDrawable.setColorFilter("#E1BEE7", Mode.SRC_ATOP);
Now I want to clear this color from my drawable.
I have tried tvTopDrawable.clearColorFilter();
But it seems its not working sometime.
I used that same image in other Activity of my Application.
Where to write clearColorFilter() so it clears the color from drawable,Can anyone help with this?
Thanks in Advance.
Try setting color filter to transparent
tvTopDrawable.setColorFilter("#00E1BEE7", Mode.SRC_ATOP);

CardView default background color problematic

The default white background of CardView is problematic or am I missing something? When I fill the CardView with normal unstyled Android UI the white text of TextView is not readable e.g.
Has someone an idea what a good fix for that would be? I use the default "Theme.AppCompat" theme and the other background colors look correct. Is that a missing attribute in the Theme.AppCompat? Or am I doing something wrong? The default colors without setting any values manually should be always working or not?
Edit:
I now apply the default background color for the current style to the cardview like this:
TypedArray array = context.getTheme().obtainStyledAttributes(
new int[] { android.R.attr.colorBackground });
card.setCardBackgroundColor(array.getColor(0, 0xFF00FF));
I think its a quite save "default" fix to have at least no text color problems like in the screenshot but the question remains what should be the best practice here and why the cardview has alsways white as the default background color no matter what theme is used..
By this way you can change Cardview background color,
RoundRectDrawable backgroundDrawable = new RoundRectDrawable(backgroundColor, cardView.getRadius());
cardView.setBackgroundDrawable(backgroundDrawable);

android button green

i would like to have tha default button but not in gray.
I need to change the color green/blue/red.
I would like to do this thanks to xml but also from the code.
I need to have a green button which change the color of all the other button.
It's why i need to change from xml and from the code.
Define you colors in the color resource so it is easier to apply your desired colors via code or xml.

What's the use of ImageButton

When a background can be set on a Button then what is the use of an ImageButton?
With ImageButton (just like with Image) it is possible to control the way image defined by android:src is shown on the button: you can set scaleType - something you cannot do to the background.

Categories

Resources