[SOLVED] How to achieve partially color changing of drawable?

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under
CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.


I am changing my app’s design infrastructure to Material Design Components and would like to achieve something like this:

src=”https://i.stack.imgur.com/9S9Z6.png” alt=”enter image description here” />

This is from toggle button and it shows that a drawable can be colored partially, but there is no implementation example how to achieve this. In my app I have a text formatting toolbar with foreground color and I need to get shown the selected text color exactly in that way.


You can split icon into 2 parts (same sized), for example we can take “format color text” icon from material icons


and then simple combine two drawables into one using <layer-list> (LayerDrawable)


<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/ic_format_color_text_black_part1_24dp" />
    <item android:drawable="@drawable/ic_format_color_text_black_part2_24dp" />

Now, this drawable can be used as an icon for your button (MaterialButtonToggleGroup + MaterialButton). To tint “bottom shape” part only:

val dr = (colorTextBtn.icon as? LayerDrawable)?.getDrawable(0 /* colored shape layer index*/)
dr?.let { DrawableCompat.setTint(dr, /* color */) }

enter image description here

Answered By – Akaki Kapanadze

people found this article helpful. What about you?