Codementor Events

Custom Toast Library (Android Java)

Published Feb 17, 2019Last updated Feb 27, 2019
Custom Toast Library (Android Java)

Do you want to create a fancy toast message in your android application with just a few lines of code? , you are on the right blog, am going to introduce to you an Android library called HuxyApp.

This library has the following functionalities. :

  1. It displays warning,danger and success toast messages.
  2. It enables to customize a toast by specifiying the colors of your choice.
  3. It enables to change the font type or style of the toast message.

I know you really want to see this , let's dive in.

The following are the steps for setting up a HuxyApp library in your application.

STEP 1.

Add the JitPack repository to your build file as below :

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

Then add the dependency.

 dependencies {

           implementation 'com.github.huxaiphaer:HuxyLab:1.0.0'     
  }

STEP 2.

Then after that, we can start using our library. So let's start with an example of a successToast message.

Success Toast.

Below it's how it's implemented :

 HuxyApp.successToast(MainActivity.this, "Good Internet Connection")
                .setPadding(3)
                .setPositionAndOffSet(Gravity.CENTER,0,0);

The code above includes the successToast() method which takes in two parameters the Context and the String for the toast message. Then the next method setPadding() enables to set the message padding within the toast message , you can adjust this to nay integer values, then lastly , setPositionAndOffSet(), it takes in three parameters position, xOffset and yOffset. The position can vary according to what you want , you can use also the following positions Gravity.BOTTOM, Gravity.LEFT, Gravity.RIGHT etc. For the Offsets xOffset & yOffset these one help to adjust the position of the toast on the x and y axis (This reminds of maths we are plotting 😄)

Ok then, let's have a look of the toast message :

success_toast_gif.gif

Danger Toast.

Below is the Example of Danger toast, you need to specify the dangerToast() method. By default this toast's background is RED.

  HuxyApp.dangerToast(MainActivity.this, "Bad Internet Connection")
                .setPadding(3)
                .setPositionAndOffSet(Gravity.CENTER,0,0);

This is an example for it .

dangertoast.gif

Warning Toast.

Below is the example of a warningToast. You just need to specify the method warningToast(). By default this toast's background is ORANGE.

 HuxyApp.warningToast(MainActivity.this, "Slow Internet Connection")
                .setPadding(3)
                .setPositionAndOffSet(Gravity.CENTER,0,0);

warningtoast.gif

Custom Toast Message.

If you want to come up with your custom message, such as changing the Background color or the textColor etc. you have to use the customToast() method.

 String Bg = "#000000";
        String textColor ="#ffffff";
        HuxyApp.customToast(MainActivity.this, "No internet connection please.",Bg,textColor)
                .setPositionAndOffSet(Gravity.BOTTOM,0,20);

Below is the example.

customtoast.gif

Font Style For a Toast Message.

You have to follow the following steps to create a toast with a font type of your choice.

a). Initialize HuxyApp and give it an attribute called setFontStyle(Context,FontPath) .
e.g

 HuxyApp.customToast(MainActivity.this, "No internet connection please.",Bg,textColor)
                .setPositionAndOffSet(Gravity.BOTTOM,0,0)
                .setFontStyle(this,R.font.font_name);

b). Download the font of your choice , you can use Google Fonts Site

c). Then create a folder called font inside the res folder. e.g app/src/main/res/font.

d). Place any font of your choice , and then run the application. The toast should appear with your font style or type.
custom_font.gif

I think now you are able to come up with nice and styled amazing toast messages.

If you are a developer and you want to contribute on this library , feel free to raise a PR. This is the projects

Buy me a cup of coffee, if you liked my blog 😄

Discover and read more posts from Lutaaya Huzaifah Idris
get started
post commentsBe the first to share your opinion
Show more replies