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

Android 6.0 “M” – new features r... Google is providing a newer version of Android OS each year. This time also we are expecting a newer version of Android 6.0 by the end of this year. ...
Customizing TextInputLayout – Part 1 In previous article TextInputLayout – Material Design Support library Tutorial, we learn what is TextInputLayout and what is functionality of it. How ...
Exit from Activity with twice “Back” b... When we are doing some important operation or work on activity and it may possible that by mistake we pressed "back" button. Then what?? This will sim...
Using Facebook’s Shimmer effect in Android &... In this tutorial, you will learn how to implement "Shimmer effect" developed by Facebook in RecyclerView for Android. In Facebook, you see that un...
Share on Facebook0Share on Google+0Tweet about this on TwitterShare on LinkedIn0Share on Reddit0Pin on Pinterest0Share on Tumblr0Email this to someone