How to define your own toast (toast) in Android

Source: Internet
Author: User

Android system inside there is a thing very good, also very commonly used, that is toast, but long-term use will also find that Toast has his shortcomings: the form of a single, only text, style unchanged and so on, then how to customize a Toast it, We can start by analyzing the code of the Android definition toast:

Toast's Maketext method :




The real thing about Android here is that the text and duration of the toast display is set a bit, and then the toast object is returned to execute the show () method. The core of this place is to figure out


This sentence Android does some work, below we continue to look:



This is actually the new TN () object, and then sets some parameters about the position and offset of the TN object . So where's the TN? We can continue to look down:




We can see that a Handleshow method is executed inside the TN constructor.




We can find handleshow This method executes a lot of statements, but in fact we need to focus on only one sentence, that is, the above selected sentence:Mwm.addview (mview,mparams);


in fact, this MWM is a windowmanager,mview is the view of the component to be displayed, and mparams should be the setting of the parameter, but what is the specific divine? We can find a member variable in the Toast class :


we found that Mparams is actually a layout parameter for WindowManager. But if the settings of mparams are still too messy to see from the Handleshow method, we can find the following paragraph in the toast :




Here we can see the mparams part of the main parameter setting, which contains the length of the width is wrap_content, then the translucent (translucent), and then set up a The animation effect (Animation_toast), and a caption (Settitle ("toast")) finally set up some of the Toast's identity such as making the screen light, not touching, not getting focus , and so on.


Here, we basically know how the toast is displayed , the core is to use a WindowManager AddView method, the view and the parameters to be displayed Mparams passed in, However, it is necessary to set some parameters of Mparams before this.


So here is the show, how does the toAST make the display disappear ? We can find a Handhide method under the Handleshow method:




The same core statement is also the selected sentence,mwm.removeview (MView), so the toast disappears


So with some of the above analysis, it's not hard to write our own toast class, which provides static methods that let us define the display style and display content of the toast, and control when the toast's display starts and ends.

The following custom toast class, which provides several forms of the show method, can pass in text like a traditional toast, display a testview, or define the incoming view object and the Mparams parameter entirely yourself, so that you can define a toast that is completely defined by itself


Package Com.alexchen.mobilesafeexercise.ui;import Android.content.context;import Android.graphics.PixelFormat; Import Android.view.view;import Android.view.windowmanager;import Android.widget.textview;import android.widget.toast;/** * Custom Toast * * @author Alex * */public class Mytoast {/** * form manager */private Static WindowManager W M;private static Windowmanager.layoutparams mparams = new Windowmanager.layoutparams ();p rivate static View MView; private static TextView tv;/** * Display Custom Toast * * @param info * @param context */public static void show (String message, Conte XT context) {wm = (WindowManager) context.getsystemservice (context.window_service); TV = new TextView (context); Tv.settext (message); Tv.settextsize (20);//The work done by tn windowmanager.layoutparams params = Mparams;params.height = Windowmanager.layoutparams.wrap_content;params.width = Windowmanager.layoutparams.wrap_content;params.format = Pixelformat.translucent;params.type = Windowmanager.layoutparams.type_toast;params.settitle ("TOAST");p arAms.flags = windowmanager.layoutparams.flag_keep_screen_on| windowmanager.layoutparams.flag_not_focusable| Windowmanager.layoutparams.flag_not_touchable;wm.addview (TV, params);}  /** * Custom Text toast Display method, this method provides more custom content, such as TextView can apply an ID of an incoming style * * @param message * @param context * @param textviewresid * @param params */public static void show (String message, context context, int textviewresid,windowmanager.layoutparams p Arams) {wm = (WindowManager) context.getsystemservice (context.window_service); TV = new TextView (context);// Added a style tv.settextappearance (context, textviewresid);//original TN work//Windowmanager.layoutparams params = mparams;// Params.height = windowmanager.layoutparams.wrap_content;//Params.width = windowmanager.layoutparams.wrap_content;/ /Params.format = pixelformat.translucent;//Params.type = windowmanager.layoutparams.type_toast;//Params.setTitle (" Toast ");//params.flags = windowmanager.layoutparams.flag_keep_screen_on//| windowmanager.layoutparams.flag_not_focusable//| Windowmanager.layoutparams.flag_not_touchable;wm.addview (TV, params);} /** * More custom forms, you can directly pass in any of your own defined view, set the parameters of WM * * @param view * @param context * @param params * windowmanager. Parameters of the Layoutparams type, windowmanager.layoutparams * mparams = new Windowmanager.layoutparams (); params.height = * WindowManager.LayoutParams.WRAP_CONTENT; params.width = * WindowManager.LayoutParams.WRAP_CONTENT; Params.format = * Pixelformat.translucent; Params.type = * WindowManager.LayoutParams.TYPE_TOAST; * Params.settitle ("Toast"); Params.flags = * WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | * WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | * WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE; */public static void Show (View view, Context Context,windowmanager.layoutparams params) {wm = (WindowManager) context.get Systemservice (Context.window_service); Mytoast.mview = View;wm.addview (MView, params);}/** * Hide Custom toast It's important to remember to make a blank here, because when you do not call, both volumes should be empty */public static void Hide () {if (wm! = NULL) {if (TV!!) {Wm.removevi EW (TV); TV = null;} if (MView! = null) {Wm.removeview (mView); mView = null;} if (TV = = NULL && MView = = null) {WM = NULL;}}}



How to define your own toast (toast) in Android

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.