Create custom Toast in Android Studio

In this article we learn how to create Custom Toast in Android Studio.

Toast

A Toast provides simple feedback in a small popup about an operation. It only fills the amount of space required for the message and the current activity remains visible and interactive.

Custom-Toast-in-Android_1

Need of Custom Toast

But what if you want to display your custom view in a Toast? Such as shown in the image above, only text is displayed, no other views are attached to it. It displayed at the bottom of the screen, but what if I want to display it in
the center or the top of the screen?

Each of the preceding questions have an answer. Let’s have a look.

Tutorial: Custom Toast

In this tutorial, we will learn how to create and how to use a custom toast in our application. We will create an application that displays a toast message such as “Success” and “Failure” messages.

Step 1

Create a new Android project called “CustomToastDemo” with the following properties:

  • Project Build Target: Android 2.2 or higher
  • Package Name: com.test
  • Activity Name: CustomToastDemoActivity

Step 2

Now, we need to create a custom toast layout file. So create one layout file and name it “custom_toast.xml” and paste the following code into it:

But this code will give you an error at “drawable/success”. So what you need to do is to download 2 files shown here.

Failure Icon:

Custom-Toast-in-Android_2

Success Icon:

Custom-Toast-in-Android_3

Download them and put then in the “drawable” directory as shown in the following figure:

Custom-Toast-in-Android_4

If you see graphical output of the current layout then it will look as in the following:

Custom-Toast-in-Android_5

Step 3

Create a package called “com.custom” in your “src” directory.

Create a Java file called “CustomToast” in the “com.custom” package.

After completing the process above, paste the following code into the “CustomToast.java” file:

Step 4

Now, open your “CustomToastDemoActivity.java” file and paste following code in it.

Summary

In this tutorial, we learn how to use Toast as per our need and extend the use of Toast.

You may also like to read

NFC Mifare UID (Card Serial Number) reverse issue ... If you are using NFC (Near Field Communication) Mifare in your application, you should be careful while using NFC Mifare UID  (Unique IDentification ...
MaxHeightScrollView in Android using Android Studi... In Android, there are two constants for providing height and width of any view. But if you provide any specific height to any view, it won't be change...
Read Logs programmatically in Android In this blog you will learn how to read logs programmatically in Android and used for bug report purposes. Its easy to read logs from Logcat an...
APK Analysis : Spotlight on WhatsApp | How to use ... Android Studio includes an APK Analyzer that provides immediate insight into the composition of your APK after the build process is complete. In th...
Share on Facebook0Share on Google+0Tweet about this on TwitterShare on LinkedIn0Share on Reddit0Pin on Pinterest0Share on Tumblr0Email this to someone