Hi, I try to implement color selector on the butto...
# compose
j
Hi, I try to implement color selector on the button background. Any API I can use so far? The first thought I can use is
PressIndicatorGestureDetector
, like
Ripple
to customize one.
Copy code
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:color="#ffff0000"/> <!-- pressed -->
    <item android:state_focused="true"
          android:color="#ff0000ff"/> <!-- focused -->
    <item android:color="#ff000000"/> <!-- default -->
</selector>
This is what I implemented. But it seems like
PressIndicatorGestureDetector
will consume the down events. It cannot combine it with
Ripple
. I cannot figure out a better way to fix this problem.
Copy code
@Composable
fun Selector(
    enabled: Boolean = true,
    children: @Composable() (active: Boolean) -> Unit
) {
    var active by state { false }

    PressIndicatorGestureDetector(
        onStart = { active = true },
        onStop = { active = false },
        onCancel = { active = false },
        enabled = enabled,
        children = { children(active) }
    )
}
i
Will
Clickable(onClick = { state = !state }) { children(active) }
work here?
j
Clickable
only triggers when user released the finger not press the finger. This is not what I expected.
👍 1