2
  • I have displayed a snackbar with specific action in Jetpack compose but click listener on that action is not working.

Here is my code:

@Composable fun MySnackBar() { Snackbar( modifier = Modifier.padding(4.dp), action = { TextButton(color = LightBlue,onClick = { Log.d("TAG", "Action clicked!") }) { Text(text = "Remove") } } ) { Text(text = "This is a basic Snackbar with action item") } } 

2 Answers 2

3

In a Scaffold you can use:

 val scope = rememberCoroutineScope() scope.launch { val snackbarResult = scaffoldState.snackbarHostState.showSnackbar( message = "This is a basic Snackbar with action item", actionLabel = "Remove" ) when (snackbarResult) { SnackbarResult.Dismissed -> {} SnackbarResult.ActionPerformed -> {} } } 

enter image description here

If you don't have a Scaffold you can use something like:

 val snackState = remember { SnackbarHostState() } SnackbarHost(hostState = snackState, Modifier){ data -> Snackbar( actionColor = Red, snackbarData = data ) } 

and then use the same code above to show the Snackbar changing scaffoldState.snackbarHostState to snackState.

Sign up to request clarification or add additional context in comments.

6 Comments

I cannot use this implementation because I need custom color action button of snackbar !
@AndroidDev why not? Just check stackoverflow.com/questions/69734835/…
Yes I have checked this link but due to usage of Snackbarhost at other place I cannot use this because I do not have scaffold present in current file. I am showing snackbar at the bottom of the screen using Box alignment property.
@AndroidDev Just updated the answer. The code is pretty the same.
Still action click listener is not working!
|
0

Button( onClick = { Snackbar(action = {}) { Text("hello") } }`

2 Comments

Snackbar is visible but it's action click is not working!!!!
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.