Build your own Toast

Source: Internet
Author: User

Toast is used to display some help/prompts to users. The following is an example of how Toast works. It defines your own 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, 400 phone, http://www.my400800.cn ");
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
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 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>

Author: "I love technology"
 

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.