Utility method to show a toast to the user. - Android User Interface

Android examples for User Interface:Toast

Description

Utility method to show a toast to the user.

Demo Code


//package com.java2s;

import android.content.Context;
import android.os.Handler;

import android.widget.Toast;

public class Main {
    /**/*w w w.  j a v  a  2s. co  m*/
     * Utility method to show a toast to the user.
     * This toast is customised to display for the specified time only.
     *
     * @param context Current context of the application.
     * @param message Message to display to the user.
     */
    public static void showCustomLengthToastShort(Context context,
            String message) {
        //custom display time
        final long DISPLAY_TIME = 500;//millis
        final Toast toast = Toast.makeText(context, message,
                Toast.LENGTH_SHORT);

        toast.show();

        //setDuration here using a handler to cancel after DISPLAY_TIME
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                toast.cancel();
            }
        }, DISPLAY_TIME);
    }
}

Related Tutorials