Toast display image

Source: Internet
Author: User

Toast is used to display some help/prompts to users. The following shows five effects to illustrate the power of toast.

Note:

Length_long --- display the view or text prompt for a long time

Length_short --- display the view or text prompt in a short time

Setgravity (INT gravity, int xoffset, int yoffset) --- set the position where the prompt should be displayed on the screen

Setduration (INT duartion) --- set the display duration

 

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>

 

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.