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:
But it doesn't tint when deployed on device:
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.
For anyone else that may come across this question. This is what I did.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With