Android custom toast and androidtoast

Source: Internet
Author: User

Android custom toast and androidtoast

After a long time, I suddenly wanted to write a blog post .. Haha ......

Okay, let's talk about things...

When developing Android applications, we usually use toast to pop up the prompt message, which is simple and efficient. However, the positions and styles displayed by toast on different mobile phones may be different, and the built-in toast style of the system may be ugly (I personally think ...), so how to customize a customized toast prompt box... Today, I will share my own custom toast. For more information about the shortcomings, see. (Later)


1. Because of toast features, we define toast as the singleton mode.

Private static ZToast instance; // private View mToastView of a single instance; // custom toast view private TextView mTextView; private Boolean mIsShow; // whether the record status is private Timer mTimer; // timer public synchronized static ZToast getInstance (Context context) {if (instance = null) instance = new ZToast (context); return instance;} private ZToast (Context context) {mIsShow = false; // record whether the current Toast content is displayed. // initialize toast view mToastView = LayoutInflater here. from (context ). inflate (R. layout. common_toast, null); // The text mTextView = (TextView) mToastView to be prompted. findViewById (R. id. toast_text); // initialize the counter mTimer = new Timer (); // set the layout parameter setParams ();}

2. Set the layout style:

Private LayoutParams mParams; private void setParams () {mParams = new WindowManager. layoutParams (); // initialize mParams. height = WindowManager. layoutParams. WRAP_CONTENT; // high mParams. width = WindowManager. layoutParams. WRAP_CONTENT; // wide mParams. format = PixelFormat. TRANSLUCENT; mParams. windowAnimations = R. style. custom_animation_toast; // set to exit the animation mParams. type = WindowManager. layoutParams. TYPE_TOAST; mParams. flags = WindowManager. layoutParams. FLAG_KEEP_SCREEN_ON | WindowManager. layoutParams. FLAG_NOT_FOCUSABLE | WindowManager. layoutParams. FLAG_NOT_TOUCHABLE; mParams. gravity = Gravity. BOTTOM; // The method mParams. y = 45; // bottom spacing}


3. Customize the effect of the toast Pop-up style animation. Toast_styles.xml

<?xml version="1.0" encoding="utf-8"?><resources>    <style name="custom.animation.toast" parent="@android:style/Animation.Toast">        <item name="android:windowEnterAnimation">@anim/toast_enter</item>        <item name="android:windowExitAnimation">@anim/toast_exit</item>    </style></resources>
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="" >    <translate        android:duration="1"        android:fromXDelta="0"        android:fromYDelta="0"        android:toXDelta="0"        android:toYDelta="85" />    <translate        android:duration="350"        android:fillAfter="true"        android:fromXDelta="0"        android:fromYDelta="0"        android:interpolator="@interpolator/accelerate_quad"        android:toXDelta="0"        android:toYDelta="-105" />    <alpha        android:duration="100"        android:fromAlpha="0"        android:toAlpha="1" />    <translate        android:duration="80"        android:fillAfter="true"        android:fromXDelta="0"        android:fromYDelta="0"        android:startOffset="350"        android:toXDelta="0"        android:toYDelta="20" /></set>

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="" >    <translate        android:duration="500"        android:fromYDelta="0"        android:interpolator="@interpolator/accelerate_quad"        android:toYDelta="50%p" />    <alpha        android:duration="500"        android:fromAlpha="1.0"        android:toAlpha="0.0" /></set>



4. common_toast.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=""    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:background="@drawable/bg_common_toast"    android:orientation="horizontal" >    <TextView        android:id="@+id/toast_text"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center_vertical"        android:gravity="left|center"        android:textColor="@android:color/black"        android:textSize="@dimen/toast_font_size" /></LinearLayout>


In this way, a custom view container with animation effect is obtained. Then, how can we call it? Don't worry. We need to write another method ..

Public void show (String text, int mShowTime) {if (mIsShow) {// if Toast is already displayed, hide if (ManageApp. mWdm! = Null & mToastView! = Null) ManageApp. mWdm. removeView (mToastView); // cancel the timer if (mTimer! = Null) {mTimer. cancel (); mTimer = new Timer () ;}// you can specify mTextView. setText (text); // set the display status mIsShow = true; // load it to ManageApp on windowManager. mWdm. addView (mToastView, mParams); // sets the timer mTimer. schedule (new TimerTask () {@ Override public void run () {ManageApp. mWdm. removeView (mToastView); mIsShow = false ;}}, (long) (mShowTime = Toast. LENGTH_LONG? (2200: 1200 ));}

Will you ask if mWdm is a magic horse? In fact, it is WindowManager (public static WindowManager mWdm;). You still need to use it to display the view on the screen. We define it in the Application class of the program, and initialize mWdm = (WindowManager) getSystemService (Context. WINDOW_SERVICE); in this way, we can ensure that his life cycle is longer than our activity, so that no exception is reported when the timer is executed. (If there are other better methods, please let us know .)


If you want to upload a gif image, you will find that the gif animation effect will not be done .... Forget it ..


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.