2

AFAIK, the buttons created with Jetpack compose should have ripple effect on clicking, by default. But my button doesn't show ripple effect on clicking. Below is code for my button:

 BoxWithConstraints(modifier = Modifier
                .constrainAs(button) {
                    top.linkTo(glTopButtonProceed)
                    start.linkTo(glLeftBtn)
                    end.linkTo(glRightBtn)
                    width = Dimension.fillToConstraints

                }) {

                Button(
                    onClick = {
                        navController.navigate("autosave_screen")
                    },
                    modifier = Modifier
                        .fillMaxWidth()
                        .height(if (screenHeight>=700.dp)
                            50.dp
                        else
                            with(LocalDensity.current) {dimensionResource(id = R.dimen._35sdp)}) ,



                    colors = if (text.text != "" && text.text.length == 4)
                        ButtonDefaults.buttonColors(backgroundColor = colorResource(id = R.color.bright_green))
                    else
                        ButtonDefaults.buttonColors(backgroundColor = colorResource(id = R.color.gray))

                ) {
                    Text(
                        "Proceed", color = colorResource(id = R.color.dark_blue),
                        fontSize =
                        if (screenHeight>=700.dp)
                            19.sp
                        else
                            with(LocalDensity.current) {dimensionResource(id = R.dimen._12sdp).toSp()},
                        fontFamily = FontFamily(Font(R.font.poppins_medium)),

                        textAlign = TextAlign.Center,
                        modifier = Modifier.align(Alignment.CenterVertically)
                    )

                }

            }
        }

How do I enable ripple effect for my button?

Sparsh Dutta
  • 2,450
  • 4
  • 27
  • 54
  • See I am almost certain that it is because of the custom background that you are setting. The built-in ripple might not be able to infer the colour it should produce. – Richard Onslow Roper Jun 21 '21 at 10:51
  • Does it happen when it is enabled = false? – Gabriele Mariotti Jun 21 '21 at 10:55
  • The ripple effect appears when the background of button is gray, not when it is bright green. I think MARSK you are right. – Sparsh Dutta Jun 21 '21 at 10:57
  • Gabriele, ripple doesn't appear for bright green background both when enabled is true and false. – Sparsh Dutta Jun 21 '21 at 10:58
  • @SparshDutta it is quite strange. when the background is gray the button should be disabled. – Gabriele Mariotti Jun 21 '21 at 12:05
  • Gabriele, I haven't used the 'enabled' parameter. My bad, I should have removed this line "enabled = (text.text != "" && text.text.length == 4)", from question. Updating the question. – Sparsh Dutta Jun 21 '21 at 12:09
  • Also, I tried removing all background colors from the button. Ripple effect seems to appear only when the button is 'non-functional', i.e doesn't meet the criteria to go to next screen. When the button is 'functional', i.e clicking on it goes to next screen, ripple effect doesn't appear. – Sparsh Dutta Jun 21 '21 at 12:13
  • Is this the default behaviour of Jetpack compose? – Sparsh Dutta Jun 21 '21 at 12:14
  • @Marsk and Gabriele, The ripple effect is produced when the color of button is gray, but not when the color of button is bright green. Is there any way to produce ripple effect even with bright green color of button? – Sparsh Dutta Aug 13 '21 at 07:40

3 Answers3

1

Instead of calling onClick on Button can you try to use click on Modifier click

modifier = Modifier .clickable(
 interactionSource = remember { MutableInteractionSource() },
    indication = rememberRipple(bounded = false),
    onClick = {}
        )

You can customise the button ripple also.

Rajshekhar
  • 571
  • 5
  • 9
0

Wrap your Box with Material Theme to get nice ripple effect

Dharman
  • 30,962
  • 25
  • 85
  • 135
0

If you want to impart specific colors to your button, try instead using Modifier.fillMaxSize().background(...) on theText Composable instead. Don't use that colors parameter of Button.

Richard Onslow Roper
  • 5,477
  • 2
  • 11
  • 42
  • Didn't work. 2 problems : 1. Text composable inside button didn't cover the entire button, there was still some thick border surrounding the Text composable in button. 2. Ripple effect happened only in the surrounding thick border – Sparsh Dutta Aug 13 '21 at 09:01