User notification: Toast

Hi!
I'm begining a series of articles about how to alert the user. Course is designed for beginners.There are three ways to alert the user: Toast, Notification and through different types of Dialog. Today I will discuss of the first type of warning - Toast.
Introduction
Toast is a pop-up window that allows you to quickly notify the user about the event has occurred, such as maintaining the program settings to SD-card. Toast feature is that during the message, the user can interact with them is behind Activity, or with a home screen (home screen). Also worth noting that the user can not control the closing of Toast with hardware Back button or other possible means, the message appears and then smoothly itself as smoothly disappears. The time delay between the appearance and disappearance can be set programmatically. In most cases, Toast is a short message, but there is an opportunity to ask any appearance of Toast, for example, add an image next to the text. In addition, you can control the location of Toast on the screen.Toast can be created from an Activity, or from the Service. In the case of a service Toast appears on top of Activity, which has the focus, or top of the home screen.


Create a simple Toast
Create a simple Toast can be a static method of class makeText Toast, setting the necessary parameters.


Toast.makeText(getApplicationContext()"Привет, мир!", Toast.LENGTH_SHORT).show();

The details given application context, the message delay and wrote about earlier. The message can be set directly in the form of text, or using a text-line resourcefor example, R.string.hello_world, which stores the text to be displayed, in this case, "Hello,world!". The delay may be short - LENGTH_SHORT, or long - LENGTH_LONG. By default, the Toast is given a short delayThe software delay is given by setDuration.

The essence of the method makeText is: inside the method creates an Toast, set the text and type of delayFurther, the object can be either usedas in my case, the methodshow, which displays created by Toast, or set additional properties Toast, for example, its location on the screen, or you create the look.

Toast created as follows:


image
Change the position of Toast

Toast on the screen location specified by the method setGravity as follows:


Toast toast = Toast.makeText(getApplicationContext()"Привет, мир!",
Toast.LENGTH_LONG);toast.setGravity(Gravity.CENTER00);
toast.show();
The first parameter sets the alignment method, versions of which quite a lot in the class of Gravity. The second and third parameters specify how many pixels will be shifted horizontally to the right Toast and vertically downward, respectively, relative to the value in the first parameter. The result of the above code displays as follows:

image
Adding images in an easy Toast 
To add an image in standard Toast software required to create an object of class ImageView and ask for a picture of the resources using the setImageResource. You will then get the standard look of Toast, if you look in the debugger it is LinearLayout, and add the created object ImageView showing in what position to add a picture, in my case I have a zero position so that the image has been added above the text. Code to create this image with Toast is presented below.


Toast toast = Toast.makeText(getApplicationContext()"Привет, мир!", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER00);
LinearLayout toastView = (LinearLayout) toast.getView();
ImageView imageWorld = new ImageView(getApplicationContext());
imageWorld.setImageResource(R.drawable.world);
toastView.addView(imageWorld, 0);
toast.show();
Toast thus created is as follows:

image
Toast the creation of complex

To create a complex Toast to create your own layout, code is as follows:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="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"></TextView>
<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>
I created a Toast in a dialogue with the headlinewhich is located inside the image and text.
Now, this layout is necessary to register for Toast and ask for the message title and text, and set the image. This is done as follows:

LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast,
  (ViewGroup) findViewById(R.id.llToast));
ImageView image = (ImageView) layout.findViewById(R.id.tvImageToast);
image.setImageResource(R.drawable.world);
TextView title = (TextView) layout.findViewById(R.id.tvTitleToast);
title.setText("Внимание");
TextView text = (TextView) layout.findViewById(R.id.tvTextToast);
text.setText("Привет, мир!");


Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.RIGHT | Gravity.TOP1240);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
The first two lines are initialized by filling the View object to xml-file from (used for thisinstance LayoutInflater, obtained by the method getLayoutInflater), created earlier. The first parameter of inflate sets the ID of a previously created layout - R.layout.toast (in this case, it corresponds to the file res / layout / toast.xml). Then obtained by reference to theimage, caption and text messagesand filled with the desired dataThen immediatelycreate a Toast, set the parameters and layout as the one prescribed layout, which weinitialized earlier this is done using the setView. The result of all these manipulations weget Toast, which looks like the figure below.

image
In this first article on how to alert the user is finished. If you find a mistake or want to addsome other ways to work with Toast, write comments, and I will add your comments in the article.