Android Custom Toast

Source: Internet
Author: User

1. Initialize the project: mainly a few buttons, respectively, the display of destruction and mobile methods.

<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "Horizontal"Android:weightsum= "3">    <ButtonAndroid:id= "@+id/btn_dispaly"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_weight= "1"Android:text= "Display" />    <ButtonAndroid:id= "@+id/btn_gone"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_weight= "1"Android:text= "Gone" />        <ButtonAndroid:id= "@+id/btn_move"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_weight= "1"Android:text= "Move" /></LinearLayout>
Layout.xml
 Public classMainactivityextendsActivityImplementsonclicklistener{/*** Button Display*/    PrivateButton Btndisplay; /*** Button Gone*/    PrivateButton Btngone; /*** Button Move*/    PrivateButton Btnmove; /*** Def Toast class*/    PrivateMytoast Mytoast; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);                Setcontentview (R.layout.activity_main);    Init (); }    Private voidinit () {Btndisplay= (Button) This. Findviewbyid (R.id.btn_dispaly); Btndisplay.setonclicklistener ( This); Btngone= (Button) This. Findviewbyid (R.id.btn_gone); Btngone.setonclicklistener ( This); Btnmove= (Button) This. Findviewbyid (R.id.btn_move); Btnmove.setonclicklistener ( This); Mytoast=NewMytoast ( This); } @Override Public voidOnClick (View v) {if(V.getid () = =r.id.btn_dispaly) {Mytoast.simpledisplay ("Show toast for Test"); }                if(V.getid () = =r.id.btn_move) {Mytoast.movetoast ("Move toast for test"); }                if(V.getid () = =R.id.btn_gone)        {mytoast.destorytoast (); }    }}
mainactivity

2. Define the Toast class: Mytoast

 Public classMytoast {/*** View Inflater*/    PrivateLayoutinflater Inflater =NULL; /*** Prop window for toast*/    PrivateWindowManager Windowmanage; /*** Toast Show View*/    PrivateView view =NULL; /*** Toast Show params*/    PrivateWindowmanager.layoutparams params =NULL; /**     *      * @paramContext*/     PublicMytoast (Context context) {Inflater=Layoutinflater.from (context); Params=NewWindowmanager.layoutparams (); Windowmanage=(WindowManager) Context.getsystemservice (Context.window_service); }    /*** Toast display and moveable *@paramtext*/     Public voidmovetoast (String text) {checkparamsenv (text); //Inflate ViewView = Inflater.inflate (R.layout.activity_toast,NULL); //init toast ViewTextView Tvdesc =(TextView) View.findviewbyid (R.ID.TV_TOAST_DESC);                Tvdesc.settext (text); //Set Display locationParams.gravity =Gravity.center; Params.width=WindowManager.LayoutParams.WRAP_CONTENT; Params.height=WindowManager.LayoutParams.WRAP_CONTENT; //add touch Listener for MoveView.setontouchlistener (NewOntouchlistener () {intinitx,inity; @Override Public BooleanOnTouch (View V, motionevent event) {Switch(Event.getaction ()) {//Touch Down                 CaseMotionEvent.ACTION_DOWN:initX= (int) event.getrawx (); Inity= (int) Event.getrawy ();  Break; //Touch Move                 CaseMotionevent.action_move:intNewx = (int) event.getrawx (); intNewy = (int) Event.getrawy (); intDX = newx-Initx; intDY = newy-inity; Params.x+=DX; Params.y+=dy;                                        Windowmanage.updateviewlayout (view, params); INITX= (int) event.getrawx (); Inity= (int) Event.getrawy ();  Break; //Touch up                 Casemotionevent.action_up: Break; default:                     Break; }                return false;                }        }); //set params flags for keep screens on and no focusableParams.flags =WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON; Params.format=pixelformat.translucent; //Toast Display TypeParams.type =WindowManager.LayoutParams.TYPE_TOAST; //Pop windowWindowmanage.addview (view, params); }    /*** Simple Toast Show *@paramtext*/     Public voidSimpledisplay (String text) {checkparamsenv (text); View= Inflater.inflate (R.layout.activity_toast,NULL); TextView Tvdesc=(TextView) View.findviewbyid (R.ID.TV_TOAST_DESC);        Tvdesc.settext (text); Params.gravity=Gravity.center; Params.width=WindowManager.LayoutParams.WRAP_CONTENT; Params.height=WindowManager.LayoutParams.WRAP_CONTENT; Params.flags=WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON|WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE; Params.format=pixelformat.translucent; Params.type=WindowManager.LayoutParams.TYPE_TOAST;    Windowmanage.addview (view, params); }     Public voidDestorytoast () {if(View! =NULL) {Windowmanage.removeview (view); }    }    Private voidcheckparamsenv (String text) {if(Text = =NULL|| "". Equals (text)) {            return; }    }}
Mytoast

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.