Android Development Series (23): Implement a Toast hint message box with a picture hint

Source: Internet
Author: User

Toast in Android is a very common message prompt, but the default message prompt is a single line of plain text, so we can set up some other message prompts for it, such as a stripe.

Implementing this is simple:

is to define a layout view, and then set the toast to display a custom view.

Here, you set up a LinearLayout container, and then add a picture to the container and add text messages. Then set the container to the toast object and let it show.


First create an Android project, and then we edit the next Main.xml file:

<span style= "FONT-SIZE:14PX;" ><?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/ Res/android "android:orientation=" vertical "android:layout_width=" fill_parent "android:layout_height=" fill_parent "Android:gravity=" Center_horizontal "><button  android:id=" @+id/simple "android:layout_width=" Wrap_ Content "android:layout_height=" wrap_content "android:text=" Display simple hint "/><button  android:id=" @+id/bn " Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:text= "Show hints with pictures"/></ Linearlayout></span>
There are two buttons defined here, one is the default toast message prompt, and the other is the display of informational cues with pictures.


Next, we can edit the Java code of the main interface: Toasttest.java

<span style= "FONT-SIZE:14PX;" >import android.app.activity;import android.graphics.color;import Android.os.bundle;import android.view.Gravity ; Import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.imageview;import Android.widget.linearlayout;import Android.widget.textview;import Android.widget.toast;public class Toasttest extends activity{@Overridepublic void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main); Button simple = (button) Findviewbyid (r.id.simple);//the event listener Simple.setonclicklistener for the button's Click event (New Onclicklistener () {@Overridepublic void OnClick (View source) {//Create a toast prompt message toast toast = Toast.maketext (Toasttest.this, "simple cue message"// Set the duration of the TOAST hint message, toast.length_short); Toast.show ();}); Button bn = (Button) Findviewbyid (r.id.bn);//For the button's Click event Binding Event Listener Bn.setonclicklistener (new Onclicklistener () {@ overridepublic void OnClick (View source) {//Create a toast prompt message toast toast = new TOast (toasttest.this);//Set the display position of the toast toast.setgravity (gravity.center, 0, 0);//Create a imageviewimageview image = new ImageView (Toasttest.this); Image.setimageresource (r.drawable.tools);//Create a linearlayout container linearlayout ll = new LinearLayout (toasttest.this);//Add a picture to LinearLayout, the original Viewll.addview (image);//Create a imageviewtextview TextView = new TextView (Toasttest.this); Textview.settext ("Cue letter with picture");//Set the font size and color in the text box Textview.settextsize (30); Textview.settextcolor (Color.magenta); Ll.addview (TextView);//Set toast Display custom Viewtoast.setview (ll);// Set the display time of the Toast toast.setduration (Toast.length_long); Toast.show ();}});}} </span>


we can get the bottom:




Android Development Series (23): Implement a Toast hint message box with a picture hint

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.