Skip to content

Qt on Android: How to use Android Toast

To get you started on this, we’ll be using KDAB’s Android utils with the humble Toast. Toasts are small popups which are used to show the user some feedback. Check Google’s API guide for more info about toasts.

The easiest way to show a toast is to use Toast.makeText(Context context, CharSequence text, int duration) static method. This method needs 3 params:

  • the context (we’ll use the activity)
  • the text to show
  • and the duration: one of LENGTH_SHORT (0) and LENGTH_LONG (1).

Then we just need to call the show method.

Of course all the calls MUST happen on the Android UI thread.

As usual we’re going to make use (abuse) of QtAndroidExtras and KDAB’s Android utils (check this article to see how to add them to your project).

Let’s create a simple function to show the toast:

enum Duration {
    SHORT = 0,
    LONG = 1
};

void showToast(const QString &message, Duration duration = LONG) {
    // all the magic must happen on Android UI thread
    QtAndroid::runOnAndroidThread([message, duration] {
        QAndroidJniObject javaString = QAndroidJniObject::fromString(message);
        QAndroidJniObject toast = QAndroidJniObject::callStaticObjectMethod("android/widget/Toast", "makeText",
                                                                            "(Landroid/content/Context;Ljava/lang/CharSequence;I)Landroid/widget/Toast;",
                                                                            QtAndroid::androidActivity().object(),
                                                                            javaString.object(),
                                                                            jint(duration));
        toast.callMethod<void>("show");
    });
}

Yep, it’s that simple!

Now let’s take a closer look at the code:

  • We’re using KDAB::Android::runOnAndroidThread to run everything on the Android UI thread.
  • We’re capturing message and duration params as values not as references, because as we’ve seen in a previous article KDAB::Android::runOnAndroidThread is (most of the time) asynchronous.
  • We’re using QAndroidJniObject::fromString to convert a QString to a QAndroidJniObject string object.
  • We’re using QAndroidJniObject::callStaticObjectMethod to call the makeText static method that returns a Toast java object. We need to pass the following parameters:
    • “android/widget/Toast” is the fully qualified class name.
    • “makeText” is the static method name
    • “(Landroid/content/Context;Ljava/lang/CharSequence;I)Landroid/widget/Toast;” – is the method signature. The method returns Landroid/widget/Toast; object and expects 3 arguments:
      • Landroid/content/Context; – a Content object
      • Ljava/lang/CharSequence; – a CharSequence object
      • I – an int
    • QtAndroid::androidActivity().object(), javaString.object(), jint(duration) are the makeText arguments.
  • after we get the toast object, we call show method and we’re done.

Enjoy!

showToast(QLatin1String("Hello from Qt"));
FacebookTwitterLinkedInEmail

5 thoughts on “Qt on Android: How to use Android Toast”

Leave a Reply

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