Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import android.content.Context;

import android.support.annotation.StringRes;

import android.widget.Toast;

public class Main {
    private static long oneTime;
    private static long twoTime;
    private static String oldMsg;
    private static Toast toast;

    public static void showSingleToast(Context ctx, @StringRes int msgID) {
        showSingleToast(ctx, msgID, 0);
    }

    public static void showSingleToast(Context ctx, @StringRes int msgID, int duration) {
        String msg = ctx.getString(msgID);
        int showDuration = duration <= Toast.LENGTH_SHORT ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT;
        if (null == toast) {
            toast = Toast.makeText(ctx.getApplicationContext(), msgID, showDuration);
            toast.show();
            oneTime = System.currentTimeMillis();
        } else {
            twoTime = System.currentTimeMillis();
            if (msg.equals(oldMsg)) {
                if (twoTime - oneTime > showDuration) {
                    toast.show();
                }
            } else {
                oldMsg = msg;
                toast.setText(msg);
                toast.show();
            }
        }
        oneTime = twoTime;
    }

    public static void showSingleToast(Context ctx, String msg) {
        showSingleToast(ctx, msg, 0);
    }

    public static void showSingleToast(Context ctx, String msg, int duration) {
        int showDuration = duration <= Toast.LENGTH_SHORT ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT;
        if (null == toast) {
            toast = Toast.makeText(ctx.getApplicationContext(), msg, showDuration);
            toast.show();
            oneTime = System.currentTimeMillis();
        } else {
            twoTime = System.currentTimeMillis();
            if (msg.equals(oldMsg)) {
                if (twoTime - oneTime > showDuration) {
                    toast.show();
                }
            } else {
                oldMsg = msg;
                toast.setText(msg);
                toast.show();
            }
        }
        oneTime = twoTime;
    }
}