Android Toast Create showToast(final Context context, String text)

Here you can find the source of showToast(final Context context, String text)

Description

show Toast

Declaration

public static void showToast(final Context context, String text) 

Method Source Code

//package com.java2s;
import android.content.Context;

import android.os.*;
import android.widget.Toast;

public class Main {
    private static Handler handler;

    public static void showToast(final Context context, String text) {
        if (null == context) {
            return;
        }/*from  w  ww  .j av  a 2s.co m*/
        if (Looper.myLooper() != Looper.getMainLooper()) {
            if (handler == null) {
                handler = new Handler(Looper.getMainLooper()) {
                    @Override
                    public void handleMessage(Message msg) {
                        String str = (String) msg.obj;
                        Toast.makeText(context, str, Toast.LENGTH_SHORT)
                                .show();
                        super.handleMessage(msg);
                    }
                };
            }
            Message message = handler.obtainMessage();
            message.obj = text;
            message.sendToTarget();
        } else {
            Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
        }
    }

    public static void showToast(Context context, int id) {
        if (null != context) {
            Toast.makeText(context, id, Toast.LENGTH_SHORT).show();
        }
    }
}

Related

  1. displayToast(String message, Activity activity)
  2. showToast(Context context, String message)
  3. showToast(Context context, int id)
  4. showToast(Context context, int stringId)
  5. showToast(String text, Context context)
  6. showToast(final Context context, final String message)
  7. showToast(final Context context, final String string)
  8. showToast(final Context context, final int resId)
  9. showToast(final Context ctx, final Handler handler, final String text, final int duration)