Toast in processing

edited June 2016 in Android Mode

I want to use toast on a button click how can i do it in java mode and in android mode as well kindly do tell me the way

Answers

  • Toasts are built into Android - see the relevant API Guidelines. Something like this should do the trick:

    void showToast(String message) {
      android.widget.Toast.makeText(getApplicationContext(), message, android.widget.Toast.LENGTH_SHORT).show();
    }
    

    Java mode, on the other hand, does not have the same functionality, although there is probably a library (not necessarily a Processing library) that will do the trick.

  • kindly tell me the name of the library plz

  • edited August 2014

    i run the code as an example here is my code

    import android.widget.Toast;
    
    
    void setup()
    {
      size(displayWidth,displayHeight);
    }
    
    void draw()
    {
      background(255);
    }
    
    void mousePressed()
    {
       showToast("Hellow World");
    }
    
    void showToast(String message) {
      android.widget.Toast.makeText(getApplicationContext(), message, android.widget.Toast.LENGTH_SHORT).show();
    }
    

    but i found error when application run on the screen here are errors

    java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
        at android.os.Handler.<init>(Handler.java:205)
        at android.os.Handler.<init>(Handler.java:119)
        at android.widget.Toast$TN.<init>(Toast.java:339)
        at android.widget.Toast.<init>(Toast.java:98)
        at android.widget.Toast.makeText(Toast.java:245)
        at processing.test.toast.toast.showToast(toast.java:40)
        at processing.test.toast.toast.mousePressed(toast.java:36)
        at processing.core.PApplet.mousePressed(Unknown Source)
        at processing.core.PApplet.handleMouseEvent(Unknown Source)
        at processing.core.PApplet.dequeueEvents(Unknown Source)
        at processing.core.PApplet.handleDraw(Unknown Source)
        at processing.core.PGraphicsAndroid2D.requestDraw(Unknown Source)
        at processing.core.PApplet.run(Unknown Source)
        at java.lang.Thread.run(Thread.java:838)
    FATAL EXCEPTION: Animation Thread
    java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
        at android.os.Handler.<init>(Handler.java:205)
        at android.os.Handler.<init>(Handler.java:119)
        at android.widget.Toast$TN.<init>(Toast.java:339)
        at android.widget.Toast.<init>(Toast.java:98)
        at android.widget.Toast.makeText(Toast.java:245)
        at processing.test.toast.toast.showToast(toast.java:40)
        at processing.test.toast.toast.mousePressed(toast.java:36)
        at processing.core.PApplet.mousePressed(Unknown Source)
        at processing.core.PApplet.handleMouseEvent(Unknown Source)
        at processing.core.PApplet.dequeueEvents(Unknown Source)
        at processing.core.PApplet.handleDraw(Unknown Source)
        at processing.core.PGraphicsAndroid2D.requestDraw(Unknown Source)
        at processing.core.PApplet.run(Unknown Source)
        at java.lang.Thread.run(Thread.java:838)
    
  • I don't know Android programming, but the error message seems explicit: "Can't create handler inside thread that has not called Looper.prepare()"

  • Answer ✓

    Oh, right. This code has to be run on the UI thread, like so:

    void showToast(final String message) {
      runOnUiThread(new Runnable() {
        public void run() {
          android.widget.Toast.makeText(getApplicationContext(), message, android.widget.Toast.LENGTH_SHORT).show();
        }
      });
    }
    
  • thanks u calsign it works

  • calsign do tell me the library for showing message in java mode thanks in advance

  • @calsign Is it possible to launch a toast even when the app is closed. Sorry if i sound rubbish!

  • edited May 2016

    For the code

    import android.widget.Toast;
    
    void setup()
    {
      size(displayWidth,displayHeight);
    }
    
    void draw()
    {
      background(255);
    }
    
    void mousePressed()
    {
       showToast("Hellow World");
    }
    
    void showToast(final String message) {
      runOnUiThread(new Runnable() {
        public void run() {
          android.widget.Toast.makeText(getApplicationContext(), message, android.widget.Toast.LENGTH_SHORT).show();
        }
      });
    }
    

    I get the following ERROR

    1. ERROR in C:\Users\BPAS\AppData\Local\Temp\android4289731801464903815sketch\src\processing\test\notify\notify.java (at line 40)
        runOnUiThread(new Runnable() {
        ^^^^^^^^^^^^^
    The method runOnUiThread(new Runnable(){}) is undefined for the type notify
    ----------
    2. ERROR in C:\Users\BPAS\AppData\Local\Temp\android4289731801464903815sketch\src\processing\test\notify\notify.java (at line 42)
        android.widget.Toast.makeText(getApplicationContext(), message, android.widget.Toast.LENGTH_SHORT).show();
                                      ^^^^^^^^^^^^^^^^^^^^^
    The method getApplicationContext() is undefined for the type new Runnable(){}
    ----------
    2 problems (2 errors)
    

    PLEASE HELP!!!

  • @Aswinth_Raj===

    • of course: you are in a fragment...
    • and no, you cannot launch a toast when your app is "closed" (what do you mean by "closed"??? - Is it "paused" - is it "destroyed???")
  • @calsign===

    you are not in an activity but in a fragment; so you have to write::

        //Activity act = this.getActivity();
    
        void showToast(final String message) {
          act.runOnUiThread(new Runnable() {
            public void run() {
              android.widget.Toast.makeText(act.getApplicationContext(), message, android.widget.Toast.LENGTH_SHORT).show();
            }
          }); 
    

    [-X

  • Thanks a lot guys!!

    'import android.widget.Toast; import android.app.Activity;

    Activity act;

    void setup() { size(displayWidth,displayHeight); act = this.getActivity(); }

    void draw() { background(255); }

    void mousePressed() { showToast("Hellow World"); }

    void showToast(final String message) { act.runOnUiThread(new Runnable() { public void run() { android.widget.Toast.makeText(act.getApplicationContext(), message, android.widget.Toast.LENGTH_SHORT).show(); } }); } '

    I got it working! Thanks again

Sign In or Register to comment.