Gene Bo
Gene Bo

Reputation: 12073

Can provided Android SDK icons be set with color?

Android SDK provides the following icons.

Is there a way to set a color to those .. and if possible, how to do so?

    <ImageView
        android:id="@+id/share_button"
        style="@style/IconNav"
        android:src="@android:drawable/ic_menu_share"/>

    <ImageView
        android:id="@+id/bookmark_button"
        style="@style/IconNav"
        android:src="@android:drawable/ic_input_get"/>

enter image description here

UPDATE

After doing a complete refresh of the project, it turns out the tint attribute in Xml did the trick.

For the short answer

.. this is the solution that worked for me - adding the property to the ImageView xml:

    android:tint="@color/grass_dark"

The answer from @goldenb is a thorough run through of the different ways to solve for this, so am marking that one as the answer.

Upvotes: 1

Views: 353

Answers (2)

HenriqueMS
HenriqueMS

Reputation: 3974

You can indeed use a tint as a way of changing an ImageView's colour, BUT you should be reminded that the android:tint will always be applied on top of the original colour.

as stated by blogger danlew

  • ImageView's tint mixes the tint color with the original asset. What you want is for the tint color to take over entirely; instead it applies the tint on top of the existing color. So, for example, if the source asset is black, and you want it to be #77FFFFFF (a translucent shade of white), you'll actually end up getting that shade of white with a black background beneath it.

  • android:tint is limited to ImageView. You want to be able to tint any Drawable in any View.

One possible alternative would be for you to use android ColorFilter

According to the official documentation:

A color filter can be used with a Paint to modify the color of each pixel drawn with that paint. This is an abstract class that should never be used directly.

There are lots of more or less complex things you can do with ColorFilter but how can you apply this then?

One simple example from another so question is:

//White tint
imageView.setColorFilter(Color.argb(255, 255, 255, 255)); 

or

imageView.setColorFilter(ContextCompat.getColor(context,R.color.COLOR_YOUR_COLOR))

Or a more complete answer here in SO from here

ImageView redCircle = (ImageView) findViewById(R.id.circle_red_imageview);
ImageView greenCircle = (ImageView) findViewById(R.id.circle_green_imageview);
ImageView blueCircle = (ImageView) findViewById(R.id.circle_blue_imageview);



// we can create the color values in different ways:
redCircle.getDrawable().setColorFilter(Color.RED, PorterDuff.Mode.MULTIPLY );
greenCircle.getDrawable().setColorFilter(0xff00ff00, PorterDuff.Mode.MULTIPLY );
blueCircle.getDrawable().setColorFilter(getResources().getColor(R.color.blue), PorterDuff.Mode.MULTIPLY );

You should check these links if you want to learn more

SO - What is the difference between background, backgroundTint, backgroundTintMode attributes in android layout xml?

setColorFilter()

Fast Android asset theming with ColorFilter

SO-Modifying the color of an android drawable

Upvotes: 3

MohanadMohie
MohanadMohie

Reputation: 1044

You can use a bitmap with a tint. Add this to your drawables folder.

ic_input_get_colored.xml :

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
        android:src="@android:drawable/ic_input_get"
        android:tint="@color/yourDesiredColor"/>

Upvotes: 1

Related Questions