Simple use of a beautiful Toast third-party library

Keywords: Android Gradle Maven github

  • I used to use the Android native Toast. I feel that there is no need to work hard on it. When I saw the Toast, it was immediately circled. It's really beautiful.
  • Project address

As we all know, the usage of Android native Toast is

1 Toast.makeText(MainActivity.this,"Toast show contents",Toast.LENGTH_SHORT).show();

Method is divided into Context, content and duration (length  short is short time, 2 seconds). Length long is 3.5 seconds. It can also be constructed in milliseconds. And the actual use of toast is basically the same as the original use method, which is very comfortable.

 

First of all, we need to add a warehouse in build.gradle of the project root directory

1 allprojects {
2     repositories {
3         ...
4         maven { url "https://jitpack.io" }
5     }
6 }

 

Then, add the dependency in build.gradle under the app directory

1 dependencies {
2     ...
3     //As of the time of writing this blog, the latest version is 1.4.2
4     implementation 'com.github.GrenderG:Toasty:1.4.2'
5 }

 

After completing the above preparations, you can use it now

 

Error Toast

1 Toasty.error(MainActivity.this, "This is an error toast.", Toast.LENGTH_SHORT, true).show();

 

Successful Toast

1 Toasty.success(MainActivity.this, "This is a success toast.", Toast.LENGTH_SHORT, true).show();

 

Prompt Toast

1 Toasty.info(MainActivity.this, "This is an info toast.", Toast.LENGTH_SHORT, true).show();

 

Warning Toast

1 Toasty.warning(MainActivity.this, "This is a warning toast.", Toast.LENGTH_SHORT, true).show();

 

Native Toast

Of course, there are also modified native Toast to meet your different needs

1 Toasty.normal(MainActivity.this, "Normal toast.").show();

 

Native Toast with pictures

1 Toasty.normal(MainActivity.this, "Normal toast.", IconDrawable).show();
2 //IconDrawable It's the picture you need to put in. You can use getDrawable Realization. Software icon as Toast For example, the usage is:
3 Toasty.normal(MainActivity.this, "Normal toast.", getDrawable(R.drawable.ic_launcher)).show();

 

Custom Toast

Of course, there must be custom Toast

1 //The official introduction is:
2 Toasty.custom(yourContext, "I'm a custom Toast", yourIconDrawable, tintColor, duration, withIcon, shouldTint).show();

But in actual use, you need to complete the steps by yourself

The picture still uses getDrawable, while the color can directly use the Android defined Color.BLUE and Color.RED to set the background color and text color respectively. The usage is as follows:

1 Toasty.custom(MainActivity.this, "I'm a custom Toast", getDrawable(R.drawable.ic_launcher),Color.BLUE,Color.RED, Toast.LENGTH_SHORT, true,true).show();

However, in practice, it is more necessary to customize the color. This can be achieved with a similar getColor:

1. Set the color value in colors.xml under the / res/valus directory

1 <resources>
2     <color name="color1">#BBDEFB</color>
3     <color name="color2">#ffffff</color>
4 </resources>

2. Use getColor to reference color1 and color2

1 Toasty.custom(MainActivity.this, "I'm a custom Toast", getDrawable(R.drawable.ic_launcher),getColor(R.color.color1),getColor(R.color.color2), Toast.LENGTH_SHORT, true,true).show();

 

Unified configuration

Toasty supports unified configuration of content, but I haven't studied it at present. Interested people can go to the original project to check readme and source code to learn how to use them.

 

Effect display

 

Related resources

Download demo

Download source code

Posted by CoreyR on Thu, 09 Jan 2020 06:00:45 -0800