Tuesday, November 19, 2024
Google search engine
HomeLanguagesJavaAndroid | How to Change Toast font?

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.




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

    
    
  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.




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

    
    
  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.




    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();
    }

    
    
  4. Step 4: 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.




    // 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();
                }
            });

    
    

Finally files are

activity_main.xml




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


styles.xml




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


MainActivity.java




package org.neveropen.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();
    }
}


Output:

RELATED ARTICLES

Most Popular

Recent Comments