code: public static void showOnUiThread(final Context applicationContext, final String message, final int duration, Handler handler) {
          handler.post(new Runnable() {
              @Override
              public void run() {
                  Toast.makeText(applicationContext, message, duration).show();
              }
          });
      }
docs: Show toast on the UI thread.
  @param applicationContext the application context.
  @param message the message to show.
  @param duration the display duration.
  @param handler the main handler.