Android | How to Change Toast font?

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.

Toast disappears automatically based on the toast length defined by the developer. Steps to change the toast message font are as follow:

  1. Step 1: Add a buttons in activity_main.xml file to show toast message with custom font.
    Open activity_main.xml file and create a button with id showToast.



    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
      
        <!--  To show the Toolbar-->
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:background="@color/colorPrimary"
            app:title="GFG"
            app:titleTextColor="@android:color/white"
            android:layout_height="android:attr/actionBarSize"/>
      
        <!-- Button To show the toast message-->
      
        <Button
            android:id="@+id/showToast"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Show Toast"
            android:layout_marginTop="16dp"
            android:padding="8dp"
            android:layout_below="@id/toolbar"
            android:layout_centerHorizontal="true"/>
      
    </RelativeLayout>

    chevron_right

    
    

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

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

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

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

    Create a new instance of Toast using makeText() method. Use getView() method to get the view of the Toast. Open MainActivity.java file and add function to show toast message.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    private void showMessage(Boolean b, String msg)
    {
      
        // Creating new instance of Toast
        Toast toast
            = Toast.makeText(
                MainActivity.this,
                " " + msg + " ",
                Toast.LENGTH_SHORT);
      
        // Getting the View
        View view = toast.getView();
      
        // Finding the textview in Toast view
        TextView text
            = (TextView)view
                  .findViewById(
                      android.R.id.message);
      
        // Setting the Text Appearance
        if (Build.VERSION.SDK_INT
            >= Build.VERSION_CODES.M) {
            text.setTextAppearance(
                R.style.toastTextStyle);
        }
      
        // Showing the Toast Message
        toast.show();
    }

    chevron_right

    
    

  4. Step 4: setOnClickListner to the button and show the toast message.

    To setOnclickListner() 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.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Finding the button
    Button showToast
        = findViewById(R.id.showToast);
      
    // Setting the on click listener
    showToast
        .setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v)
                {
      
                    // Calling the function
                    // to show toast message
                    showMessage();
                }
            });

    chevron_right

    
    

Finally files are

activity_main.xml

filter_none

edit
close

play_arrow

link
brightness_4
code

<?xml version="1.0" encoding="utf-8"?>
  
<RelativeLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
  
    <!--  To show the Toolbar-->
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:background="@color/colorPrimary"
        app:title="GFG"
        app:titleTextColor="@android:color/white"
        android:layout_height="android:attr/actionBarSize"/>
  
    <!-- Button To show the toast message-->
  
    <Button
        android:id="@+id/showToast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Toast"
        android:layout_marginTop="16dp"
        android:padding="8dp"
        android:layout_below="@id/toolbar"
        android:layout_centerHorizontal="true"/>
  
</RelativeLayout>

chevron_right


styles.xml

filter_none

edit
close

play_arrow

link
brightness_4
code

<resources >
  
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
  
    <!-- Toast Style -->
    <style name="toastTextStyle" parent="TextAppearance.AppCompat">
        <item name="android:fontFamily">sans-serif-black</item>
    </style>
</resources>

chevron_right


MainActivity.java

filter_none

edit
close

play_arrow

link
brightness_4
code

package org.geeksforgeeks.customtoast;
  
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
  
public class MainActivity extends AppCompatActivity {
  
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // Finding the button
        Button showToast = findViewById(R.id.showToast);
  
        // Setting the on click listener
        showToast.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                showMessage();
            }
        });
    }
  
    private void showMessage()
    {
  
        // Creating new instance of Toast
        Toast toast
            = Toast.makeText(
                MainActivity.this,
                "GeeksForGeeks",
                Toast.LENGTH_SHORT);
  
        // Getting the View
        View view = toast.getView();
  
        // Finding the textview in Toast view
        TextView text
            = (TextView)view.findViewById(
                android.R.id.message);
  
        // Setting the Text Appearance
        if (Build.VERSION.SDK_INT
            >= Build.VERSION_CODES.M) {
            text.setTextAppearance(
                R.style.toastTextStyle);
        }
  
        // Showing the Toast Message
        toast.show();
    }
}

chevron_right


Output:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.