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