One Off the Slack: What Replaces ColorStateList?

Eric Schrag asked:

I’m trying to figure out how to replicate some of the old drawable state list background color functionality, where the background color changed on press (with no ripple), and really struggling. any resources on how to accomplish this?

i just want to replicate the old state list behavior. if button is pressed, button background color is blue, otherwise it’s teal. no ripple at all.

Colton Idle offered:

Can’t you just switch on state and change the color accordingly?

Like background = if (PRESSED) Color.Blue else Color.Red

However, Eric pointed out that ButtonColors does not offer arbitrary states, just enabled.

Louis suggested forking Button(), which is going to be a typical design system solution:

At this point it is probably easier just to build your own button, since ripple is a core part of Material components.

Zach Klippenstein offered up the eventual solution:

You probably want to implement your own Indication. https://developer.android.com/reference/kotlin/androidx/compose/foundation/Indication

That in turn could be passed through the LocalIndication portion of the CompositionLocal system, so Button() or other interactive elements in some branch of the composition tree can take that Indication into account. Or, you can apply it to an individual Button() if you prefer, as Eric did:

I figured out that my issue was simply that I wasn’t connecting my interactionSource to the button with interactionSource = interactionSource, in the Composable call


Read the original thread in the kotlinlang Slack workspace. Not a member? Join that Slack workspace here!