Home>

bamboy's custom toast, (hereinafter referred to as "btoast")

Features are simple to use, And comes with two styles:

1) common text style;

2) With icon style.

The icons are √ and ×.

btoast has another feature:

The system comes with toast using a queue method,After the current toast disappears, the next toast can be displayed;

And btoast will top out the current toast and directly display the latest toast.

So, in three simple steps,Let's customize it now!

(A), layout:

To customize toast,

First we need an xml layout.

But before layout we need three resource files,They are background, √, and ×.

The background can be drawn using xml:

toast_back.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
  <corners android:radius="12dp" />
  <solid android:color="#cc000000" />
</shape>

√ and × are best to use pictures,There are two pictures in the source code,I won't post it here.

Now you can write the layout:

toast_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:background="@ drawable/toast_back"
       android:gravity="center_vertical"
       android:padding="13dp"
       android:orientation="vertical">
  <imageview
    android:id="@ + id/toast_img"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:background="@ drawable/toast_y"
    android:layout_gravity="center_horizontal"
    android:layout_marginbottom="5dp" />
  <textview
    android:id="@ + id/toast_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_marginleft="10dp"
    android:layout_marginright="10dp"
    android:textcolor="#ffffff"
    android:gravity="center"
    android:textsize="17sp" />
</linearlayout>

The required xml is now OK, and the rest is the java part.

(Two), java:

Write a btoast class that inherits toast, the singleton of the member variable itself, and the constructor:

public class btoast extends toast {
  / **
   * toast singleton
   * /
  private static btoast toast;
  / **
   * Construction
   *
   * @param context
   * /
  public btoast (context context) {
    super (context);
  }
}

In order to achieve the current toast demand,We need to override several methods

 / **
   * Hide current toast
   * /
  public static void canceltoast () {
    if (toast!=null) {
      toast.cancel ();
    }
  }
  public void cancel () {
    try {
      super.cancel ();
    } catch (exception e) {
    }
  }
  @override
  public void show () {
    try {
      super.show ();
    } catch (exception e) {
    }
  }

Now we can write our logic,The first is of course to introduce our layout:

 / **
   * Initialize toast
   *
   * @param context
   * @param text displayed text
   * /
  private static void inittoast (context context, charsequence text) {
    try {
      canceltoast ();
      toast=new btoast (context);
      // Get layoutinflater object
      layoutinflater inflater =
        (layoutinflater) context.getsystemservice (context.layout_inflater_service);
      // Create a view object from the layout file
      view layout=inflater.inflate (r.layout.toast_layout, null);
      // picture on toast
      toast_img=(imageview) layout.findviewbyid (r.id.toast_img);
      // text on toast
      textview toast_text=(textview) layout.findviewbyid (r.id.toast_text);
      toast_text.settext (text);
      toast.setview (layout);
      toast.setgravity (gravity.center, 0, 70);
    } catch (exception e) {
      e.printstacktrace ();
    }
  }

Everything is ready,The next step is to display the toast:

 / **
   * Icon status No icon
   * /
  private static final int type_hide=-1;
  / **
   * Icon status display √
   * /
  private static final int type_true=0;
  / **
   * Icon status display ×
   * /
  private static final int type_false=1;
  / **
   * Show toast
   *
   * @param context
   * @param text displayed text
   * @param time display duration
   * @param imgtype icon status
   * /
  private static void showtoast (context context, charsequence text, int time, int imgtype) {
    // initialize a new toast object
    inittoast (context, text);
    // Set the display duration
    if (time == toast.length_long) {
      toast.setduration (toast.length_long);
    } else {
      toast.setduration (toast.length_short);
    }
    // determine whether the icon should be displayed,Show √ or ×
    if (imgtype == type_hide) {
      toast_img.setvisibility (view.gone);
    } else {
      if (imgtype == type_true) {
        toast_img.setbackgroundresource (r.drawable.toast_y);
      } else {
        toast_img.setbackgroundresource (r.drawable.toast_n);
      }
      toast_img.setvisibility (view.visible);
      // animation
      objectanimator.offloat (toast_img, "rotationy", 0, 360) .setduration (1700) .start ();
    }
    // show toast
    toast.show ();
  }

It's that simple.

Attentive friends may find out,This method is private. Do n’t worry about it.Listen to me slowly.

Write here, In fact, you can directly change this method to public. Then you are done. But how is this different from using native toast?

Still need to write that long list of parameters,The only good thing is that you don't have to write.show ().

What we do now is called "custom", "custom" means we define the rules ourselves, In this case, Why don't we improve the "user experience"?

Besides, this "user" is still ourselves.

Not much nonsense, We begin the last step.

(3) Sublimation:

 / **
   * Show a plain text toast
   *
   * @param context
   * @param text displayed text
   * /
  public static void showtext (context context, charsequence text) {
    showtoast (context, text, toast.length_short, type_hide);
  }
  / **
   * Display an icon of toast
   *
   * @param context
   * @param text displayed text
   * @param issucceed shows [check icon] or [cross icon]
   * /
  public static void showtext (context context, charsequence text, boolean issucceed) {
    showtoast (context, text, toast.length_short, issucceed?type_true:type_false);
  }
  / **
   * Show a plain text toast
   *
   * @param context
   * @param text displayed text
   * @param time duration
   * /
  public static void showtext (context context, charsequence text, int time) {
    showtoast (context, text, time, type_hide);
  }
  / **
   * Display an icon of toast
   *
   * @param context
   * @param text displayed text
   * @param time duration
   * @param issucceed shows [check icon] or [cross icon]
   * /
  public static void showtext (context context, charsequence text, int time, boolean issucceed) {
    showtoast (context, text, time, issucceed?type_true:type_false);
  }

A few simple methods,The user experience skyrocketed in an instant,Take a look at the time of use:

 public void click (view view) {
    switch (view.getid ()) {
      case r.id.btn_text:
        btoast.showtext (this, "Simple Tip");
        break;
      case r.id.btn_text_true:
        btoast.showtext (this, "Simple hint right icon", true);
        break;
      case r.id.btn_text_false:
        btoast.showtext (this, "Simple hint error icon", false);
        break;
      case r.id.btn_text_long:
        btoast.showtext (this, "Simple hint long ~", toast.length_long);
        break;
      case r.id.btn_text_true_long:
        btoast.showtext (this, "Simple hint for correct icon length ~", toast.length_long, true);
        break;
      case r.id.btn_text_false_long:
        btoast.showtext (this, "Simple hint error icon long ~", toast.length_long, false);
        break;
    }
  }

Is it much easier to use than native toast?

Complete merit!

Take a quick look at our results:

bamboytoast renderings

  • Previous The difference between the use of PHP isset () and empty ()
  • Next Detailed explanation of CentOS72 deployment of KVM virtual machine (graphics)
  • Trends