https://kotlinlang.org logo
#compose
Title
# compose
k

KotlinLeaner

08/02/2022, 1:44 PM
Hey guys, I asked this stack overflow but didn't get the answer. I am working on ripple effect in jetpack compose. I provide my color and after clicking on view it show some time correct color after that different type of color showing like dark grey on press state.
Copy code
binding.itemComposable.setContent {
            Column(modifier = Modifier.fillMaxSize(), verticalArrangement = Arrangement.spacedBy(12.dp)) {
                val options = getOptions()
                options.forEachIndexed { _, optionText ->
                    val interactionSource = remember { MutableInteractionSource() }
                    val isPressed by interactionSource.collectIsPressedAsState()
                    val backgroundColor = if (isPressed) DuckEggBlue else OffWhite
                    val textColor = if (isPressed) TealBlue else Slate
                    val borderWidth = if (isPressed) 1.dp else 0.dp
                    val borderColor = if (isPressed) Aqua else OffWhite
                    Surface(
                        onClick = {
                            logE("Item Click")
                        },
                        shape = RoundedCornerShape(4.dp),
                        border = BorderStroke(borderWidth, borderColor),
                        interactionSource = interactionSource
                    ) {
                        Text(
                            modifier = Modifier
                                .fillMaxWidth()
                                .background(backgroundColor)
                                .padding(16.dp),
                            text = optionText,
                            style = Typography.h3,
                            fontWeight = FontWeight.Medium,
                            color = textColor
                        )
                    }
                }
            }
        }
I cannot add image, instead i added my youtube video. Please have a look.
Copy code
val DuckEggBlue = Color(0xFFF0FCFC)
This is my color which I am using.
tried from this doc
Copy code
@Immutable
private object SecondaryRippleTheme : RippleTheme {
    @Composable
    override fun defaultColor() = RippleTheme.defaultRippleColor(
        contentColor = DuckEggBlue,
        lightTheme = true
    )

    @Composable
    override fun rippleAlpha() = RippleTheme.defaultRippleAlpha(
        contentColor = DuckEggBlue,
        lightTheme = true
    )
}
In my code
Copy code
CompositionLocalProvider(LocalRippleTheme provides SecondaryRippleTheme) {
            Text(
                modifier = Modifier
                    .fillMaxWidth()
                    .background(OffWhite)
                    .padding(16.dp),
                text = optionText,
                style = Typography.h3,
                fontWeight = FontWeight.Medium,
                color = textColor
            )
    }
but nothing works. It still grey ripple effect.
o

oianmol

08/02/2022, 1:49 PM
you need to work with Indication, so there's something called rememberRipple() which takes in the ripple color also, ideally it would take the colors from the default ripple theme! @KotlinLeaner
If you see the IMPL for MaterialRippleTheme
Copy code
@Composable
    override fun defaultColor() = RippleTheme.defaultRippleColor(
        contentColor = LocalContentColor.current,
        lightTheme = MaterialTheme.colors.isLight
    )

    @Composable
    override fun rippleAlpha() = RippleTheme.defaultRippleAlpha(
        contentColor = LocalContentColor.current,
        lightTheme = MaterialTheme.colors.isLight
    )
It takes in the contentColor!
k

KotlinLeaner

08/02/2022, 1:51 PM
I tried this in my surface but nothing works
Copy code
Surface(
    onClick = {
        println("Item Click")
    },
    shape = RoundedCornerShape(4.dp),
    border = BorderStroke(borderWidth, borderColor),
    interactionSource = interactionSource
    indication = rememberRipple(bounded = false),
)
@oianmol I am doing correct?
but still don't work..
o

oianmol

08/02/2022, 1:53 PM
Looks like this constructor is deprecated, the one which takes in an indication.
the rememberRipple() also takes in a color
can you give it color by yourself ?
k

KotlinLeaner

08/02/2022, 1:54 PM
ok trying now
o

oianmol

08/02/2022, 1:54 PM
Copy code
@Composable
public fun rememberRipple(
    bounded: Boolean = true,
    radius: Dp = Dp.Unspecified,
    color: Color = Color.Unspecified
)
k

KotlinLeaner

08/02/2022, 1:55 PM
building my application
o

oianmol

08/02/2022, 1:55 PM
Is the code committed somewhere ?
k

KotlinLeaner

08/02/2022, 1:56 PM
No it's my college private repository.
I'll make a copy
o

oianmol

08/02/2022, 1:56 PM
Also ideally you don't need to do anything special for a color, it depends on the contentColor which is calculated from
Copy code
fun Colors.contentColorFor(backgroundColor: Color): Color {
    return when (backgroundColor) {
        primary -> onPrimary
        primaryVariant -> onPrimary
        secondary -> onSecondary
        secondaryVariant -> onSecondary
        background -> onBackground
        surface -> onSurface
        error -> onError
        else -> Color.Unspecified
    }
}
k

KotlinLeaner

08/02/2022, 1:56 PM
and share with you
give me few min
o

oianmol

08/02/2022, 1:58 PM
also you don't need to give your
interactionSource
since Surface makes one for you
Also is this Compose Desktop or Mobile ? 🤔
k

KotlinLeaner

08/02/2022, 1:59 PM
It's moblie
@oianmol this is github repo.
o

oianmol

08/02/2022, 2:11 PM
Copy code
rememberRipple(bounded = true, color = TealBlue)
k

KotlinLeaner

08/02/2022, 2:11 PM
yes it working now. what is the use of
bounded = true
?
o

oianmol

08/02/2022, 2:19 PM
It has something to do with the origin for the ripple
Copy code
internal fun Density.getRippleEndRadius(bounded: Boolean, size: Size): Float {
    val radiusCoveringBounds =
        (Offset(size.width, size.height).getDistance() / 2f)
    return if (bounded) {
        radiusCoveringBounds + BoundedRippleExtraRadius.toPx()
    } else {
        radiusCoveringBounds
    }
}
k

KotlinLeaner

08/02/2022, 2:20 PM
okk now I got it.
I really appreciate it.
o

oianmol

08/02/2022, 2:21 PM
Anytime bro!
7 Views