Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to Customize Toast in Android?

  • Last Updated : 08 Oct, 2021

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




<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
  
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Toast"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
  
</androidx.constraintlayout.widget.ConstraintLayout>

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.

Kotlin




import android.graphics.Color
import android.os.Bundle
import android.view.Gravity
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
  
class MainActivity : AppCompatActivity() {
  
    lateinit var btn: Button
  
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        btn = findViewById(R.id.button1)
        val text = "GeeksForGeeks"
  
        btn.setOnClickListener {
            showToast(text)
        }
    }
  
    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
        viewGroup!!.setBackgroundColor(Color.parseColor("#079A0F")) 
  
        // Display the Toast
        toast.show() 
    }
}

So our app is ready.

Output:


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!