an Toast Util For Android!
buildscript {
repositories {
...
jcenter()
}
dependencies {
...
}
}
dependencies {
...
implementation 'com.bravin.btoast:BToast:x.x.x'
}
custom your Application and override onCreate() method
such as:
public class BToastApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
BToast.Config.getInstance()
// .setAnimate() // Whether to startAnimation. default is fasle;
// .setAnimationDuration()// Animation duration. default is 800 millisecond
// .setAnimationGravity()// Animation entering position. default is BToast.ANIMATION_GRAVITY_TOP
// .setDuration()// toast duration is Either BToast.DURATION_SHORT or BToast.DURATION_LONG
// .setTextColor()// textcolor. default is white
// .setErrorColor()// error style background Color default is red
// .setInfoColor()// info style background Color default is blue
// .setSuccessColor()// success style background Color default is green
// .setWarningColor()// waring style background Color default is orange
// .setLayoutGravity()// whan show an toast with target, coder can assgin position relative to target. default is BToast.LAYOUT_GRAVITY_BOTTOM
// .setLongDurationMillis()// long duration. default is 4500 millisecond
// .setRadius()// radius. default is half of view's height. coder can assgin a positive value
// .setRelativeGravity()// whan show an toast with target, coder can assgin position relative to toastself(like relativeLayout start end center), default is BToast.RELATIVE_GRAVITY_CENTER
// .setSameLength()// sameLength. whan layoutGravity is BToast.LAYOUT_GRAVITY_TOP or BToast.LAYOUT_GRAVITY_BOTTOM,sameLength mean toast's width is as same as target,otherwise is same height
// .setShortDurationMillis()// short duration. default is 3000 millisecond
// .setShowIcon()// show or hide icon
// .setTextSize()// text size. sp unit
.apply(this);// must call
}
}
To display an success Toast:
BToast.success(v.getContext())
.text("this is text")
.show();
To display an animate success Toast:(coder can assign animation direction using animationGravity. default is BToast.ANIMATION_GRAVITY_TOP. other values are BToast.ANIMATION_GRAVITY_LEFT, BToast.ANIMATION_GRAVITY_RIGHT, BToast.ANIMATION_GRAVITY_BOTTOM)
BToast.success(v.getContext())
.text(R.string.text_test_content)
.animate(true)
.show();
To display an success Toast with target(View):
BToast.success(v.getContext())
.text(R.string.text_test_content)
.target(target)
.show();
To display an error Toast:
BToast.error(v.getContext())
.text(R.string.text_test_content)
.show();
To display an info Toast:
BToast.info(v.getContext())
.text(R.string.text_test_content)
.show();
To display an waring Toast:
BToast.waring(v.getContext())
.text(R.string.text_test_content)
.show();
To display an normal Toast:
BToast.normal(v.getContext())
.text(R.string.text_test_content)
.show();
To display an rectangle success Toast:
BToast.success(v.getContext())
.text(R.string.text_test_content)
.radius(0)
.show();
To display an samelength success Toast with target(View): (samelength mean same width(layout_gravity_top layout_gravity_bottm) or same height(layout_gravity_left layout_gravity_right))
BToast.success(v.getContext())
.text(R.string.text_test_content)
.sameLength(true)
.target(target)
.show();
To display an relative_end samelength success Toast:
BToast.success(v.getContext())
.text(R.string.text_test_content)
.sameLength(true)
.relativeGravity(BToast.RELATIVE_GRAVITY_END)
.show();
To display an relative_end samelength animate success Toast:
BToast.success(v.getContext())
.text(R.string.text_test_content)
.relativeGravity(BToast.RELATIVE_GRAVITY_END)
.sameLength(true)
.animate(true)
.target(target)
.show();
Offset value on the X axis, if you display a toast with target, u can adjust toast postion on X axis using this value.
Offset value on the Y axis, if you display a toast with target, u can adjust toast postion on Y axis using this value.
Offset value for toast's width, if you display a toast with target and sameLength(LAYOUT_GRAVITY_TOP LAYOUT_GRAVITY_BOTTOM), u can adjust toast's width using this value.
Offset value for toast's height, if you display a toast with target and sameLength(LAYOUT_GRAVITY_LEFT LAYOUT_GRAVITY_RIGHT), u can adjust toast's height using this value.
tag is a advanced feature of BToast, toasts with same tag only can keep one in the waiting queue.if you do not set tag, tag is 0(default)
create a toast with tag
BToast.warning(v.getContext())
.text(R.string.text_test_content)
.tag(1)
.show();
u can use BToast in a sub thread without run Looper.prepare() and Looper.loop()
for example:
new Thread(new Runnable() {
@Override
public void run() {
BToast.info(MainActivity.this).text("text").show();
}
}).start();
when activity finished, toasts(delivered by this activity) inside of queue will be removed(If there is still)