Introduction
People presently use Android-enabled smartphones. So in these phones when a small message, more precisely called a small notification, appears on the screen for some time and then vanishes then that is called a toast.
It only requires the amount of space required for the message and the current activity remains visible and is interactive with the user. For example, when internet browsing, in the history part, we can see the various links we have visited. Now a bookmarked one raises a popup "bookmarked saved" that means the toast tells the user that he may continue. Toasts automatically disappear after a period of time because there is a time set for its visibility on any activity.
![]()
Figure: The red portion illustrates the Toast "Added to Bookmarks"
Toast class
A toast class is a view containing a small message for the user as stated previously. When the view is shown to the user it appears as a floating view over the application. It will never receive focus means it can't be interactable. The user will probably be in the middle of typing something else so these toast notifications just provide notification and does not hinder the work of the user. Examples are the volume control and the brief message saying that your settings have been saved and draft saved and so on.
The most convieninet way to use and call the static member is to use the toast class and access these static members within a program.
Note: users can't interact with these toast notifications, these are just for information in response to user inputs generally.
First create a toast object with one of the makeTest() methods. This method takes the three parameters "application context" , "the text message" and "duration of toast". It will return a properly initialized toast object. To display a toast message we can use any method.
- Context context = getApplicationContext();
- CharSequence text = "Welcome Gaurav!!";
- int duration = Toast.LENGTH_SHORT;
-
- Toast toast = Toast.makeText(context, text, duration);
- toast.show();
However we have created a toast method that will initiate the toast. We have already defined all the toast attributes, like length, duration and many things to be defined.
Chaining of members is possible. Mainly methods prevent the holding of a toast object.
- Toast.makeText(context, text, duration).show();
Positioning Your toastA standard toast notification must appear near the bottom of the screen and must be centered horizontally as well. However we can see these things in any version of Android and other phones as well. You can change this position with the setGravity(int, int, int) method. This accepts the three parameters, a Gravity constant, an x-position offset and a y-position offset. This is quite interesting that we use this method.
For example if you want that my text should appear in the top-left corner you can set the gravity as shown below.
- toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);
If we want that the position should be right then we must increase the value of the second parameter and if we want the toast to appear down then increase the value of the third and last parameter. However it depends upon us, wherever we want the toast on screen.
Creating the custom Toast viewNow to print the Toast directly we need to add a TextView as we have created in previous articles. Now if we don't create a custom text view then it will look weird so it is the duty of the programmer to make a system with a look and feel.
Define the layout in the activity_main.xml and use the following procedure:
- Change the RelativeLayout with LinearLayout.
- Insert ImageView.
- <ImageView android:src="@drawable/droid"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginRight="8dp" />
Create the
TextView
- <TextView android:id="@+id/text"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textColor="#FFF"
- />
Now follow the code given below so this creates some text view on the activity.
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/toast_layout_root"
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:padding="8dp"
- android:background="#DAAA">
-
- <ImageView android:src="@drawable/droid"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginRight="8dp" />
-
- <TextView android:id="@+id/text"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textColor="#FFF" />
-
- </LinearLayout>
Here we have defined ImageView and Textview as well. Now add the
corresponding Java code.
- package com.example.gkumar.Toast;
- import android.R;
- import android.support.v7.app.ActionBarActivity;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuItem;
-
-
- public class MainActivity extends ActionBarActivity {
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_list_item);
- }
- public boolean onCreateOptionsMenu(Menu menu) {
-
-
- LayoutInflater inflater = getLayoutInflater();
- View layout = inflater.inflate(R.layout.custom_toast,
- (ViewGroup) findViewById(R.id.main_layout_root));
-
- TextView text = (TextView) layout.findViewById(R.id.text);
- text.setText("welcome Gaurav");
-
- Toast toast = new Toast(getApplicationContext());
- toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
- toast.setDuration(Toast.LENGTH_LONG);
- toast.setView(layout);
- toast.show();
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
-
-
-
- int id = item.getItemId();
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- }
Here basically what is happening is that first, retrieve the
LayoutInflater with
getLayoutInflater() (or
getSystemService()), and then inflate the layout from XML using inflate(int,
ViewGroup). The first parameter is the layout resource ID and the second is the root View. Now it can use this inflated layout to find more View objects in the layout, so now capture and define the content for the
ImageView and
TextView elements.
Finally, create a new Toast with
Toast(Context) and set some properties of the toast, such as the gravity and duration. Then call
setView(View) and it the inflated layout. You can now display the toast with your custom layout by
calling show().
Note: Here we are not using any public constructor for a toast because we haven't used s
etView(view) here. If anyone doesn't want to use a custom layout then use the
makeText.() method to create the toast.
Summary This article explains the basics of Toasts and how it can be created. It may be considered to be like notifications but it is quite snazzy and looks awesome and renders for just a second and then vanishes.