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:
- 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.<?xmlversion="1.0"encoding="utf-8"?><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><!-- To show the Toolbar--><android.support.v7.widget.Toolbarandroid: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--><Buttonandroid: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> - 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 --><stylename="toastTextStyle"parent="TextAppearance.AppCompat"><itemname="android:fontFamily">sans-serif-black</item></style> - 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.
privatevoidshowMessage(Boolean b, String msg){// Creating new instance of ToastToast toast= Toast.makeText(MainActivity.this," "+ msg +" ",Toast.LENGTH_SHORT);// Getting the ViewView view = toast.getView();// Finding the textview in Toast viewTextView text= (TextView)view.findViewById(android.R.id.message);// Setting the Text Appearanceif(Build.VERSION.SDK_INT>= Build.VERSION_CODES.M) {text.setTextAppearance(R.style.toastTextStyle);}// Showing the Toast Messagetoast.show();} - 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 buttonButton showToast= findViewById(R.id.showToast);// Setting the on click listenershowToast.setOnClickListener(newView.OnClickListener() {@OverridepublicvoidonClick(View v){// Calling the function// to show toast messageshowMessage();}}); 
Finally files are
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>  <RelativeLayout     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.zambiatek.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();    }} | 
				
					


