Instantly share code, notes, and snippets.

Embed
What would you like to do?
"Recolouring / modifying multi-layer drawables dynamically in Android" for blog.jakelee.co.uk
// Changing colour
val layerDrawable = (ImageView)findViewById(R.id.my_drawable).drawable as LayerDrawable
val backgroundDrawable = layerDrawable.findDrawableByLayerId(R.id.background_circle) as GradientDrawable
backgroundDrawable.setColor(ContextCompat.getColor(context, R.color.my_colour))
// Changing drawable
layerDrawable.setDrawableByLayerId(R.id.foreground_icon, ContextCompat.getDrawable(context, R.drawable.new_foreground_icon))
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/background_circle">
<shape android:shape="oval">
<solid android:color="@color/orange" />
</shape>
</item>
<item android:id="@+id/foreground_icon">
<shape>
<solid android:color="@android:color/transparent" />
</shape>
</item>
</layer-list>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment