Home>

Toast in android is a simple message prompt box,toast prompt box cannot be clicked by the user,toast will automatically disappear after the display time set by the user.This article will introduce the custom use of toast,Let ’s take a look together.

1.toast source analysis

Old rules, let's first look at the source code of toast.

toast has two display layout methods,One of the most common calls totoast.maketext () , see the source code is written like this

public static toast maketext (context context, charsequence text, @duration int duration) {
toast result=new toast (context);
layoutinflater inflate=(layoutinflater)
context.getsystemservice (context.layout_inflater_service);
view v=inflate.inflate (com.android.internal.r.layout.transient_notification, null);
textview tv=(textview) v.findviewbyid (com.android.internal.r.id.message);
tv.settext (text);
result.mnextview=v;
result.mduration=duration;
return result;
}

transient_notification this layout file code is like this

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="?android:attr/toastframebackground">
<textview
android:id="@ android:id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:textappearance="@ style/textappearance.toast"
android:textcolor="@ color/bright_foreground_dark"
android:shadowcolor="#bb000000"
android:shadowradius="2.75"
/>
</linearlayout>

Then we want to modify the toast text message style,In fact, it is to modify the toast root layout and message textview.

Toast's other display mode is custom layout display.This method does not calltoast.maketext () method, but new a toast object, and then callsetview () method. Of course the custom layout will not loadtransient_notification layout.

2. Implement custom toast

Let me show you the tool class toastutil I packaged.

import android.content.context;
import android.view.view;
import android.widget.linearlayout;
import android.widget.textview;
import android.widget.toast;
/**
 * created byZhao Chenzhang on 2016/8/11.
 * /
public class toastutil {
private toast toast;
private linearlayout toastview;
/**
 * Modify the toast of the original layout
 * /
public toastutil () {
}
/**
 * Fully custom layout toast
 * @param context
 * @param view
 * /
public toastutil (context context, view view, int duration) {
  toast=new toast (context);
  toast.setview (view);
  toast.setduration (duration);
}
/**
 * Add custom view to toast
 * @param view
 * @param postion
 * @return
 * /
public toastutil addview (view view, int postion) {
  toastview=(linearlayout) toast.getview ();
  toastview.addview (view, postion);
  return this;
}
/**
 * Set toast font and background color
 * @param messagecolor
 * @param backgroundcolor
 * @return
 * /
public toastutil settoastcolor (int messagecolor, int backgroundcolor) {
  view view=toast.getview ();
  if (view!=null) {
    textview message=((textview) view.findviewbyid (android.r.id.message));
    message.setbackgroundcolor (backgroundcolor);
    message.settextcolor (messagecolor);
  }
  return this;
}
/**
 * Set toast font and background
 * @param messagecolor
 * @param background
 * @return
 * /
public toastutil settoastbackground (int messagecolor, int background) {
  view view=toast.getview ();
  if (view!=null) {
    textview message=((textview) view.findviewbyid (android.r.id.message));
    message.setbackgroundresource (background);
    message.settextcolor (messagecolor);
  }
  return this;
}
/**
 * Short time display toast
 * /
public toastutil short (context context, charsequence message) {
  if (toast == null || (toastview!=null&&toastview.getchildcount ()>1)) {
    toast=toast.maketext (context, message, toast.length_short);
    toastview=null;
  } else {
    toast.settext (message);
    toast.setduration (toast.length_short);
  }
  return this;
}
/**
 * Short time display toast
 * /
public toastutil short (context context, int message) {
  if (toast == null || (toastview!=null&&toastview.getchildcount ()>1)) {
    toast=toast.maketext (context, message, toast.length_short);
    toastview=null;
  } else {
    toast.settext (message);
    toast.setduration (toast.length_short);
  }
 return this;
}
/**
 * Display toast for a long time
 * /
public toastutil long (context context, charsequence message) {
  if (toast == null || (toastview!=null&&toastview.getchildcount ()>1)) {
    toast=toast.maketext (context, message, toast.length_long);
    toastview=null;
  } else {
    toast.settext (message);
    toast.setduration (toast.length_long);
  }
  return this;
}
/**
 * Display toast for a long time
 *
 * @param context
 * @param message
 * /
public toastutil long (context context, int message) {
  if (toast == null || (toastview!=null&&toastview.getchildcount ()>1)) {
    toast=toast.maketext (context, message, toast.length_long);
    toastview=null;
  } else {
    toast.settext (message);
    toast.setduration (toast.length_long);
  }
  return this;
}
/**
 * Custom display toast time
 *
 * @param context
 * @param message
 * @param duration
 * /
public toastutil indefinite (context context, charsequence message, int duration) {
  if (toast == null || (toastview!=null&&toastview.getchildcount ()>1)) {
    toast=toast.maketext (context, message, duration);
    toastview=null;
  } else {
    toast.settext (message);
    toast.setduration (duration);
  }
   return this;
}
/**
 * Custom display toast time
 *
 * @param context
 * @param message
 * @param duration
 * /
public toastutil indefinite (context context, int message, int duration) {
  if (toast == null || (toastview!=null&&toastview.getchildcount ()>1)) {
    toast=toast.maketext (context, message, duration);
    toastview=null;
  } else {
    toast.settext (message);
    toast.setduration (duration);
  }
  return this;
}
/**
 * Show toast
 * @return
 * /
public toastutil show () {
  toast.show ();
  return this;
}
/**
 * Get toast
 * @return
 * /
public toast gettoast () {
  return toast;
}
}

The method of modifying the toast background color is as follows:

toastutil toastutil=new toastutil ();
toastutil.short (mainactivity.this, "Custom message font, background color"). settoastcolor (color.white, getresources (). getcolor (r.color.coloraccent)). show ();

Modify the toast background color

The square toast looks a bit rigid,I customized a file calledtoast_radius.xmlbackground, the code is as follows:

<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-Filled color->
<solid android:color="#ffc107" />
<!-Android:radius radius of the arc->
<corners android:radius="20dip" />
</shape>

Then change the above background code to:

toastutil.short (mainactivity.this, "Custom message font color and background"). settoastbackground (color.white, r.drawable.toast_radius) .show ();

Modified background toast

Although the official thinks that both toast and snackbar should be in the form of short text,Cannot contain icons,However, I feel that it is quite fun to add icons...

To add an icon to toast:

imageview toastimage=new imageview (getapplicationcontext ());
 toastimage.setimageresource (r.mipmap.ic_launcher);
 toastutil.short (mainactivity.this, "Added an imageview to toast"). settoastbackground (color.white, r.drawable.toast_radius) .addview (toastimage, 0) .show ();

Add icon toast

If i want toast to display a custom layout,Can be like this:

view view=layoutinflater.from (mainactivity.this) .inflate (r.layout.image, null);
 new toastutil (mainactivity.this, view, toast.length_short) .show ();
Custom layout toast, my layout file has only one imageview with default icon

everybody knows,that continuously triggers toast show () method,toast will display continuously in line,Feels less friendly.So I first judged whether the toast was not created or whether an extra view was added. If so, a toast object is regenerated;if not, only the message text and display time are modified.

toast layout modification,Do not line up

to sum up

  • Previous Python + django for simple file upload
  • Next Talking about garbage collection strategy in jvm
  • Trends