Open In App

How to Customize Toast in Android?

Last Updated : 08 Oct, 2021
Like Article

A Toast is a feedback message. It takes very little space for displaying and it is displayed on top of the main content of an activity, and only remains visible for a short time period. In this article, we will learn how to customize Toast in android. So, we will understand this by making a simple app to display a Toast.

Step by Step Implementation

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.

Step 2: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. 


<?xml version="1.0" encoding="utf-8"?>
        android:text="Show Toast"
        app:layout_constraintTop_toTopOf="parent" />

Step 3: 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. Here we will bind the views and write the logic of the app.


import android.os.Bundle
import android.view.Gravity
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
class MainActivity : AppCompatActivity() {
    lateinit var btn: Button
    override fun onCreate(savedInstanceState: Bundle?) {
        btn = findViewById(
        val text = "GeeksForGeeks"
        btn.setOnClickListener {
    private fun showToast(text: String) {
        val toast = Toast.makeText(this, text, Toast.LENGTH_SHORT)
        // Set the position of the toast
        toast.setGravity(Gravity.CENTER_HORIZONTAL, 0, 0
        val viewGroup = toast.view as ViewGroup?
          // Get the TextView of the toast
        val textView = viewGroup!!.getChildAt(0) as TextView 
        // Set the text size
        textView.textSize = 20f 
        // Set the background color of toast
        // Display the Toast 

So our app is ready.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads