Open In App

How to Change Toast font in Android?

Last Updated : 29 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

A Toast is a feedback message. It takes a very little space for displaying while overall activity is interactive and visible to the user. It disappears after a few seconds. It disappears automatically. If user wants permanent visible message, Notification can be used.

Note: Toast disappears automatically based on the toast length defined by the developer.

Steps to change the toast message font are as follow:

Step 1: Add a buttons in activity_main.xml file

Open activity_main.xml file and create a button with id showToast. It will help us show toast message with custom font.

activity_main.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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/showToast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Toast"
        android:layout_marginTop="50sp"
        android:padding="8dp"
        android:layout_centerHorizontal="true"/>

</RelativeLayout>


After filling out the activity_main file, we also need a xml file defining the custom toast for us. So, Let us create a new xml file name custom_toast.xml inside the layout directory.

custom_toast.xml:

XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/custom_toast_container"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    android:background="@drawable/toast_background">

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/white" />

</LinearLayout>


Step 2: Open styles.xml file and add new style for toast message (Optional)

Open style.xml file and add the following code. Here sans-serif-black font is used.

styles.xml:

<resources >
  <style name="toastTextStyle" parent="TextAppearance.AppCompat">
        <item name="android:fontFamily">sans-serif-black</item>
    </style>
</resources>

Note: In case you don't define styles.xml remove the involvement of xml file from MainActivity.java too.

Step 3: Open MainActivity.java and add function to show custom Toast.

We can break the step into two sub steps.

  • Clicking the Button
  • Custom Toast Appears

- setOnClickListener to the button and show the toast message.

To setOnclickListener() first create a new instance of Button class in Java file and find the button view using the id given in xml file and call the setOnClickListener() method on the button object.

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Checking on the button
Button button = findViewById(R.id.showToast);

// Clicked the Button
button.setOnClickListener(v -> showMessage());

- Implementing the Custom Toast

It is step which is easy to understand if thought in this way:

  • Inflate custom layout
  • Get the TextView from the custom layout
  • Set layout custom_toast.xml as the layout view for the toast
  • Show the toast

Now, let us check the whole process in the application.

MainActivity.java:

MainActivity.java
package com.gfg.toast_font;

import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

      	// Checking on the button 
        Button button = findViewById(R.id.showToast);

      	// Clicked the Button
        button.setOnClickListener(v -> showMessage());
    }

  	// Method Called on Button Clicked
    private void showMessage() {
      
        // Inflate custom layout
        LayoutInflater inflater = getLayoutInflater();
        View layout = inflater.inflate(R.layout.custom_toast,
                                       findViewById(R.id.custom_toast_container));

        // Get the TextView from the custom layout
        TextView text = layout.findViewById(R.id.text);
        text.setTextAppearance(R.style.toastTextStyle);
      
      	// Set the text
        text.setText("GeeksForGeeks"); 

        // Create and show the Toast
        Toast toast = new Toast(getApplicationContext());
        toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setView(layout);
        
      	// Toast Showed
      	toast.show();
    }
}
MainActivity.kt
package com.gfg.toast_font_kotlin

import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity


class MainActivity : AppCompatActivity() 
{
    override fun onCreate(savedInstanceState: Bundle?) 
  	{
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // Button in the Layout
        val button = findViewById<Button>(R.id.showToast)
        
        // Button Clicked
      	button.setOnClickListener {
            showMessage()
        }
    }

   	// Function Called when Button is
    // Clicked
    private fun showMessage()
  	{
        val inflater = getLayoutInflater()
        val container = findViewById<ViewGroup>(R.id.custom_toast_container)
        val layout = inflater.inflate(R.layout.custom_toast, container)

        val text: TextView = layout.findViewById(R.id.text)
        text.setTextAppearance(R.style.toastTextStyle)

        // Set the text
        text.text = "GeeksForGeeks"

        // Create and show the Toast
        val toast = Toast(applicationContext)
        toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0)
        toast.duration = Toast.LENGTH_LONG
        toast.view = layout
        toast.show()
    }
}

Output:




Next Article

Similar Reads