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.text.TextUtils;
import android.widget.Toast;

public class Main {
    private static Toast sToast;

    public static void toastMessage(Context context, String message) {
        if (context == null) {
            return;
        }
        if (TextUtils.isEmpty(message)) {
            sToast = null;
            return;
        }
        if (sToast != null) {
            sToast.cancel();
            sToast = null;
        }
        sToast = getToast(context);
        sToast.setDuration(Toast.LENGTH_SHORT);
        sToast.setText(message);
        sToast.show();
    }

    public static void toastMessage(Context context, int resId) {
        if (context == null) {
            return;
        }
        if (sToast != null) {
            sToast.cancel();
            sToast = null;
        }
        sToast = getToast(context);
        sToast.setDuration(Toast.LENGTH_SHORT);
        sToast.setText(resId);
        sToast.show();
    }

    private static Toast getToast(Context context) {
        return Toast.makeText(context, "", Toast.LENGTH_SHORT);
    }
}