中文版文档请戳:这里
- elegant & flexible
- can handle click event & custom duration (by adding view with WindowManager)
- auto avoid BadTokenException in android N (which cannot be caught in you application)
- step 1 : add dependency
allprojects {
repositories {
......
maven { url 'https://jitpack.io' }
}
}
dependencies {
......
compile 'com.github.bboylin:UniversalToast:v1.0.4'
}- step 2 : simply use it like system toast
UniversalToast.makeText(context, text, duration).show();
UniversalToast.makeText(context, text, duration,type).show();duration must be either UniversalToast.LENGTH_LONG or UniversalToast.LENGTH_SHORT,
type must be one of UniversalToast.UNIVERSAL ,UniversalToast.EMPHASIZE,UniversalToast.CLICKABLE, by default UniversalToast.UNIVERSAL.
from left to right : UNIVERSAL , EMPHASIZE , CLICKABLE , you can set the icon and text as you like 。
- further api:
//example
UniversalToast.makeText(context, text, UniversalToast.LENGTH_SHORT, UniversalToast.CLICKABLE)
.setGravity(gravity,xOffset,yOffset)
.setBackground(drawable)//set the background drawable as you like
.setColor(R.color.my_color)//set the background color as you like
.setIcon(R.drawable.my_ic)// set the icon as you like (it's visibility is gone until you set icon)
.setClickCallBack(text,R.drawable.my_btn,onClickListener)
.show();it has been provided 3 basically default icons for you,you can use showSuccess(),showWarning() or showError() instead of show()
from left to right : showSuccess , showWarning , showError (the pics above shows the type EMPHASIZE ,you can also use CLICKABLE or UNIVERSAL)
using UniversalToast.CLICKABLE on android O requires permission :
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />Refer to demo about how to request permissions dynamically on android O.
with system versions below Android 8.0,it's ok if you don't want to request permission dynamically,but it's also required to request permission staticly in AndroidManifest.xml
thanks to : ToastCompat







