How to Customize Toast in Android? - GeeksforGeeks

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 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: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/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.Colorimport android.os.Bundleimport android.view.Gravityimport android.view.ViewGroupimport android.widget.Buttonimport android.widget.TextViewimport android.widget.Toastimport 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:
Android
Kotlin
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Resource Raw Folder in Android Studio
Flutter - Custom Bottom Navigation Bar
How to Read Data from SQLite Database in Android?
Flexbox-Layout in Android
How to Post Data to API using Retrofit in Android?
Android UI Layouts
Kotlin Array
Retrofit with Kotlin Coroutine in Android
Kotlin Setters and Getters
Kotlin when expression