Hello guys, are you bored using the same Toast all the time and fancied by all the libraries that are out there and rely on them for a customized Toast? Well, not anymore when you can create your own custom Toast and also can put it out as a library.  Yes, you heard me correct, in my next blog post I will tell you how you can publish your own library.


Let’s start,

Toast Overview:-
A Toast is a view that provides quick feedback for the user in a small popup. That’s it. it’s pretty simple but handy tool.

Ok, Ok, I am coming to the topic.

First Step, create a layout file named “custom_toast_layout.xml” (you can name it whatever you want to) for your custom toast with an image view and a text view.

custom_toast_layout.xml

 

If you see the above layout you see that I have included the background “@drawable/toast_round_background” inside our parent constraint layout that is because Toast in Android is not round by default. 

toast_round_background.xml

the above code is simple enough to understand but although I’ll try to describe it a little. The drawable has a radius of 40dp and that is because I don’t think it will be any bigger than it is normally displayed, with a white color in the background and a size of 82dp for the drawable.

After having these two files we will be discussing some real code to start off we will create a Kotlin class named “KCustomToast” basically k is for Kotlin and nothing else. You must be thinking why I am creating a custom file for showing the just a Toast. So, to remind you as I mentioned at the start that I will be writing about publishing a library in the next blog this is what I am planning to publish pretty soon. Coming back to the topic let’s see some code. I will be creating an info toast.

KCustomToast.kt

 

So from what we can see above, we have created a “companion object” for those who don’t know Kotlin this is how we declare something static in Kotlin I have declared 3 variables for specifying the gravity I have done that because I don’t want you to use any other class in this case “Gravity” GRAVITY_TOP = 48, GRAVITY_CENTER = 17, GRAVITY_BOTTOM = 80. So, we will be using everything of ours. 😉 

next, private lateinit var layoutInflater: LayoutInflater we will be needing layout inflater to inflate our custom toast layout that we will be using.

next, as you can see I have created two version of infoToast one with custom font and one with the normal font.

So, now we are ready we just need to call this in out activity. Let’s head to “MainActivity.kt”.

MainActivity.kt

Pretty basic here, I have created a function that is directly called from the button in any case you don’t know how to do that have a look.

activity_main.xml

Hurray!! A custom Toast, we have done it. If you followed me correctly you should see something like the below image.

Hope, this will make you the boss of Toast. Go ahead and explore all the other possibilities for a Toast and may the force be with you.

If you have any question just ask me in comments and I promise I will help you. pinky promise. 😉 See you until next time.