SATURN  RING  STATION

At a distant space view post, we chat about codes.

Android: Show Toast Anywhere

This handles showing a Toast in both main thread or background threads. Meaning you can use it anywhere you like without caring it’s in an Activity, Service, IntentService, AsyncTask or Handler.

 

    if (Looper.myLooper() == Looper.getMainLooper()) {
        Toast.makeText(context, massage, Toast.LENGTH_SHORT).show();
    } else {
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(context, massage, Toast.LENGTH_SHORT).show();
            }
        });
    }

Put it in an Util class as static method. You can also apply style to the Toast based on themes, or use 3rd party Toast library.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Android: Show Toast Anywhere