Toast in Android Jetpack Compose
In Android, a Toast is a message or a pop-up message that generally appears at the bottom of the screen for a short span. A Toast is used to deliver simple feedback about any function or operation the application is running on the device. In simpler words, it displays the status of any running or finished task.
So in this article, we will show you how you could create a Toast message and display it in Android using Jetpack Compose. Follow the below steps once the IDE is ready.
Step by Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. While choosing the template, select Empty Compose Activity. If you do not find this template, try upgrading the Android Studio to the latest version. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.
Step 2: Working with the MainActivity.kt file
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
package com.w3wiki.toast import android.content.Context import android.os.Bundle import android.widget.Toast import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.material.* import androidx.compose.runtime.Composable import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.colorResource import androidx.compose.ui.tooling.preview.Preview class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super .onCreate(savedInstanceState) setContent { // Calling the function // to display the content GetScaffold() } } } // Function to generate a Toast private fun mToast(context: Context){ Toast.makeText(context, "This is a Sample Toast" , Toast.LENGTH_LONG).show() } // Creating a function for creating a Top Bar. // Creating and Setting a function SomeContent() // to display a button (below this function) @Composable fun GetScaffold(){ Scaffold( topBar = { TopAppBar( title = {Text( "GFG | SampleToast" , color = Color.White)}, backgroundColor = Color( 0XFF0F9D58 ) ) }, content = { SomeContent()} ) } // Creating a function // to display a Button. @Composable fun SomeContent(){ // Fetching the local context for using the Toast val mContext = LocalContext.current // Creating a Box layout to display a Button Box(Modifier.fillMaxSize(), Alignment.Center) { // Creating a Button and calling the // Toast function when clicked Button(onClick = { mToast(mContext) }, colors = ButtonDefaults.buttonColors(backgroundColor = Color( 0XFF0F9D58 ))) { Text(text = "Click" , color = Color.White) } } } // Displaying the application // preview in the Android Studio @Preview (showBackground = true ) @Composable fun DefaultPreview() { GetScaffold() } |
Output:
In the below video, you can see that when the Button is clicked, a Toast is displayed.
Contact Us