Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable ripple effect when clicking in Jetpack Compose

In Jetpack Compose, when you enable clickable {} on a modifier for a composable, by default it enables ripple effect for it. How to disable this behavior?

Example code

Row(modifier = Modifier          .clickable { // action } ) 
like image 556
ImMathan Avatar asked Mar 19 '21 06:03

ImMathan


People also ask

How to remove ripple effect Compose?

if you are using modifier clickable attribute on Column or any other component then you can use enabled attribute along with onClick to disable ripple effect when needed.

What is LazyColumn in jetpack compose?

A LazyColumn is a vertically scrolling list that only composes and lays out the currently visible items. It's similar to a Recyclerview in the classic Android View system.

What is mutableStateOf jetpack compose?

mutableStateOf creates an observable MutableState<T> , which is an observable type integrated with the compose runtime. Any changes to value will schedule recomposition of any composable functions that read value . In the case of ExpandingCard , whenever expanded changes, it causes ExpandingCard to be recomposed.


2 Answers

Short answer:
to disable the ripple pass null in the indication parameter in the clickable modifier:

val interactionSource = remember { MutableInteractionSource() } Column {     Text(         text = "Click me and my neighbour will indicate as well!",         modifier = Modifier             .clickable(                 interactionSource = interactionSource,                 indication = null             ) {                 /* .... */             }     ) 

Long answer:
If you add the clickable modifier to the element to make it clickable within its bounds it will show an Indication as specified in indication parameter.

By default, indication from LocalIndication will be used.

If you are using a MaterialTheme in your hierarchy, a Ripple will be used as the default Indication inside components such as androidx.compose.foundation.clickable and androidx.compose.foundation.indication.

like image 180
Gabriele Mariotti Avatar answered Sep 28 '22 02:09

Gabriele Mariotti


Use this Modifier extension:

inline fun Modifier.noRippleClickable(crossinline onClick: ()->Unit): Modifier = composed {     clickable(indication = null,         interactionSource = remember { MutableInteractionSource() }) {         onClick()     } } 

then simply replace Modifier.clickable {} with Modifier.noRippleClickable {}

Row(modifier = Modifier.noRippleClickable {    // action  }) 
like image 33
Mahdi-Malv Avatar answered Sep 28 '22 02:09

Mahdi-Malv