VOOZH about

URL: https://www.geeksforgeeks.org/android/android-how-to-change-toast-font/

⇱ How to Change Toast font in Android? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Change Toast font in Android?

Last Updated : 12 Jul, 2025

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 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:


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:


Step 2: Open 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 and add function to show custom Toast.

We can break the step into two sub steps.

  • Clicking the Button
  • Custom Toast Appears

- 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:

Output:



Comment

Explore