How to add a custom styled Toast in Android using Kotlin
Last Updated :
29 Jan, 2025
A Toast is a short alert message shown on the Android screen for a short interval of time. Android Toast is a short popup notification that is used to display information when we perform any operation in the app. In this article, let's learn how to create a custom toast in Android using Kotlin.

To create custom styled toast in Android using Java please refer to How to add a custom styled Toast in Android.
Table of Attributes
Attributes | Description |
---|
LayoutInflater | Instantiates a layout XML file into its corresponding View objects |
inflate | Inflate a new view hierarchy from the specified XML resource. |
setGravity | Used to change the position of Toast |
Steps to create custom styled toast in Android Kotlin
Step 1: Create new project
- Click on File, then New => New Project.
- Choose “Empty Activity” for the project template.
- Select language as Kotlin.
- Select the minimum SDK(According to the application needs).
Step 2: Create the Toast Layout
Go to res -> layout (right-click) -> new -> Layout Resource file -> Create (custom_toast_layout.xml) file. Add a CardView to contain the custom toast message and also add a TextView to display the text inside the custom toast message. FrameLayout is used to specify the position of multiple views placed on top of each other to represent a single view screen.
custom_toast_layout.xml:
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/toast_container">
<RelativeLayout
android:id="@+id/button_parent"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true">
<androidx.cardview.widget.CardView
android:id="@+id/button_card_parent"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginLeft="25dp"
app:cardElevation="20dp"
android:layout_marginRight="25dp"
app:cardCornerRadius="4dp">
<RelativeLayout
android:id="@+id/button_click_parent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:focusable="true">
<FrameLayout
android:id="@+id/button_accent_border"
android:layout_width="4dp"
android:layout_height="match_parent"
android:background="#3EAA56" />
<TextView
android:id="@+id/toast_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginStart="17dp"
android:ellipsize="end"
android:lines="1"
android:text="This is a custom Toast"
android:textColor="#131313"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout>
</RelativeLayout>
Step 3: Create a New Kotlin File
Now creates a new Kotlin file and name it as WrapToast.kt to make the code reusable. Go to Project Package (right-click) -> new -> Kotlin file/class -> Create (WrapToast.kt) file. Now we are going to extend the Toast::class with showCustomToast() which will take String and Context as a parameter.
Note:
- Inflate the previously created layout (custom_toast_layout.xml) using the layoutInflater.
- After, inflated the layout, find its view. In this case, set the text of the TextView of the message.
- The last step is to create a new instance about the Toast:: class. Then, using its application extension function sets the gravity, the duration, and the layout. Inside apply, call the show() method as well.
WrapToast.kt:
Kotlin
import android.app.Activity
import android.view.Gravity
import android.widget.TextView
import android.widget.Toast
import com.gfg.custom_toast_kotlin.R
fun Toast.showCustomToast(message: String, activity: Activity)
{
val layout = activity.layoutInflater.inflate (
R.layout.custom_toast_layout,
activity.findViewById(R.id.toast_container)
)
// Set the text of the TextView of the message
val textView = layout.findViewById<TextView>(R.id.toast_text)
textView.text = message
// Use the application extension function
this.apply {
setGravity(Gravity.BOTTOM, 0, 40)
duration = Toast.LENGTH_LONG
view = layout
show()
}
}
Step 4: Create a Button to Show Toast in an Activity
Add a Button inside the ConstraintLayout. So when the user clicks on the button then the custom Toast is popped up on the screen.
activity_main.xml:
XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/btn_show_toast"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Show Toast"
android:background="#3EAA56"
android:textColor="#fff"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Step 5: Create the Toast
After, creating the button to show a toast apply an onClickListener() and pass the Toast message and the context of the activity.
MainActivity.kt:
Kotlin
package com.gfg.custom_toast_kotlin
import android.os.Bundle
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import showCustomToast
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var button=findViewById<Button>(R.id.btn_show_toast)
button.setOnClickListener {
val toast = Toast.makeText(this, "This is a custom Toast", Toast.LENGTH_LONG)
toast.showCustomToast("This is a custom Toast", this)
}
}
}
Output:
Note: Custom toast views are no longer recommended. When in the foreground, apps can use the makeText() function to produce a normal text toast, or they can create a Snackbar. Custom toast views will not be displayed when the owning application, targeting API level Build.VERSION_CODES#R or above is in the background. For now, Toasts built using makeText() or its variations will likewise return null here in apps targeting API level Build.VERSION CODES.R or above, unless they called setView with a non-null view.
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
CTE in SQL In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi
6 min read
What is Vacuum Circuit Breaker? A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac
13 min read
Spring Boot Interview Questions and Answers Spring Boot is a Java-based framework used to develop stand-alone, production-ready applications with minimal configuration. Introduced by Pivotal in 2014, it simplifies the development of Spring applications by offering embedded servers, auto-configuration, and fast startup. Many top companies, inc
15+ min read
Python Variables In Python, variables are used to store data that can be referenced and manipulated during program execution. A variable is essentially a name that is assigned to a value. Unlike many other programming languages, Python variables do not require explicit declaration of type. The type of the variable i
6 min read