Better_Software_Header_MobileBetter_Software_Header_Web

Find what you need - explore our website and developer resources

Qt on Android: How to use Android 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");
    });
}

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

5 Comments

18 - Sept - 2017

Igbaria

19 - Sept - 2017

BogDan Vatra

17 - Oct - 2017

Taimoor

17 - Oct - 2017

BogDan Vatra

20 - Apr - 2018

Mynor Choc