Android Custom Toast

Source: Internet
Author: User

When developing Android apps, we usually use toast to pop up the message, which is simple and efficient. But the location and style of the toast on different phones may be different, and the system's own toast style is ugly (personally think ...).  ), then how to customize a personalized Toast prompt box ... Today I would like to share the custom toast I wrote, the shortcomings are also please give us a lot of advice. (There are behind)

1. Because of the nature of toast, we define toast as a singleton pattern.

  Private StaticZtoast instance;//single-Case    PrivateView Mtoastview;//Customizing Toast View    PrivateTextView Mtextview; PrivateBoolean misshow;//record whether the status is displayed    PrivateTimer Mtimer;//Timer     Public synchronized StaticZtoast getinstance (context context) {if(Instance = =NULL) Instance=Newztoast (context); returninstance; }        PrivateZtoast (Context context) {Misshow=false;//records whether the contents of the current toast are already displayed//here to initialize toast ViewMtoastview = Layoutinflater.from (context). Inflate (R.layout.common_toast,NULL); //text to promptMtextview =( (TextView) Mtoastview.findviewbyid (R.id.toast_text)); //Initializing countersMtimer =NewTimer (); //Setting Layout ParametersSetParams (); }        

2. Then set the layout style:

    PrivateLayoutparams Mparams; Private voidSetParams () {Mparams=Newwindowmanager.layoutparams ();//Initialize Mparams.height=WindowManager.LayoutParams.WRAP_CONTENT; Hi mparams.width=WindowManager.LayoutParams.WRAP_CONTENT; Wide Mparams.format=pixelformat.translucent; Mparams.windowanimations= R.style.custom_animation_toast;//Set entry exit animation effectMparams.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; On its way mparams.y= 45; Bottom spacing}

3. Customize the effect of toast pop-up style animations. Toast_styles.xml

<?XML version= "1.0" encoding= "Utf-8"?><Resources>    <stylename= "Custom.animation.toast"Parent= "@android: Style/animation.toast">        <Itemname= "Android:windowenteranimation">@anim/toast_enter</Item>        <Itemname= "Android:windowexitanimation">@anim/toast_exit</Item>    </style></Resources>
Toast_enter.xml
<?XML version= "1.0" encoding= "Utf-8"?><Setxmlns:android= "Http://schemas.android.com/apk/res/android" >    <Translateandroid:duration= "1"Android:fromxdelta= "0"Android:fromydelta= "0"Android:toxdelta= "0"Android:toydelta= "$" />    <Translateandroid:duration= " the"Android:fillafter= "true"Android:fromxdelta= "0"Android:fromydelta= "0"Android:interpolator= "@interpolator/accelerate_quad"Android:toxdelta= "0"Android:toydelta= " -105" />    <Alphaandroid:duration= "+"Android:fromalpha= "0"Android:toalpha= "1" />    <Translateandroid:duration= "a"Android:fillafter= "true"Android:fromxdelta= "0"Android:fromydelta= "0"Android:startoffset= " the"Android:toxdelta= "0"Android:toydelta= " the" /></Set>
Toast_exit.xml

<?XML version= "1.0" encoding= "Utf-8"?><Setxmlns:android= "Http://schemas.android.com/apk/res/android" >    <Translateandroid:duration= "$"Android:fromydelta= "0"Android:interpolator= "@interpolator/accelerate_quad"Android:toydelta= "50%p" />    <Alphaandroid:duration= "$"Android:fromalpha= "1.0"Android:toalpha= "0.0" /></Set>

4, Common_toast.xml

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:background= "@drawable/bg_common_toast"android:orientation= "Horizontal" >    <TextViewAndroid: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>

Above, this gives a custom view container to drive the picture effect. Then, how to invoke it? Don't worry, we need to write another way.

     Public voidShow (String text,intmshowtime) {        if(misshow) {//If the toast is already displayed, it's hidden.            if(MANAGEAPP.MWDM! =NULL&& Mtoastview! =NULL) ManageApp.mWdm.removeView (Mtoastview); //Cancel Timer            if(Mtimer! =NULL) {mtimer.cancel (); Mtimer=NewTimer (); }        }        //Setting display contentmtextview.settext (text); //Set Display statusMisshow =true; //load it onto the WindowManagerManageApp.mWdm.addView (Mtoastview, mparams); //Setting TimersMtimer.schedule (NewTimerTask () {@Override Public voidrun () {ManageApp.mWdm.removeView (Mtoastview); Misshow=false; }        }, (Long) (Mshowtime = = Toast.length_long? 2,200:1200)); }

People will ask MWDM is a god horse? In fact, it is windowmanager (public static WindowManager MWDM;), eventually you need to use it to view the screen display. We define it in the application class of the program and initialize MWDM = (WindowManager) getsystemservice (Context.window_service) in OnCreate (); This will ensure that his life cycle will be longer than our activity, so that it does not report a variety of anomalies when executing a timer. (if there are other better ways, hope to inform.) )

Then, in other classes, use the

Ztoast.getinstance (Mcontext). Show ("I am a custom Toast", Toast.length_long);

The effect is as follows:

Android Custom Toast

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: info-contact@alibabacloud.com 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.