Android Tutorial

Software Setup and Configuration

Android Studio

File Structure

Components

Core Topics

Layout

View

Button

Intent and Intent Filters

Toast

RecyclerView

Fragments

Adapters

Other UI Component

Image Loading Libraries

Date and Time

Material Design

Bars

Working with Google Maps

Chart

Animation

Database

Advance Android

Jetpack

Architecture

App Publish

App Monetization

Android Toast in Kotlin

A Toast in Android provides simple feedback in a small popup. It is used to display short-lived messages to the user. Toasts are shown on the UI thread, and they are automatically dismissed after a certain period.

Here's how you can create and display a Toast in Kotlin:

  • Using the Default Toast Lengths:

There are two default durations you can use with a Toast: Toast.LENGTH_SHORT and Toast.LENGTH_LONG.

Toast.makeText(this, "Short Toast Message", Toast.LENGTH_SHORT).show()
Toast.makeText(this, "Long Toast Message", Toast.LENGTH_LONG).show()
  • Custom Toast with Custom View:

You can create a custom layout for a Toast, giving you more control over its appearance.

First, create an XML layout for the Toast:

res/layout/custom_toast.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="8dp"
    android:background="@android:color/holo_blue_dark">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@android:drawable/ic_dialog_info" />

    <TextView
        android:id="@+id/customToastText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/white"
        android:padding="8dp"
        android:text="Custom Toast" />

</LinearLayout>

Use this layout in Kotlin:

val layoutInflater = LayoutInflater.from(this)
val customToastRoot = layoutInflater.inflate(R.layout.custom_toast, null)
val customToastText: TextView = customToastRoot.findViewById(R.id.customToastText)
customToastText.text = "This is a custom toast message."

val customToast = Toast(this)
customToast.view = customToastRoot
customToast.duration = Toast.LENGTH_LONG
customToast.show()
  • Gravity and Position:

You can also set the position of the Toast using setGravity:

val toast = Toast.makeText(this, "Toast at Top", Toast.LENGTH_SHORT)
toast.setGravity(Gravity.TOP, 0, 0)
toast.show()

In this example, the Toast will appear at the top of the screen. The two zero values after Gravity.TOP are the x-offset and y-offset from the specified gravity.

When using Toasts, make sure they don't disrupt the user experience. Use them judiciously for short, non-critical messages. For critical notifications, consider using a Snackbar or other UI elements.

  1. Using Toast messages in Android with Kotlin:

    Toast.makeText(applicationContext, "Hello, Toast!", Toast.LENGTH_SHORT).show()
    
  2. Creating custom Toast in Android using Kotlin:

    val inflater: LayoutInflater = layoutInflater
    val layout: View = inflater.inflate(R.layout.custom_toast_layout, findViewById(R.id.custom_toast_container))
    
    val toast = Toast(applicationContext)
    toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0)
    toast.duration = Toast.LENGTH_SHORT
    toast.view = layout
    toast.show()
    
  3. Positioning Toast messages in Android Kotlin:

    toast.setGravity(Gravity.TOP or Gravity.START, 0, 0)
    
  4. Changing Toast background color in Kotlin:

    val toast = Toast.makeText(applicationContext, "Custom Background Color", Toast.LENGTH_SHORT)
    val view = toast.view
    view.setBackgroundColor(Color.RED)
    toast.show()
    
  5. Styling Toast text in Android with Kotlin:

    val toast = Toast.makeText(applicationContext, "Styled Text", Toast.LENGTH_SHORT)
    val view = toast.view
    val textView = view.findViewById<TextView>(android.R.id.message)
    textView.setTextColor(Color.WHITE)
    textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18f)
    toast.show()
    
  6. Long and short duration Toast in Kotlin:

    Toast.makeText(applicationContext, "Short Duration", Toast.LENGTH_SHORT).show()
    Toast.makeText(applicationContext, "Long Duration", Toast.LENGTH_LONG).show()