Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Make toast in Qt android from Java code
Forum Updated to NodeBB v4.3 + New Features

Make toast in Qt android from Java code

Scheduled Pinned Locked Moved Solved Mobile and Embedded
9 Posts 3 Posters 2.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AmrCoder
    wrote on 26 Oct 2016, 12:13 last edited by
    #1

    I try to use Toast function from java in android using qt 5.6.1
    I add the java file in

    $PWD/android/src/org/qtproject/example/amr
    

    then i add

    QT += androidextras
    

    in the .pro file
    the java code

    package org.qtproject.example.amr;
    
    import android.app.Notification;
    import android.app.NotificationManager;
    import android.content.Context;
    import android.widget.Toast;
    
    import org.qtproject.qt5.android.bindings.QtApplication;
    import org.qtproject.qt5.android.bindings.QtActivity;
    
    public class Main extends org.qtproject.qt5.android.bindings.QtActivity
    {
    
      public static int getSize()
      {
      return 111;
      }
      public void MakeToast(){
      Toast.makeText(Main.this,"Hello world",Toast.LENGTH_LONG).show();
      }
    
    }
    

    the c++ code

    #include "widget.h"
    #include "ui_widget.h"
    #include <QDebug>
    
    Widget::Widget(QWidget *parent) :
      QWidget(parent),
      ui(new Ui::Widget)
    {
      ui->setupUi(this);
    }
    
    Widget::~Widget()
    {
      delete ui;
    }
    
    void Widget::on_pushButton_clicked()
    {
      //QAndroidJniObject::callStaticMethod<void>("org/qtproject/example/notification/Main", "MakeToast");
    // QAndroidJniObject obj;
    // obj.callMethod<void>("org/qtproject/example/notification/Main","MakeToast");
      QtAndroid::androidActivity().callMethod<void>("org/qtproject/example/notification/Main","MakeToast");
    }
    

    but when i press the button to see the toast i don't get any response for that how to fix that
    Thanks

    D R 2 Replies Last reply 26 Oct 2016, 12:37
    0
    • A AmrCoder
      26 Oct 2016, 12:13

      I try to use Toast function from java in android using qt 5.6.1
      I add the java file in

      $PWD/android/src/org/qtproject/example/amr
      

      then i add

      QT += androidextras
      

      in the .pro file
      the java code

      package org.qtproject.example.amr;
      
      import android.app.Notification;
      import android.app.NotificationManager;
      import android.content.Context;
      import android.widget.Toast;
      
      import org.qtproject.qt5.android.bindings.QtApplication;
      import org.qtproject.qt5.android.bindings.QtActivity;
      
      public class Main extends org.qtproject.qt5.android.bindings.QtActivity
      {
      
        public static int getSize()
        {
        return 111;
        }
        public void MakeToast(){
        Toast.makeText(Main.this,"Hello world",Toast.LENGTH_LONG).show();
        }
      
      }
      

      the c++ code

      #include "widget.h"
      #include "ui_widget.h"
      #include <QDebug>
      
      Widget::Widget(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::Widget)
      {
        ui->setupUi(this);
      }
      
      Widget::~Widget()
      {
        delete ui;
      }
      
      void Widget::on_pushButton_clicked()
      {
        //QAndroidJniObject::callStaticMethod<void>("org/qtproject/example/notification/Main", "MakeToast");
      // QAndroidJniObject obj;
      // obj.callMethod<void>("org/qtproject/example/notification/Main","MakeToast");
        QtAndroid::androidActivity().callMethod<void>("org/qtproject/example/notification/Main","MakeToast");
      }
      

      but when i press the button to see the toast i don't get any response for that how to fix that
      Thanks

      D Offline
      D Offline
      Devopia53
      wrote on 26 Oct 2016, 12:37 last edited by Devopia53
      #2

      @AmrCoder

      Hi.

      First, you need to check your java class name.
      Probably it will be "org/qtproject/example/amr/Main" instead of "org/qtproject/example/notification/Main".

      See check using QAndroidJniObject::isClassAvailable()

      1 Reply Last reply
      0
      • A Offline
        A Offline
        AmrCoder
        wrote on 26 Oct 2016, 12:46 last edited by
        #3

        ok i fix it and i check QAndroidJniObject::isClassAvailable() and return true
        but when i press button i don't see the toast !

        1 Reply Last reply
        0
        • A Offline
          A Offline
          AmrCoder
          wrote on 26 Oct 2016, 13:07 last edited by
          #4

          what i have reached

          void Widget::on_pushButton_clicked()
          {
              QtAndroid::androidActivity().callMethod<void>("org/qtproject/example/amr/Main","MakeToast");
              qDebug() << "class avalible = " << QAndroidJniObject::isClassAvailable("org/qtproject/example/amr/Main"); // return true
          }
          
          1 Reply Last reply
          0
          • A AmrCoder
            26 Oct 2016, 12:13

            I try to use Toast function from java in android using qt 5.6.1
            I add the java file in

            $PWD/android/src/org/qtproject/example/amr
            

            then i add

            QT += androidextras
            

            in the .pro file
            the java code

            package org.qtproject.example.amr;
            
            import android.app.Notification;
            import android.app.NotificationManager;
            import android.content.Context;
            import android.widget.Toast;
            
            import org.qtproject.qt5.android.bindings.QtApplication;
            import org.qtproject.qt5.android.bindings.QtActivity;
            
            public class Main extends org.qtproject.qt5.android.bindings.QtActivity
            {
            
              public static int getSize()
              {
              return 111;
              }
              public void MakeToast(){
              Toast.makeText(Main.this,"Hello world",Toast.LENGTH_LONG).show();
              }
            
            }
            

            the c++ code

            #include "widget.h"
            #include "ui_widget.h"
            #include <QDebug>
            
            Widget::Widget(QWidget *parent) :
              QWidget(parent),
              ui(new Ui::Widget)
            {
              ui->setupUi(this);
            }
            
            Widget::~Widget()
            {
              delete ui;
            }
            
            void Widget::on_pushButton_clicked()
            {
              //QAndroidJniObject::callStaticMethod<void>("org/qtproject/example/notification/Main", "MakeToast");
            // QAndroidJniObject obj;
            // obj.callMethod<void>("org/qtproject/example/notification/Main","MakeToast");
              QtAndroid::androidActivity().callMethod<void>("org/qtproject/example/notification/Main","MakeToast");
            }
            

            but when i press the button to see the toast i don't get any response for that how to fix that
            Thanks

            R Offline
            R Offline
            raven-worx
            Moderators
            wrote on 26 Oct 2016, 13:16 last edited by
            #5

            @AmrCoder said in Make toast in Qt android from Java code:

            Toast.makeText(Main.this,"Hello world",Toast.LENGTH_LONG).show();

            JNI calls made from C++ are running in the Qt thread. But Toast.makeText() must be called from the Android UI thread.
            This should be visible by a thrown exception in the console output.

            So either you change your Java code to run on the UI thread or you use Qt 5.7 to make the call explicitly on the UI thread:

            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");
                });
            }
            

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            4
            • A Offline
              A Offline
              AmrCoder
              wrote on 26 Oct 2016, 14:35 last edited by
              #6

              Thank you very much i install qt 5.7 and use this function it works

              D 1 Reply Last reply 27 Oct 2016, 12:26
              0
              • A AmrCoder
                26 Oct 2016, 14:35

                Thank you very much i install qt 5.7 and use this function it works

                D Offline
                D Offline
                Devopia53
                wrote on 27 Oct 2016, 12:26 last edited by
                #7

                @AmrCoder

                This introduces a different way using Java... like your code.

                // in your java code

                [...]
                import android.os.Handler;
                [...]
                
                private Handler     handler = new Handler();
                
                public void MakeToast() {
                    handler.post(new Runnable() {
                        @Override
                        public void run () {
                            Toast.makeText(getApplicationContext(), "Hello world", Toast.LENGTH_LONG).show();
                        }
                    });
                }
                
                [...]
                

                // in your main.cpp

                void Widget::on_pushButton_clicked()
                {
                    QtAndroid::androidActivity().callMethod<void>("MakeToast");
                }
                
                1 Reply Last reply
                1
                • A Offline
                  A Offline
                  AmrCoder
                  wrote on 27 Oct 2016, 19:20 last edited by
                  #8

                  i didn't work for me this my full java class

                  package org.qtproject.example.amr;
                  
                  import android.content.Context;
                  import android.os.Environment;
                  import android.os.Handler;
                  import android.widget.*;
                  
                  public class Main extends org.qtproject.qt5.android.bindings.QtActivity
                  {
                      private Handler     handler = new Handler();
                  
                      public void MakeToast() {
                          handler.post(new Runnable() {
                              @Override
                              public void run () {
                                  Toast.makeText(getApplicationContext(), "Hello world", Toast.LENGTH_LONG).show();
                              }
                          });
                      }
                  }
                  
                  

                  what i do wrong here ?

                  D 1 Reply Last reply 28 Oct 2016, 11:24
                  0
                  • A AmrCoder
                    27 Oct 2016, 19:20

                    i didn't work for me this my full java class

                    package org.qtproject.example.amr;
                    
                    import android.content.Context;
                    import android.os.Environment;
                    import android.os.Handler;
                    import android.widget.*;
                    
                    public class Main extends org.qtproject.qt5.android.bindings.QtActivity
                    {
                        private Handler     handler = new Handler();
                    
                        public void MakeToast() {
                            handler.post(new Runnable() {
                                @Override
                                public void run () {
                                    Toast.makeText(getApplicationContext(), "Hello world", Toast.LENGTH_LONG).show();
                                }
                            });
                        }
                    }
                    
                    

                    what i do wrong here ?

                    D Offline
                    D Offline
                    Devopia53
                    wrote on 28 Oct 2016, 11:24 last edited by
                    #9

                    @AmrCoder

                    I created Qt package for Android Toast.
                    You can test after download the package from this link.

                    1 Reply Last reply
                    0

                    1/9

                    26 Oct 2016, 12:13

                    • Login

                    • Login or register to search.
                    1 out of 9
                    • First post
                      1/9
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved