Usage of toast in Android

Source: Internet
Author: User

Toast is a mechanism used by Android to display information. Unlike dialog, Toast has no focus and the display time of toast is limited, after a certain period of time, it will automatically disappear. Next we will use an instance to see how to use toast.

 

1. Default Effect

Code

Toast. maketext (getapplicationcontext (), "Default toast style ",
Toast. length_short). Show ();

 

2. Custom display Position Effect

Code

Toast = toast. maketext (getapplicationcontext (),
"Custom location toast", Toast. length_long );
Toast. setgravity (gravity. Center, 0, 0 );
Toast. Show ();

 

3. image effects

 

Code

Toast = toast. maketext (getapplicationcontext (),
"Toast with image", 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 ();

 

4. Fully customized results

Code

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 customized toast ");
Toast = new toast (getapplicationcontext ());
Toast. setgravity (gravity. Right | gravity. Top, 12, 40 );
Toast. setduration (toast. length_long );
Toast. setview (layout );
Toast. Show ();

 

5. other threads

Code

New thread (New runnable (){
Public void run (){
Showtoast ();
}
}). Start ();

 

 

Complete code

1. Main, Java

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. maketext (getapplicationcontext (), "I'm from another thread! ",
Toast. length_short). Show ();

}
});
}

@ Override
Public void onclick (view v ){
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. length_long );
Toast. setgravity (gravity. Center, 0, 0 );
Toast. Show ();
Break;
Case R. Id. btnsimpletoastwithimage:
Toast = toast. maketext (getapplicationcontext (),
"Toast with image", 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 customized 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;

}

}
}

 

2. Main, XML

<? 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: padding = "5dip" Android: gravity = "center">
<Button Android: layout_height = "wrap_content"
Android: layout_width = "fill_parent" Android: Id = "@ + ID/btnsimpletoast"
Android: text = "default"> </button>
<Button Android: layout_height = "wrap_content"
Android: layout_width = "fill_parent" Android: text = "Custom display position"
Android: Id = "@ + ID/btnsimpletoastwithcustomposition"> </button>
<Button Android: layout_height = "wrap_content"
Android: layout_width = "fill_parent" Android: Id = "@ + ID/btnsimpletoastwithimage"
Android: text = "with image"> </button>
<Button Android: layout_height = "wrap_content"
Android: layout_width = "fill_parent" Android: text = "Custom"
Android: Id = "@ + ID/btncustomtoast"> </button>
<Button Android: layout_height = "wrap_content"
Android: layout_width = "fill_parent" Android: text = "other threads"
Android: Id = "@ + ID/btnruntoastfromotherthread"> </button>

</Linearlayout>

 

3. Custom. xml

<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout
Xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: layout_height = "wrap_content" Android: layout_width = "wrap_content"
Android: Background = "# ffffffff" Android: Orientation = "vertical"
Android: Id = "@ + ID/lltoast">
<Textview
Android: layout_height = "wrap_content"
Android: layout_margin = "1dip"
Android: textcolor = "# ffffffff"
Android: layout_width = "fill_parent"
Android: gravity = "center"
Android: Background = "# bb000000"
Android: Id = "@ + ID/tvtitletoast"/>
<Linearlayout
Android: layout_height = "wrap_content"
Android: Orientation = "vertical"
Android: Id = "@ + ID/lltoastcontent"
Android: layout_marginleft = "1dip"
Android: layout_marginright = "1dip"
Android: layout_marginbottom = "1dip"
Android: layout_width = "wrap_content"
Android: padding = "15dip"
Android: Background = "#44000000">
<Imageview
Android: layout_height = "wrap_content"
Android: layout_gravity = "center"
Android: layout_width = "wrap_content"
Android: Id = "@ + ID/tvimagetoast"/>
<Textview
Android: layout_height = "wrap_content"
Android: paddingright = "10dip"
Android: paddingleft = "10dip"
Android: layout_width = "wrap_content"
Android: gravity = "center"
Android: textcolor = "# ff000000"
Android: Id = "@ + ID/tvtexttoast"/>
</Linearlayout>
</Linearlayout>

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.