1

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.

Community
  • 1
  • 1
Gene Bo
  • 11,284
  • 8
  • 90
  • 137

2 Answers2

3

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

Community
  • 1
  • 1
HenriqueMS
  • 3,864
  • 2
  • 30
  • 39
  • @gnB have you been able to solve your problem? I believe this answer should be marked as correct , let me know if this helped =) – HenriqueMS Oct 17 '16 at 18:31
  • 1
    This solution setting the color in Java worked as well as using `android:tint`. As with the other approach, I had to clean/refresh the project to get it to work. Thank you for the pointer and details – Gene Bo Oct 19 '16 at 03:46
  • @gnB my pleasure! – HenriqueMS Oct 19 '16 at 10:56
1

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"/>
MohanadMohie
  • 1,033
  • 1
  • 10
  • 17