ANDROID Toast Notification Usage example detailed _android

Source: Internet
Author: User
Tags sqlite database

This example describes the Android toast notification usage. Share to everyone for your reference, specific as follows:

Toast displays a message to the user on the phone screen, and the information disappears automatically after a period of time.

1. Default usage

Copy Code code as follows:
Toast.maketext (Getapplicationcontext (), "Default Toast style", Toast.length_short). Show ();

The usage in 2.Fragment

Copy Code code as follows:
Toast.maketext (Getactivity (), "Network connection error, please check network Settings", Toast.length_long). Show ();

3. Customize the display position effect

Toast = Toast.maketext (Getapplicationcontext (), "Custom location Toast", Toast.length_long);
Toast.setgravity (gravity.center, 0, 0);
Toast.show ();

4. With picture effect

Toast = Toast.maketext (Getapplicationcontext (), "toast with pictures", toast.length_long);
Toast.setgravity (gravity.center, 0, 0);
LinearLayout Toastview = (linearlayout) toast.getview ();
ImageView imagecodeproject = new ImageView (Getapplicationcontext ());
Imagecodeproject.setimageresource (R.drawable.icon);
Toastview.addview (imagecodeproject, 0);
Toast.show ();

5. Full Custom effect

Layoutinflater inflater = Getlayoutinflater ();
View layout = Inflater.inflate (R.layout.custom, (ViewGroup) Findviewbyid (r.id.lltoast));
ImageView image = (ImageView) Layout.findviewbyid (r.id.tvimagetoast);
Image.setimageresource (R.drawable.icon);
TextView title = (TextView) Layout.findviewbyid (r.id.tvtitletoast);
Title.settext ("Attention");
TextView Text = (TextView) Layout.findviewbyid (r.id.tvtexttoast);
Text.settext ("Fully customizable Toast");
Toast = new Toast (Getapplicationcontext ());
Toast.setgravity (Gravity.right | Gravity.top,);
Toast.setduration (Toast.length_long);
Toast.setview (layout);
Toast.show ();

6. Other Threads

Main.java Code:

Package com.wjq.toast;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.os.Handler;
Import android.view.Gravity;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.view.View.OnClickListener;
Import Android.widget.ImageView;
Import Android.widget.LinearLayout;
Import Android.widget.TextView;
Import Android.widget.Toast;
  public class Main extends activity implements Onclicklistener {Handler Handler = new Handler ();
    @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Setcontentview (R.layout.main);
    Findviewbyid (R.id.btnsimpletoast). Setonclicklistener (this);
    Findviewbyid (r.id.btnsimpletoastwithcustomposition). Setonclicklistener (this);
    Findviewbyid (r.id.btnsimpletoastwithimage). Setonclicklistener (this);
    Findviewbyid (R.id.btncustomtoast). Setonclicklistener (this);
  Findviewbyid (R.id.btnruntoastfromotherthread). Setonclicklistener (this); }
  public void Showtoast () {handler.post, new Runnable () {@Override public void run () {Toast.mak EText (Getapplicationcontext (), "I come from other threads!"
      ", Toast.length_short). Show ();
  }
    });
    @Override public void OnClick (View v) {Toast Toast = null; Switch (V.getid ()) {Case R.id.btnsimpletoast:toast.maketext (Getapplicationcontext (), default Toast style, Toast.length_
      Short). Show ();
    Break Case r.id.btnsimpletoastwithcustomposition:toast = Toast.maketext (Getapplicationcontext (), "Custom location toast", Toast.LENG
      Th_long);
      Toast.setgravity (gravity.center, 0, 0);
      Toast.show ();
    Break
      Case r.id.btnsimpletoastwithimage:toast = Toast.maketext (Getapplicationcontext (), "Toast with picture", Toast.length_long);
      Toast.setgravity (gravity.center, 0, 0);
      LinearLayout Toastview = (linearlayout) Toast.getview ();
      ImageView imagecodeproject = new ImageView (Getapplicationcontext ()); Imagecodeproject.setimageresource (R.DRawable.icon);
      Toastview.addview (imagecodeproject, 0);
      Toast.show ();
    Break
      Case R.id.btncustomtoast:layoutinflater Inflater = Getlayoutinflater ();
      View layout = Inflater.inflate (R.layout.custom, (ViewGroup) Findviewbyid (r.id.lltoast));
      ImageView image = (ImageView) Layout.findviewbyid (r.id.tvimagetoast);
      Image.setimageresource (R.drawable.icon);
      TextView title = (TextView) Layout.findviewbyid (r.id.tvtitletoast);
      Title.settext ("Attention");
      TextView Text = (TextView) Layout.findviewbyid (r.id.tvtexttoast);
      Text.settext ("Fully customizable toast");
      Toast = new Toast (Getapplicationcontext ()); Toast.setgravity (Gravity.right |
      Gravity.top, 12, 40);
      Toast.setduration (Toast.length_long);
      Toast.setview (layout);
      Toast.show ();
    Break 
        Case R.id.btnruntoastfromotherthread:new Thread (new Runnable () {public void run () {showtoast ());
      }). Start ();
    Break
 } }
}

 

Main.xml Code:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" Android "Android:layout_width=" Fill_parent "android:layout_height=" fill_parent "android:gravity=" center "android:o rientation= "vertical" android:padding= "5dip" > <button android:id= "@+id/btnsimpletoast" Android:layout_w
    Idth= "Fill_parent" android:layout_height= "wrap_content" android:text= "Default" > </Button> <button Android:id= "@+id/btnsimpletoastwithcustomposition" android:layout_width= "Fill_parent" android:layout_height= " 
    Wrap_content "android:text=" Custom display location > </Button> <button android:id= "@+id/btnsimpletoastwithimage" Android:layout_width= "Fill_parent" android:layout_height= "Wrap_content" android:text= "with pictures" > </Button > <button android:id= "@+id/btncustomtoast" android:layout_width= "Fill_parent" W Rap_content "android:text=" full customization> </Button> <button android:id= "@+id/btnruntoastfromotherthread" android:layout_width= "fill_parent

 "Android:layout_height=" Wrap_content "android:text=" Other Threads "> </Button> </LinearLayout>

Custom.xml:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" Android "android:id=" @+id/lltoast "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "Androi" D:background= "#ffffffff" android:orientation= "vertical" > <textview android:id= "@+id/tvtitletoast" Andro Id:layout_width= "Fill_parent" android:layout_height= "wrap_content" android:layout_margin= "1dip" android:backgr Ound= "#bb000000" android:gravity= "center" android:textcolor= "#ffffffff"/> <linearlayout android:id= "@ +id/lltoastcontent "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "Android:layout_ma" Rginbottom= "1dip" android:layout_marginleft= "1dip" android:layout_marginright= "1dip" android:background= "#4400 0000 "android:orientation=" vertical "android:padding=" 15dip "> <imageview android:id=" @+id/tvimage Toast "Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_gravity= "center"/> <textview
      Android:id= "@+id/tvtexttoast" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:gravity= "center" android:paddingleft= "10dip" android:paddingright= "10dip" android:textcolor= "

 #ff000000 "/> </LinearLayout> </LinearLayout>

For more information on Android-related content readers can view the site: "The activity of Android programming skills Summary", "Android Resources Operating Skills Summary", "Android File Operating skills summary", " Android Operation SQLite Database skills Summary, "Android operation JSON format Data Skills summary", "Android Database Operation skills Summary", "Android programming development of SD card Operation Summary", "Android Development introduction and Advanced Course", The Android View view tips summary and the Android Control usage summary

I hope this article will help you with the Android program.

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.