Android with picture toast (custom toast)

Source: Internet
Author: User

Use the Android default toast

Toast Introduction:

A toast is a simple message display box that can appear briefly in a location on the screen and display a prompt message.

The default location is the lower middle of the screen, and the general toast is used as follows:

Toast.maketext (This, "1222222", Toast.length_short). Show ();

Toast is a static class with static modifiers, meaning it can be used directly, so you can call the Maketext method directly without creating an object.

This method requires three parameters to be passed in:

   /*** Make a standard toast, that just contains a text view. *     * @paramContext the context to use . Usually your {@linkAndroid.app.Application} * or {@linkandroid.app.Activity} object. * @paramtext the text to show.     Can be formatted text. * @paramduration How long to display the message. either {@link#LENGTH_SHORT} or * {@link#LENGTH_LONG} **/

The first number of entries in the current context, the second is the text content that needs to be displayed, the third parameter is the display time

But there are only two display time, one is Toast.length_short and Toast.length_long. As the name implies, the latter is a little longer than the former.

Custom Toast Custom Pictures

Today I see a music playback software has a collection function will pop similar effect toast

The above one red ♥, the following display text content, then how does this effect implement?

You can see a way to open the toast source Setview

   /**      * Set the view to show.      @see #getView      */     Public void Setview (view view) {        = view;    }

You can add pictures and text by this method

Then you can try customizing a layout file and adding that layout to the toast in a setview way

The layout file is a Linetype layout with the following additions, adding a imageview and a textview to the Linetype layout.

The layout file is named Toast_view.xml, set orientation to vertical as vertical, and set the prepared heart-shaped picture to ImageView background

<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical" >    <LinearLayoutAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:background= "@android: Color/black"android:gravity= "Center"Android:minwidth= "100DP"android:orientation= "vertical">        <!--android:background= "@drawable/toast_bg" -        <ImageViewAndroid:id= "@+id/toast_image"Android:layout_width= "30DP"Android:layout_height= "30DP"android:layout_gravity= "Center"Android:layout_margin= "2DP"Android:background= "@drawable/redheart" />        <TextViewAndroid:id= "@+id/toast_text"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_margin= "2DP"android:gravity= "Center"Android:text=""Android:textcolor= "#ffffff"android:textsize= "15DP" />    </LinearLayout></LinearLayout>

Knot to create a Toastview Class that associates the layout file.

   /**     *      * @paramContext *@paramtext*/     PublicToastview (Context context, String text) {Layoutinflater Inflater=Layoutinflater.from (context); View View= Inflater.inflate (R.layout.toast_view,NULL); TextView T=(TextView) View.findviewbyid (R.id.toast_text);        T.settext (text); if(Toast! =NULL) {toast.cancel (); } Toast=NewToast (context);        Toast.setduration (Toast.length_short);    Toast.setview (view); }

Display the text to be displayed by SetText method

Of course, it can be further optimized to replace the imageview background.

  PublicToastview (Context context, String text) {Layoutinflater Inflater=Layoutinflater.from (context); View View= Inflater.inflate (R.layout.toast_view,NULL); ImageView ImageView=(ImageView) View.findviewbyid (r.id.toast_image);        Imageview.setbackgroundresource (R.mipmap.ic_launcher); TextView T=(TextView) View.findviewbyid (R.id.toast_text);        T.settext (text); if(Toast! =NULL) {toast.cancel (); } Toast=NewToast (context);        Toast.setduration (Toast.length_short);    Toast.setview (view); }

With this method, the layout is obtained first and then the child controls are set by Findviewbyid.

However, this effect is still not what we want to show, not with rounded corners.

At this point, you need to add a shape layout

Set the fillet and add the shape to the background of the LinearLayout

<shape xmlns:android= "Http://schemas.android.com/apk/res/android" >    <solid android:color= "#c83e3e3e" />    <!--radius shape-->    <corners        Android:bottomleftradius= "10DP"         Android:bottomrightradius= "10DP"        android:radius= "8DP"        android:topleftradius= " 10DP "        android:toprightradius=" 10DP "/></shape>   

Custom location

So how do I customize the display location?

By looking at the source of the toast you can see a setgravity method that is specifically used to set the toast location

   /** * Set the location at      which the notification should appear      in the screen. @see android.view.Gravity      @see #getGravity      */     Public void setgravity (intint int yoffset) {        = gravity;        = xoffset;         = yoffset;    }

The method has three entries, the first is the type of gravity, the parameter set the specific location, you can refer to the Gravity class

Generally used are:

Gravity.center
Gravity.left
Gravity.right
Gravity.top
Gravity.bottom

As the name implies, the second and third arguments are offsets, the offsets for the first parameter

So, if you set the toast to the screen, just do it.

Toast.setgravity (gravity.center, 0, 0);
Customize the display time of a toast

To be Continued ...

Android with picture toast (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.