Toast in Android

Source: Internet
Author: User

Introduction

Toast is a pop-up Message that allows you to easily notify users of some time, such as saving data to the SD card. It is worth noting that you cannot cancel Toast. In most cases, Toast is just a short message, but you can also customize the Toast interface.

 


Create Standard Toast

The standard Toast can be created using the static Toast method makeText:


Toast. makeText (getApplicationContext (), "Hello, The Code Project! ",
Toast. LENGTH_SHORT). show ();
Toast. makeText (getApplicationContext (), "Hello, The Code Project! ",
Toast. LENGTH_SHORT). show (); parameters are the application context, the displayed message content, and the displayed delay. You can also use R to call the content of the resource file, such as R. string. hello_codeproject. The delay displayed by Message can be either LENGTH_SHORT or LENGTH_LONG, Which is LENGTH_SHORT by default. You can also set the Latency by calling the setDuration method.

 


Set Toast location

You can set the Toast position on the screen by calling the following method:

 

Toast toast = Toast. makeText (getApplicationContext (),
"Hello, The Code Project! ", Toast. LENGTH_LONG );
Toast. setGravity (Gravity. CENTER, 0, 0 );
Toast. show ();
Toast toast = Toast. makeText (getApplicationContext (),
"Hello, The Code Project! ", Toast. LENGTH_LONG );
Toast. setGravity (Gravity. CENTER, 0, 0 );
Toast. show (); the first parameter sets the location, and the second parameter defines the offset pixel relative to the location of the first parameter.

 


Add images to standard Toast

You need to create an ImageView object, call the setImageResource method, and add an image to Toast.


Toast toast = Toast. makeText (getApplicationContext (),
"Hello, The Code Project! ", Toast. LENGTH_LONG );
Toast. setGravity (Gravity. CENTER, 0, 0 );
LinearLayout toastView = (LinearLayout) toast. getView ();
ImageView imageCodeProject = new ImageView (getApplicationContext ());
ImageCodeProject. setImageResource (R. drawable. codeprojectlogo );
ToastView. addView (imageCodeProject, 0 );
Toast. show ();
Toast toast = Toast. makeText (getApplicationContext (),
"Hello, The Code Project! ", Toast. LENGTH_LONG );
Toast. setGravity (Gravity. CENTER, 0, 0 );
LinearLayout toastView = (LinearLayout) toast. getView ();
ImageView imageCodeProject = new ImageView (getApplicationContext ());
ImageCodeProject. setImageResource (R. drawable. codeprojectlogo );
ToastView. addView (imageCodeProject, 0 );
Toast. show ();

Effect

 

Create Toast for custom Layout
First, create the layout of Toast for custom layout:


<? 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>
<? 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>


The Toast we created has a header, an image, and a message. Now we need to apply layout to the Toast we created:


LayoutInflater inflater = getLayoutInflater ();
View layout = inflater. inflate (R. layout. customtoast,
(ViewGroup) findViewById (R. id. llToast ));
ImageView image = (ImageView) layout. findViewById (R. id. tvImageToast );
Image. setImageResource (R. drawable. codeprojectlogo );
TextView title = (TextView) layout. findViewById (R. id. tvTitleToast );
Title. setText ("Attention ");
TextView text = (TextView) layout. findViewById (R. id. tvTextToast );
Text. setText ("Hello, The Code Project! ");
Toast toast = new Toast (getApplicationContext ());
Toast. setGravity (Gravity. RIGHT | Gravity. TOP, 12, 40 );
Toast. setDuration (Toast. LENGTH_LONG );
Toast. setView (layout );
Toast. show ();
LayoutInflater inflater = getLayoutInflater ();
View layout = inflater. inflate (R. layout. customtoast,
(ViewGroup) findViewById (R. id. llToast ));
ImageView image = (ImageView) layout. findViewById (R. id. tvImageToast );
Image. setImageResource (R. drawable. codeprojectlogo );
TextView title = (TextView) layout. findViewById (R. id. tvTitleToast );
Title. setText ("Attention ");
TextView text = (TextView) layout. findViewById (R. id. tvTextToast );
Text. setText ("Hello, The Code Project! ");
Toast toast = new Toast (getApplicationContext ());
Toast. setGravity (Gravity. RIGHT | Gravity. TOP, 12, 40 );
Toast. setDuration (Toast. LENGTH_LONG );
Toast. setView (layout );
Toast. show ();

From xinem's column
 

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.