![]() |
VOOZH | about |
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.
Note: Toast disappears automatically based on the toast length defined by the developer.
Steps to change the toast message font are as follow:
Open activity_main.xml file and create a button with id showToast. It will help us show toast message with custom font.
After filling out the activity_main file, we also need a xml file defining the custom toast for us. So, Let us create a new xml file name custom_toast.xml inside the layout directory.
Open style.xml file and add the following code. Here sans-serif-black font is used.
<resources ><style name="toastTextStyle" parent="TextAppearance.AppCompat"><item name="android:fontFamily">sans-serif-black</item></style></resources>
Note: In case you don't define styles.xml remove the involvement of xml file from MainActivity.java too.
We can break the step into two sub steps.
- 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.
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);// Checking on the button
Button button = findViewById(R.id.showToast);// Clicked the Button
button.setOnClickListener(v -> showMessage());
It is step which is easy to understand if thought in this way:
Now, let us check the whole process in the application.