Prerequisites:
Snackbar is a lightweight widget and they are used to show messages at the bottom of the application. It was introduced with the Material Design library as a replacement for a Toast. In this article, we will explain how you can create a Snackbar using Jetpack Compose. Below is the sample picture to show what we are going to build.

Step by Step Implementation
Step 1: Create a new project
To create a new project in Android Studio using Jetpack Compose please refer to How to Create a New Project in Android Studio Canary Version with Jetpack Compose.
Step 2: Working with MainActivity.kt
Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
Kotlin
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super .onCreate(savedInstanceState)
setContent {
JetpackComposePracticeTheme {
Surface(color = Color.White) {
Column(modifier = Modifier.padding( 10 .dp)) {
SnackBars()
}
}
}
}
}
}
@Preview
@Composable
fun SnackBars() {
Text(text = "Snackbars" , style = typography.h6, modifier = Modifier.padding( 8 .dp))
Snackbar(modifier = Modifier.padding( 4 .dp)) {
Text(text = "This is a basic snackbar" )
}
Snackbar(
modifier = Modifier.padding( 4 .dp),
action = {
TextButton(onClick = {}) {
Text(text = "Remove" )
}
}
) {
Text(text = "This is a basic Snackbar with action item" )
}
Snackbar(
modifier = Modifier.padding( 4 .dp),
actionOnNewLine = true ,
action = {
TextButton(onClick = {}) {
Text(text = "Remove" )
}
}
) {
Text(text = "Snackbar with action item below text" )
}
}
|
Output:

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
26 Apr, 2021
Like Article
Save Article