Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.view.Gravity;
import android.widget.Toast;

public class Main {
    private static Toast toast = null;
    /**
     * show toast, keep only one instance, modify 2014-2-10
     */
    private static Handler handler = new Handler(Looper.getMainLooper());

    public static void showMessage(final Context act, final String msg, final int len) {
        new Thread(new Runnable() {
            public void run() {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        if (toast != null) {
                            // toast.cancel();
                            toast.setText(msg);
                            // toast.setDuration(len);
                        } else {
                            toast = Toast.makeText(act, msg, len);
                            toast.setGravity(Gravity.BOTTOM, 0, 100);
                        }
                        toast.show();
                    }
                });
            }
        }).start();
    }
}