Android | How to Change Toast font? - GeeksforGeeks

17 Sep, 2019
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.<?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">      <!--  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>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>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();}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.// Finding the buttonButton showToast    = findViewById(R.id.showToast);  // Setting the on click listenershowToast    .setOnClickListener(        new View.OnClickListener() {            @Override            public void onClick(View v)            {                  // Calling the function                // to show toast message                showMessage();            }        });
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: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">      <!--  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>
<?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">      <!--  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>
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>
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>
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();}
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();}
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.// Finding the buttonButton showToast    = findViewById(R.id.showToast);  // Setting the on click listenershowToast    .setOnClickListener(        new View.OnClickListener() {            @Override            public void onClick(View v)            {                  // Calling the function                // to show toast message                showMessage();            }        });
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.
// Finding the buttonButton showToast    = findViewById(R.id.showToast);  // Setting the on click listenershowToast    .setOnClickListener(        new View.OnClickListener() {            @Override            public void onClick(View v)            {                  // Calling the function                // to show toast message                showMessage();            }        });
Finally files are
activity_main.xml
styles.xml
MainActivity.java
<?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">      <!--  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>
<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>
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();    }}
Output:
android
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Different ways of Reading a text file in Java
Stream In Java
Constructors in Java
Generics in Java
Exceptions in Java
Functional Interfaces in Java
Comparator Interface in Java with Examples
Commonly Asked Java Programming Interview Questions | Set 2
HashMap get() Method in Java
Strings in Java