There are various advantages of using Toast but specifically, it depends on the requirements. I prefer toasts because it doesn’t block the Activity or Fragment while running and disappears after a short time. It can be used to show feedback to the user in response to action such as form submission, etc. Most people use default Toasts at the initial stages of the project and move to the pop-up dialog box later. According to them, Toasts look boring and monotonous, but this isn’t always true. Just like any other component of Android, you can also customize Toasts in various ways. You can change color, shape, font size; add gravity, and even add images. So, let’s have a look at how you can create your own custom toast with an image and text for your Android apps. Also Read: 5 Tips to Speed Up Gradle Build in Android Studio
How to Create Custom Toast in Android?
First of all, you’ll have to create an XML layout file and add all the items that you want to display inside the Toast. Just remember, the layout_height and layout_width should be “match_parent” to cover the entire area available. And, don’t forget to add layout id. custom_toast.xml Then, create another XML file for the appearance of the text view which will be used as background in the above layout. This file should be present in the drawable folder. custom_textview.xml Now, go to the MainActivity.java file, look for the onCreate() method, and add the following code. Though I tried to explain each step in the code, it would be better if you proceed with the basic concept i.e what needs to be done here. In simple words, first, I am inflating the custom layout, creating Toast, setting properties such as duration, gravity, view, etc, and showing it. MainActivity.java Recommended: 5 Useful Tips for Becoming a Successful Android Developer That’s it for this article. I hope you understood the concept and can now create custom Toasts. If you face any problem by following this article, do let us know in the comments below.