Android Practical Toast Tool Class Encapsulation

  • 2021-10-15 11:31:55
  • OfStack

Hello, everyone, as we all know, Toast will disappear automatically after displaying for 1 period of time, so we can't get the focus. But there are some problems in use:

1) When a new Toast needs to be popped up, the previous Toast has not been displayed completely

2) The same message may be popped up repeatedly

3) What are the specific uses of Toast that are not very familiar, and when you use it, you can export it to find it

4) app has quit, and Toast is still playing

Wait a series of problems

A tool class is encapsulated below to help you manage Toast, which can basically meet the common needs. If it can't be met, it will be customized, hehe ~


import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
 
/**
 * Created by wangwentao on 2017/1/25.
 * Toast Unified 1 Management class 
 */
 
public class ToastUtil {
 private static boolean isShow = true;// Default display 
 private static Toast mToast = null;// Global only 1 Adj. Toast
 
/**
 *private Control should not be instantiated */
 private ToastUtil() {
  throw new UnsupportedOperationException(" Cannot be instantiated ");
 }
 
 /**
  *  Whether global control displays Toast
  * @param isShowToast
  */
 public static void controlShow(boolean isShowToast){
  isShow = isShowToast;
 }
 
 /**
  *  Cancel Toast Display 
  */
 public void cancelToast() {
  if(isShow && mToast != null){
   mToast.cancel();
  }
 }
 
 /**
  *  Short time display Toast
  *
  * @param context
  * @param message
  */
 public static void showShort(Context context, CharSequence message) {
  if (isShow){
   if (mToast == null) {
    mToast = Toast.makeText(context, message, Toast.LENGTH_SHORT);
   } else {
    mToast.setText(message);
   }
   mToast.show();
  }
 }
 
 /**
  *  Short time display Toast
  *
  * @param context
  * @param resId  Resources ID:getResources().getString(R.string.xxxxxx);
  */
 public static void showShort(Context context, int resId) {
  if (isShow){
   if (mToast == null) {
    mToast = Toast.makeText(context, resId, Toast.LENGTH_SHORT);
   } else {
    mToast.setText(resId);
   }
   mToast.show();
  }
 }
 
 /**
  *  Display for a long time Toast
  *
  * @param context
  * @param message
  */
 public static void showLong(Context context, CharSequence message) {
  if (isShow){
   if (mToast == null) {
    mToast = Toast.makeText(context, message, Toast.LENGTH_LONG);
   } else {
    mToast.setText(message);
   }
   mToast.show();
  }
 }
 
 /**
  *  Display for a long time Toast
  *
  * @param context
  * @param resId  Resources ID:getResources().getString(R.string.xxxxxx);
  */
 public static void showLong(Context context, int resId) {
  if (isShow){
   if (mToast == null) {
    mToast = Toast.makeText(context, resId, Toast.LENGTH_LONG);
   } else {
    mToast.setText(resId);
   }
   mToast.show();
  }
 }
 
 /**
  *  Custom display Toast Time 
  *
  * @param context
  * @param message
  * @param duration  Unit : Milliseconds 
  */
 public static void show(Context context, CharSequence message, int duration) {
  if (isShow){
   if (mToast == null) {
    mToast = Toast.makeText(context, message, duration);
   } else {
    mToast.setText(message);
   }
   mToast.show();
  }
 }
 
 /**
  *  Custom display Toast Time 
  *
  * @param context
  * @param resId  Resources ID:getResources().getString(R.string.xxxxxx);
  * @param duration  Unit : Milliseconds 
  */
 public static void show(Context context, int resId, int duration) {
  if (isShow){
   if (mToast == null) {
    mToast = Toast.makeText(context, resId, duration);
   } else {
    mToast.setText(resId);
   }
   mToast.show();
  }
 }
 
 /**
  *  Customize Toast Adj. View
  * @param context
  * @param message
  * @param duration  Unit : Milliseconds 
  * @param view  Display your own View
  */
 public static void customToastView(Context context, CharSequence message, int duration,View view) {
  if (isShow){
   if (mToast == null) {
    mToast = Toast.makeText(context, message, duration);
   } else {
    mToast.setText(message);
   }
   if(view != null){
    mToast.setView(view);
   }
   mToast.show();
  }
 }
 
 /**
  *  Customize Toast Location of 
  * @param context
  * @param message
  * @param duration  Unit : Milliseconds 
  * @param gravity
  * @param xOffset
  * @param yOffset
  */
 public static void customToastGravity(Context context, CharSequence message, int duration,int gravity, int xOffset, int yOffset) {
  if (isShow){
   if (mToast == null) {
    mToast = Toast.makeText(context, message, duration);
   } else {
    mToast.setText(message);
   }
   mToast.setGravity(gravity, xOffset, yOffset);
   mToast.show();
  }
 }
 
 /**
  *  Custom with pictures and text Toast The final result is that there are pictures on the top and words on the bottom 
  * @param context
  * @param message
  * @param iconResId  Resources for pictures id, Such as :R.drawable.icon
  * @param duration
  * @param gravity
  * @param xOffset
  * @param yOffset
  */
 public static void showToastWithImageAndText(Context context, CharSequence message, int iconResId,int duration,int gravity, int xOffset, int yOffset) {
  if (isShow){
   if (mToast == null) {
    mToast = Toast.makeText(context, message, duration);
   } else {
    mToast.setText(message);
   }
   mToast.setGravity(gravity, xOffset, yOffset);
   LinearLayout toastView = (LinearLayout) mToast.getView();
   ImageView imageView = new ImageView(context);
   imageView.setImageResource(iconResId);
   toastView.addView(imageView, 0);
   mToast.show();
  }
 }
 
 /**
  *  Customize Toast, For type CharSequence
  * @param context
  * @param message
  * @param duration
  * @param view
  * @param isGravity true, Represents the following 3 Layout parameters take effect ,false, Indicates that it is not effective 
  * @param gravity
  * @param xOffset
  * @param yOffset
  * @param isMargin true, Indicates that the next two parameters take effect, false, Indicates that it is not effective 
  * @param horizontalMargin
  * @param verticalMargin
  */
 public static void customToastAll(Context context, CharSequence message, int duration,View view,boolean isGravity,int gravity, int xOffset, int yOffset,boolean isMargin,float horizontalMargin, float verticalMargin) {
  if (isShow){
   if (mToast == null) {
    mToast = Toast.makeText(context, message, duration);
   } else {
    mToast.setText(message);
   }
   if(view != null){
    mToast.setView(view);
   }
   if(isMargin){
    mToast.setMargin(horizontalMargin, verticalMargin);
   }
   if(isGravity){
    mToast.setGravity(gravity, xOffset, yOffset);
   }
   mToast.show();
  }
 }
 
 /**
  *  Customize Toast, For type resId
  * @param context
  * @param resId
  * @param duration
  * @param view : It should be 1 Layout, which contains the contents set by yourself 
  * @param isGravity true, Represents the following 3 Layout parameters take effect ,false, Indicates that it is not effective 
  * @param gravity
  * @param xOffset
  * @param yOffset
  * @param isMargin true, Indicates that the next two parameters take effect, false, Indicates that it is not effective 
  * @param horizontalMargin
  * @param verticalMargin
  */
 public static void customToastAll(Context context, int resId, int duration,View view,boolean isGravity,int gravity, int xOffset, int yOffset,boolean isMargin,float horizontalMargin, float verticalMargin) {
  if (isShow){
   if (mToast == null) {
    mToast = Toast.makeText(context, resId, duration);
   } else {
    mToast.setText(resId);
   }
   if(view != null){
    mToast.setView(view);
   }
   if(isMargin){
    mToast.setMargin(horizontalMargin, verticalMargin);
   }
   if(isGravity){
    mToast.setGravity(gravity, xOffset, yOffset);
   }
   mToast.show();
  }
 }
}

Finally, let me tell you a little secret:

1) The bottom layer of Toast uses handler mechanism, which controls whether Toast is displayed or hidden by post1 nShow and 1 mHide respectively

2) The view of Toast is loaded via addView of WindowManager

3) First instantiate an Toast through makeText (), then call the toast. Show () method. Instead of displaying Toast immediately, instantiate an TN variable, and then add it to the service queue through service. enqueueToast () to wait for display. In TN, control the display format of Toast and the methods of hide () and show () to control the appearance and disappearance of Toast. It is emphasized that this queue is maintained by the system and we cannot interfere.

4) The time parameters LONG_DELAY and SHORT_DELAY that we often pass are 3.5 seconds and 2 seconds respectively

5) What if we want to achieve an effect similar to Toast ourselves?

You can use windowManager to add view to achieve the desired effect

All right, that's it!


Related articles: