Android Custom Toast-Popup message

Source: Internet
Author: User

Android Custom Toast-Popup message

Implementation method:

1.new a toast instance toast.

2. Customize a View instance view that is displayed.

3. Set Toast.setview (view), Toast.setduration (Toast.length_long) to display the message time

4. Avoid the wrong operation always repeated pop-up message processing, defining a toast's global variables to avoid repeated instantiation of control

Here is the code

 PackageCom.android.hexiang.otptoken;Importandroid.view.Gravity;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.content.Context;ImportAndroid.widget.TextView;ImportAndroid.widget.Toast;ImportCOM.ANDROID.HEXIANG.OTPTOKEN.R;/*** Created by Hexiang on 2014/12/10.*/ Public classMytoast {Private StaticToast Mtoast =NULL; /*** Custom message box *@paramc * Created by Hexiang on 2014/12/10. */@SuppressWarnings ("Deprecation")    Private StaticToast Showcustomertoast (FinalContext c,string msg,intduration) {        //gets the Layoutinflater object that can convert the layout file to a consistent view objectLayoutinflater inflater=Layoutinflater.from (c); //convert a layout file to the appropriate view objectView Layout=inflater.inflate (R.layout.my_customer_toast_layout,NULL);        Layout.setbackgrounddrawable (C.getresources (). getdrawable (R.drawable.back_toast_style)); //find TextView objects by ID from layoutTextView textview=(TextView) Layout.findviewbyid (r.id.txt_msg); //set the text content of TextViewTextview.settext (msg); //instantiate a Toast objectToast toast=NewToast (C.getapplicationcontext ());        Toast.setduration (duration); Toast.setgravity (Gravity.bottom| Gravity.center_horizontal, 0, 160);        Toast.setview (layout); returnToast; }    /*** Long message box refers to the length of time * Created by Hexiang on 2014/12/10. * @paramC Context *@paramMSG Message Content*/@SuppressWarnings ("Deprecation")     Public Static voidShowcustomertoastlong (FinalContext c,string msg) {   //avoid repeating pop-up messages        if(Mtoast = =NULL) {Mtoast=Showcustomertoast (C,msg,toast.length_long); }        Else{TextView txt_msg=(TextView) Mtoast.getview (). Findviewbyid (R.ID.TXT_MSG);            Txt_msg.settext (msg);        Mtoast.setduration (Toast.length_long);    } mtoast.show (); }    /*** Short message box refers to the length of time mtoast.setduration (Toast.length_short); * @paramC Context *@paramMSG Message content * Created by Hexiang on 2014/12/10. */@SuppressWarnings ("Deprecation")     Public Static voidShowcustomertoastshot (FinalContext c,string msg) {   //avoid repeating pop-up messages        if(Mtoast = =NULL) {Mtoast=Showcustomertoast (C,msg,toast.length_short); }        Else{TextView txt_msg=(TextView) Mtoast.getview (). Findviewbyid (R.ID.TXT_MSG);            Txt_msg.settext (msg);        Mtoast.setduration (Toast.length_short);    } mtoast.show (); }}

The following is an XML file

1 <?XML version= "1.0" encoding= "Utf-8"?>2 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"3 Android:layout_width= "Match_parent"4 Android:layout_height= "Match_parent"5 Android:id= "@+id/toast_layout_root"6 android:orientation= "vertical" >7 8   <TextView9      Android:layout_margin= "6DP" Ten android:layout_gravity= "Center_vertical|center_horizontal" One Android:id= "@+id/txt_msg" A Android:layout_width= "Match_parent" - Android:textcolor= "@color/white_light" - android:textsize= "14SP" the Android:text= "This is a test toast" - Android:layout_height= "Wrap_content" -   /> - </LinearLayout>

The following is a custom gradient background

<?XML version= "1.0" encoding= "Utf-8"?><Shapexmlns:android= "Http://schemas.android.com/apk/res/android" >      <GradientAndroid:angle= "315"Android:centerx= "0.5"Android:centery= "0.5"Android:centercolor= "#fcdede"Android:endcolor= "#e5edbe"Android:startcolor= "#d2b2b2" />    <CornersAndroid:radius= "3DP" /></Shape>

Reviewing the use of Android:shape
Gradient: Gradient
Android:startcolor and Android:endcolor are start and end colors respectively, Android:centercolor middle part color
Ndroid:angle is a gradient angle and must be an integer multiple of 45.

Finally, call

1 mytoast.showcustomertoastlong (This, "Test message box");

Finished, this article declined reprint, deadweight. Program Ape is not easy ...

Created by Hexiang on 2014/12/10. Clear points remain

Android Custom Toast-Popup message

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: 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.