Friday, July 4, 2014

Creating a Toast


Wish to display a notification message to the user? Then, “Toast” is what you should be looking for. In Android framework, Toast is a notification message that pops up and is displayed for a certain amount of time. It occupies the amount of space required for the message only and the current activity stays visible and interactive. It automatically fades in and out and is mostly used for debugging purposes by developers.

Basics 

Initially, instantiate a Toast object with one of the makeText() methods. This method requires three parameters: the application Context, the text message, and the duration for the toast. It returns a well initialized Toast object. The toast notification may be displayed with show(), as shown in the following example:
Context context = getApplicationContext();

CharSequence text = "Hello toast!";

int duration = Toast.LENGTH_SHORT;



Toast toast = Toast.makeText(context, text, duration);

toast.show();

You may also chain the methods and avoid holding on to the Toast object, in the following manner:

Toast.makeText(context, text, duration).show();

In this tutorial, we will demonstrate two Toast examples:
1. Normal Toast view
2. Custom Toast view

1. Normal Toast view


Here is a simple Toast example:

File: res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/buttonToast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Toast" />

</LinearLayout>

File: MainActivity.java

package com.endeavour.android;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

        private Button button;

        public void onCreate(Bundle savedInstanceState) {

               super.onCreate(savedInstanceState);
               setContentView(R.layout.main);

               button = (Button) findViewById(R.id.buttonToast);

               button.setOnClickListener(new OnClickListener() {

                         @Override
                         public void onClick(View arg0) {

                            Toast.makeText(getApplicationContext(),
                               "Hello World", Toast.LENGTH_LONG).show();

                         }
               });
        }
}

Output 

The following output shall be displayed.

 


2. Custom Toast View 

You may enhance the above example by customizing the original Toast view.

Positioning your Toast

A default toast notification appears horizontally centered near the bottom of the screen. You may change this position by using the setGravity(int, int, int) method. This method accepts three parameters: a Gravity constant, an x-position (horizontal) offset, and a y-position (vertical) offset. If you want to push the Toast position to the right, increase the value of the second parameter. To push it down, increase the value of the last parameter. For example, if you want the toast to appear in the top-left corner, you may set the gravity like this:

toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);

File: res/layout/customtoast.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/custom_toast_layout_id"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ABC"
    android:orientation="horizontal"
    android:padding="5dp" >

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_marginRight="5dp" />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:textColor="#000" />

</LinearLayout>

File: MainActivity.java

package com.endeavour.android;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

        private Button button;

        public void onCreate(Bundle savedInstanceState) {

               super.onCreate(savedInstanceState);
               setContentView(R.layout.main);

               button = (Button) findViewById(R.id.buttonToast);

               button.setOnClickListener(new OnClickListener() {

                       @Override
                       public void onClick(View arg0) {

                               // get your custom_toast.xml ayout
                               LayoutInflater inflater = getLayoutInflater();

                               View layout = inflater.inflate(R.layout.customtoast,
                                 (ViewGroup) findViewById(R.id.custom_toast_layout_id));

                               // set a dummy image
                               ImageView image = (ImageView) layout.findViewById(R.id.image);
                               image.setImageResource(R.drawable.ic_launcher);

                               // set a message
                               TextView text = (TextView) layout.findViewById(R.id.text);
                               text.setText("Hello World!");

                               // Toast
                               Toast toast = new Toast(getApplicationContext());
                               toast.setGravity(Gravity.BOTTOM|Gravity.RIGHT, 0, 0);
                               toast.setDuration(Toast.LENGTH_LONG);
                               toast.setView(layout);
                               toast.show();
                       }
               });
        }
}

Output 


The following output shall be displayed.

No comments:

Post a Comment