26

No longer relevant! This issue is related to a bug in old Android 4.x build. android:tint should now work correctly as in the example below

I'm trying to apply tint to bitmap inside < layer-list >

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <shape  xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
            <solid android:color="@color/grey" />
            <size android:width="45dp" android:height="45dp"/>
        </shape>
    </item>
    <item android:left="5dp" android:right="5dp" android:top="5dp" android:bottom="5dp">
        <bitmap android:src="@drawable/ic_action" android:tint="#FF000000"   />
    </item>
</layer-list>

Preview shows that it should work, FROM ANDROID-STUDIO: enter image description here

But it doesn't tint when deployed on device: enter image description here

It does tint correctly if i use ImageView in my layout, but fails with layer-list. I believe I have tried every tintMode with no results.

somerandomusername
  • 1,993
  • 4
  • 23
  • 55
  • 2
    any progress on this? would be great. – user291701 Nov 02 '14 at 04:37
  • Sorry, I got nothing new on this. I presume you have the same issue? For the moment I just avoid using layer-list with bitmaps. – somerandomusername Nov 03 '14 at 14:32
  • 1
    I submitted this to google: https://code.google.com/p/android/issues/detail?id=78631&thanks=78631&ts=1415028902 – somerandomusername Nov 03 '14 at 15:36
  • 1
    Issue has been resolved in master branch for future release. – alanv Nov 03 '14 at 20:57
  • I tried simplifying it by using just the bitmap as the root view of the drawable resource. But the issue persists, indicating that it is not a layer-list issue but an issue with the bitmap itself. Also I'd like to point out that `setTint()` only with lollipop and maybe it has something to do with that. Testing the app on a lollipop device could help rule that out. – reubenjohn Jun 25 '15 at 20:08
  • 1
    I’m voting to close this question because the OP stated himself that it is no longer relevant and was caused by a bug in Android which is long fixed in the meantime. – Ridcully Dec 07 '21 at 07:00
  • Agree. Put my vote in. – somerandomusername Dec 08 '21 at 10:36

3 Answers3

17

For anyone else that may come across this question, this is what I did:

Setup

  • Android Studio 2.3.2

Gradle

  • minSdkVersion 15
  • targetSdkVersion 25
  • compile 'com.android.support:appcompat-v7:25.3.1'

Layer Drawable I added android:id=@+id/bitmapID to the item containing the bitmap

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <shape android:shape="oval">
            <solid android:color="#9e9e9e" />
            <size android:width="45dp" android:height="45dp"/>
        </shape>
    </item>
    <item
        android:id="@+id/tintDrawableImg"
        android:left="5dp"
        android:right="5dp"
        android:top="5dp"
        android:bottom="5dp">
        <bitmap android:src="@mipmap/ic_pencil" android:tint="#860000"   />
    </item>
</layer-list>

Activity Layout I added the layer drawable to an ImageView

<ImageView
    android:id="@+id/tintLayerTest"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:src="@drawable/test_layer"/>

MainActivity In the onCreate() method we can locate the bitmap from the layer drawable using findViewByID

public class MainActivity extends AppCompatActivity {

    ImageView iv;
    LayerDrawable ld;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Declare ImageView containing LayerDrawable
        iv = (ImageView)findViewById(R.id.tintLayerTest);
        //Get LayerDrawable from ImageView
        ld = (LayerDrawable)iv.getDrawable();
        //Get specific Drawable/Bitmap from within LayerDrawable
        //by ID and pass it as an independent Drawable
        Drawable ldDrawable = ld.findDrawableByLayerId(R.id.tintDrawableImg);
        
        //Pass your new Drawable to DrawableCompat.setTint
        //and define your color int
        DrawableCompat.setTint(ldDrawable, ContextCompat.getColor(this, R.color.colorAccent));

    }
}

I hope this helps others that come across this question.

RonU
  • 5,525
  • 3
  • 16
  • 13
Steve C.
  • 1,333
  • 3
  • 19
  • 50
  • 1
    Is this still relevant ? They (supposedly) fixed this bug like 3 years ago. – somerandomusername Jun 05 '17 at 09:14
  • I had the same issue recently trying to do similar. This is what solved it for me. – Steve C. Jun 05 '17 at 09:47
  • Note that this solution doesn't work if there are several layer-list drawables to display with a different tint on the same page: The Android drawable caches uses the same drawable instance when inflating the same resource, therefore manually setting its tint is setting the same tint for all the same drawable displayed on the page. – Nit Jul 27 '17 at 10:00
  • @Nit But you would still be able to define each layer but id right? I haven't used more than 2, that's why I ask – Steve C. Jul 27 '17 at 10:03
  • @SteveC. Yes, no problem with each layer id. The issue is the drawable retrieved from `ImageView.getDrawable()` being the same between several ImageViews. Therefore, settings the tint on one of the ImageView also changes the tint of the other. – Nit Jul 27 '17 at 10:16
  • Using getDrawable on the view worked! I was using reosurces.getDrawable before. – anoo_radha Jul 02 '18 at 14:16
  • 1
    @somerandomusername It works with just `android:tint` in 2019! No hacks needed anymore – lucidbrot Mar 25 '19 at 15:38
1

Try with xml drawable using bitmap tag

Save the following file xml custom_image.xml & replace your icon & tint color

<?xml version="1.0" encoding="utf-8"?>
    <bitmap
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:src="@drawable/iv_veg"
      android:tint="@color/red">
 </bitmap>
Ranjithkumar
  • 16,071
  • 12
  • 120
  • 159
1

As it was mentioned by @lucidbrot in the comments, android:tint should work now without any hacks.

fobo66
  • 410
  • 7
  • 20