Open In App

Custom Toast in Android using Jetpack Compose

Improve
Improve
Like Article
Like
Save
Share
Report

Toast messages are displayed within the android application to display several messages to the user. Toast messages are feedback messages which are displayed to the users on performing some action within the android application. In this article, we will look at How to add Custom styled toast in android using Jetpack Compose

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: Adding a new color in the Color.kt file

Navigate to the app > java > your app’s package name > ui.theme > Color.kt file and add the below code to it. 

Kotlin




package com.example.newcanaryproject.ui.theme
  
import androidx.compose.ui.graphics.Color
  
val Purple200 = Color(0xFF0F9D58)
val Purple500 = Color(0xFF0F9D58)
val Purple700 = Color(0xFF3700B3)
val Teal200 = Color(0xFF03DAC5)
  
// on below line we are adding different colors.
val greenColor = Color(0xFF0F9D58)


Step 3: Adding an image to the drawable folder

Copy the image which you want to add to your toast message. Navigate to app > res > drawable. Right-click on it and paste all the images into the drawable folder.

Step 4: Adding dependency in build.gradle file

Navigate to Gradle Scripts > build.gradle file and add the below dependency in the dependencies section. 

implementation 'com.github.GrenderG:Toasty:1.5.2'

After adding this dependency simply sync your project to add it. 

Step 5: 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.example.newcanaryproject
  
import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.runtime.*
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.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.*
import com.example.newcanaryproject.ui.theme.*
import es.dmoral.toasty.Toasty
  
class MainActivity : ComponentActivity() {
  
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            NewCanaryProjectTheme {
                // on below line we are specifying 
                // background color for our application
                Surface(
                    // on below line we are specifying modifier and color for our app
                    modifier = Modifier.fillMaxSize(), color = MaterialTheme.colors.background
                ) {
  
                    // on below line we are specifying 
                    // theme as scaffold.
                    Scaffold(
  
                        // in scaffold we are 
                        // specifying top bar.
                        topBar = {
  
                            // inside top bar we are specifying background color.
                            TopAppBar(backgroundColor = greenColor,
  
                                // along with that we are specifying 
                                // title for our top bar.
                                title = {
  
                                    // in the top bar we are specifying 
                                    // tile as a text
                                    Text(
  
                                        // on below line we are specifying 
                                        // text to display in top app bar.
                                        text = "Custom Toast in Android",
  
                                        // on below line we are specifying modifier 
                                        // to fill max width.
                                        modifier = Modifier.fillMaxWidth(),
  
                                        // on below line we are specifying 
                                        // text alignment.
                                        textAlign = TextAlign.Center,
  
                                        // on below line we are specifying 
                                        // color for our text.
                                        color = Color.White
                                    )
                                })
                        }) {
                        // on below line we are calling custom toast 
                        // ui method to display ui for our custom toast
                        customToastUI()
                    }
                }
            }
        }
    }
}
  
// on below line we are creating a composable 
// function to display ui for custom toast
@Composable
fun customToastUI() {
    // on below line we are creating a 
    // variable for context
    val ctx = LocalContext.current
  
    // on below line we are 
    // creating a column for our ui.
    Column(
        // in this column we are adding a modifier 
        // for our column and specifying max width, 
        // height and size.
        modifier = Modifier
            .fillMaxWidth()
            .fillMaxHeight()
            .fillMaxSize()
  
            // on below line we are adding padding 
            // from all sides to our column.
            .padding(6.dp),
  
        // on below line we are adding vertical arrangement 
        // for our column as bottom
        verticalArrangement = Arrangement.Center,
  
        // on below line we are adding horizontal alignment 
        // for our column.
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        // on below line we are 
        // displaying a simple text
        Text(
  
            // on below line we are specifying modifier 
            // as padding for our text.
            modifier = Modifier.padding(6.dp),
  
            // on below line we are specifying 
            // text as normal image.
            text = "Custom Toast Message",
  
            // on below line we are specifying 
            // font weight as bold.
            fontWeight = FontWeight.Bold,
  
            // on below line we are 
            // setting color for our text
            color = greenColor,
  
            // on below line we are 
            // setting font size.
            fontSize = 20.sp
        )
  
        // on below line we are creating a simple 
        // spacer with height 20
        Spacer(modifier = Modifier.height(20.dp))
  
        // on below line we are creating a button 
        // for displaying error toast
        Button(
            // on below line we are adding width 
            // for our button and padding to it.
            modifier = Modifier
                .width(300.dp)
                .padding(7.dp),
  
            // in this button we are adding 
            // on click for it on below line.
            onClick = {
  
                // on below line we are displaying a custom 
                // toast message on below line for error toast
                Toasty.error(ctx, "This is an error toast.", Toast.LENGTH_SHORT, true).show()
  
            }) {
            // on below line we are 
            // specifying text for button.
            Text(text = "Error Toast")
        }
  
        // on below line we are creating a spacer of height 20
        Spacer(modifier = Modifier.height(20.dp))
  
        // on below line we are creating a button 
        // for displaying a toast
        Button(
            // on below line we are adding width for our 
            // button and padding to it.
            modifier = Modifier
                .width(300.dp)
                .padding(7.dp),
  
            // in this button we are adding on 
            // click for it on below line.
            onClick = {
  
                // on below line we are displaying a custom toast message on below line
                Toasty.success(ctx, "This is a Success toast.", Toast.LENGTH_SHORT, true).show()
  
            }) {
            // on below line we are 
            // specifying text for button.
            Text(text = "Success Toast")
        }
  
        // on below line we are creating a spacer of height 20
        Spacer(modifier = Modifier.height(20.dp))
  
        // on below line we are creating a 
        // button for displaying a toast
        Button(
            // on below line we are adding width 
            // for our button and padding to it.
            modifier = Modifier
                .width(300.dp)
                .padding(7.dp),
  
            // in this button we are adding 
            // on click for it on below line.
            onClick = {
  
                // on below line we are displaying a custom toast message on below line
                Toasty.info(ctx, "This is a Info toast.", Toast.LENGTH_SHORT, true).show()
  
            }) {
            // on below line we are specifying 
               // text for button.
            Text(text = "Info Toast")
        }
  
        // on below line we are creating a spacer of height 20
        Spacer(modifier = Modifier.height(20.dp))
  
        // on below line we are creating a 
        // button for displaying a toast
        Button(
            // on below line we are adding 
            // width for our button and padding to it.
            modifier = Modifier
                .width(300.dp)
                .padding(7.dp),
  
            // in this button we are adding on click for it on below line.
            onClick = {
  
                // on below line we are displaying a custom toast message on below line
                Toasty.warning(ctx, "This is a Warning toast.", Toast.LENGTH_SHORT, true).show()
  
            }) {
            // on below line we are specifying text for button.
            Text(text = "Warning Toast")
        }
  
        // on below line we are creating a spacer of height 20
        Spacer(modifier = Modifier.height(20.dp))
  
        // on below line we are creating a 
        // button for displaying a toast
        Button(
            // on below line we are adding width for our 
            // button and padding to it.
            modifier = Modifier
                .width(300.dp)
                .padding(7.dp),
  
            // in this button we are adding on 
            // click for it on below line.
            onClick = {
  
                // on below line we are displaying a custom toast 
                // message on below line
                Toasty.normal(ctx, "This is a Normal toast.").show()
  
            }) {
            // on below line we are specifying 
            // text for button.
            Text(text = "Normal Toast")
        }
  
  
        // on below line we are creating a spacer of height 20
        Spacer(modifier = Modifier.height(20.dp))
  
        // on below line we are creating a 
        // button for displaying a toast
        Button(
            // on below line we are adding width 
            // for our button and padding to it.
            modifier = Modifier
                .width(300.dp)
                .padding(7.dp),
  
            // in this button we are adding on click 
            // for it on below line.
            onClick = {
  
                // on below line we are displaying a custom toast
                // message on below line
                Toasty.custom(
                    ctx,
                    "This is a Custom toast.",
                    R.drawable.android,
                    R.color.purple_200,
                    3000,
                    true,
                    true
                ).show()
  
            }) {
            // on below line we are specifying 
            // text for button.
            Text(text = "Custom Toast")
        }
  
    }
}


Now run your application to see the output of it. 

Output: 



Last Updated : 19 Jun, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads