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

DevFestAhm – GDGAhmedabad 2015 – Aweso... Google Developer Group is Open for all including students, newbies, developers and anyone for all who are interested in Google technologies. It is Guj...
Develop apps faster using Data Binding – Par... Google has been pushing updates to existing tools and making the life of android developers easier. They also have been releasing new libraries and AP...
ProGuard Secure your APK from Reverse Engineering ... Following are words from http://proguard.sourceforge.net who are inventor of it. ProGuard is a free Java class file shrinker, optimizer, obfuscator...
Develop apps faster using Data Binding – Part 4 Google has been pushing updates to existing tools and making the life of android developers easier. They also have been releasing new libraries and AP...
Share on Facebook0Share on Google+0Tweet about this on TwitterShare on LinkedIn0Share on Reddit0Pin on Pinterest0Share on Tumblr0Email this to someone