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

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)
ic_format_color_text.xml
<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" />
</layer-list>
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 */) }

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