Android Toast realizes full screen display

  • 2021-10-13 08:42:13
  • OfStack

This article shares the specific code of Android Toast full screen display for your reference, the specific contents are as follows

Don't talk nonsense, just go to the code:


private void toastFullScreen(){
 Toast toast = Toast.makeText(this, null, Toast.LENGTH_LONG * 10 * 1000);
 toast.setGravity(Gravity.CENTER, 0, 0);
 LinearLayout toastView = (LinearLayout)toast.getView();
 
 // Get the screen size with unit pixels.
 WindowManager wm = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
 DisplayMetrics outMetrics = new DisplayMetrics();
 wm.getDefaultDisplay().getMetrics(outMetrics);
 
 TextView tv = new TextView(this);
 LayoutParams vlp = new LayoutParams(outMetrics.widthPixels,
               outMetrics.heightPixels); 
 vlp.setMargins(0, 0, 0, 0);
 tv.setLayoutParams(vlp);
 tv.setText("Hello Toast! I am full screen now.");
 tv.setGravity(Gravity.CENTER);
 
 toastView.addView(tv);
 toast.show();
}

According to the actual situation, adding different view in toastView can display different pop-up windows, hoping to help everyone!

Another section of custom toast full-screen display implementation code:


public class MyToast {

  private static Toast mGoodToast;
  private static ObjectAnimator mObjectAnimator;

  public static void showGoodToast(Context context) {

    if (mGoodToast == null) {
      mGoodToast = new Toast(context);
      mGoodToast.setGravity(Gravity.CENTER, 0, 0);
      mGoodToast.setDuration(Toast.LENGTH_LONG);
      View view = LayoutInflater.from(context).inflate(R.layout.dialog_good, null, false);
      AppCompatImageView imageView = view.findViewById(R.id.shine);
      mObjectAnimator = ObjectAnimator.ofFloat(imageView, "rotation", 5000);
      mObjectAnimator.setInterpolator(new LinearInterpolator());
      mObjectAnimator.setDuration(30000);
      mGoodToast.setGravity(Gravity.FILL, 0, 0);
      mGoodToast.setView(view);
      mGoodToast.getView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);// Settings Toast It can be laid out below the system status bar 
    }

    if (!mObjectAnimator.isRunning()) {
      mObjectAnimator.start();
    }
    mGoodToast.show();
  }

}

Related articles: