Android Basics Getting Started tutorial--2.5.1 toast (toast) basic use

Source: Internet
Author: User

Android Basics Getting Started tutorial--2.5.1 toast (toast) basic use

tags (space delimited): Android Basics Getting Started Tutorial

Introduction to this section:

OK, finally learned some of the adapter class related controls, of course, in addition to explain the few, there are many other
Related controls, do not slowly explain the need to self-check the document to see the relevant usage, this section brings:
Android is used to cue information for a control--toast (toast)! A toast is a handy message prompt that
A message prompt appears on the screen without any buttons, and will not get the focus for a period of time after the auto disappears!
Very popular! In this section we will learn how to use toast!

1. Call the Maketext () method of the toast class directly to create

This is one of the most common forms we've used! For example, click on a button and then pop the toast, using:
Toast.maketext (mainactivity.this, "hint content", Toast.length_long). Show ();
The first one is the context object! To two is the content of the display! The third one is the displayed time, only long and short two kinds of
will take effect, immediately you define the other values, the last call or the two!

In addition toast is very common, we can take these public parts out, write in a Method!
Call this method directly when you need to display a toast to show toasts, so it's much easier!
Examples are as follows:

void Midtoast (String str, int showTime) {Toast toast = toast. Maketext(Global_context, str, showTime); Toast. Setgravity(Gravity. CENTER_vertical| Gravity. CENTER_horizontal,0,0); Set Display locationTextView v = (TextView) toast. GetView(). Findviewbyid(Android. R. ID. Message);V. SetTextColor(Color. YELLOW); Set Font ColorToast. Show(); }

Above this extracted method, we find that we can call setgravity to set the location of the toast display and get
Via Findviewbyid (Android. R.id.message) Get the displayed text, then set the color, or size, etc.!
This is the second way to customize toast! by means of construction.

2. Customize the toast by constructing the method:

The above custom text, as well as the display location, below we write two simple examples:

1. Define a toast with a picture

Key Code :

private void Midtoast (String str, int showTime) {Toast toast = toast. Maketext(Mcontext, str, showTime);Toast. Setgravity(Gravity. CENTER_horizontal| Gravity. BOTTOM,0,0); Set Display locationLinearLayout layout = (linearlayout) toast. GetView();Layout. SetBackgroundColor(Color. BLUE);ImageView image = new ImageView (this);Image. Setimageresource(R. Mipmap. IC_icon_qitao);Layout. AddView(Image,0);TextView v = (TextView) toast. GetView(). Findviewbyid(Android. R. ID. Message);V. SetTextColor(Color. YELLOW); Set Font ColorToast. Show();}
2.Toast fully Customizable

If the above is not enough for you, then you can write your own toast layout, and then show it;
But time we still can't control!

Run :

Key Code :

private void Midtoast (String str, int showTime) {Layoutinflater inflater = Getlayoutinflater ();View view = Inflater. Inflate(R. Layout. View_toast_custom, (ViewGroup) Findviewbyid (R. ID. lly_toast));ImageView Img_logo = (ImageView) view. Findviewbyid(R. ID. IMG_logo);TextView tv_msg = (TextView) view. Findviewbyid(R. ID. TV_MSG);Tv_msg. SetText(str);Toast toast = new Toast (mcontext);Toast. Setgravity(Gravity. CENTER,0,0);Toast. Setduration(Toast. LENGTH_long);Toast. Setview(view);Toast. Show();}

There are also custom toast layouts and rounded backgrounds:

Fillet background:bg_toast.xml:

<?xml version= "1.0" encoding= "Utf-8"?><shape xmlns:android="Http://schemas.android.com/apk/res/android">    <!--Set Transparent background color --    <solid android:color="#BADB66" />    <!--set a black border --    <strokeandroid:width="1px"android:color="#FFFFFF" />                     <!--Set the radius of four rounded corners--    <cornersAndroid:bottomleftradius="50px"Android:bottomrightradius ="50px"android:topleftradius="50px"android:toprightradius= "50px" />                                    <!--set a bottom margin to make the space a little more--    <paddingandroid:bottom="5DP"android:left="5DP"android: right ="5DP"android:top="5DP" />                                </shape>  

Layout file:view_toast_custom.xml:

<?xml version= "1.0" encoding= "Utf-8"?><linearlayout  xmlns: Android  = "http://schemas.android.com/apk/res/android"  android:id  =" @+id/lly_toast " android:layout_width  = "match_parent"  android:layout_height  = "match_parent"  android:background  =" @drawable/bg_toast " android:orientation  =;     <imageview  android:id
      = "@+id/img_logo"  android:layout_width  =         "24DP"  android:layout_height  = "24DP"  android:layout_marginleft  =" 10DP " android:src  =" @mipmap/iv_lol_icon1 "/>     <TextViewandroid:id= "@+id/tv_msg"android:layout_width="Match_ Parent "android:layout_height="wrap_content "android:layout_marginleft=" 10DP "android:textsize=" 20sp " />                                        </linearlayout>

Very simple, hehe ~

3. Sample code Download

Toastdemo.zip

This section summarizes:

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Basics Getting Started tutorial--2.5.1 toast (toast) basic use

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.