Wonderful Android Toasts with Sound

Hemant Yadav
Frontend Weekly
Published in
3 min readDec 20, 2019

Android Toasts are small messages which pop-up on the screen for a short period of time to notify the users about some action which they have recently performed or to notify them about some other tasks and then they fade away within a second or two.

The build-in android toasts are quite simple. They have a grey background with a message inside it. They fulfill the job of simply notifying the user with some message. However, they are not quite appealing because all types of toasts i.e. success messages, failure messages, some warnings, etc have the same layout. Here I have a library for you, the “ Expressive_Toasts ” library with different backgrounds and sound clips (according to the context of the message) to express the message in a more lively way.

Adding it to your App

The library is published on JitPack. So it can simply be added in your app by following procedure :

1. Adding Dependency

First add the repository into your project gradle file :

allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}

Then add the following Dependency to your app gradle file :

dependencies {
...
implementation 'com.github.Hemant27031999:Expressive_Toasts:0.0.2'
}

That’s all the formal work you have to do. Now you can make the toasts using the following methods :

These methods takes in the following argument and show the toast instantaneously when they are called :

  1. context : The context of the activity/fragment in which you want to show the toast.
  2. message : The message you want to display in the toast.
  3. duration : The duration of the toast i.e. Toast.LENGTH_SHORT for short duration toasts and Toast.LENGTH_LONG for long duration toasts.

The methods have their own characteristics of background color and sound as shown in the video. The MyToast.simple(…) toast is accompanied by a twirk sound and simple grey color.

Wanna make your own Toasts ?

Well, this library also provides you with a method by which you can make your own customized toasts. Here are these methods :

Three arguments i.e context, message and duration are same as above. You can make Toasts with different background color by passing the hexColor value of the color you want. Make sure to add ‘#’ in the hexColor like if you want a white background, pass “#FFFFFF” as the argument.

Finally to add your own sound clip, you can pass the id of that sound in the method. Put the sound clip in the ‘raw’ folder inside ‘res’ directory of your app. For better experience, try to have the sound clip of same duration as that of toast so that it ends along with the toast.

TADA! That’s all what you need to do to make your toasts more expressive. To see the complete code of the library, follow this github link.

--

--