I'd like to have simple AlertDialog in my Processing-Android sketch

edited June 2016 in Android Mode

Hi,

I'd like to have simple AlertDialog window with OK confirm button in my processing sketch for Android target...

I've found example code here : http://forum.processing.org/one/topic/android-pop-up-windows#25080000001725349.html

but it doesn't work. It seems that fix is not that hard but is beyond my newbie knowledge in Android field... There is a hint how to solve this but I don't know how to use it (it's commented)...

My code :

//Import required classes
import android.app.AlertDialog;
import android.content.DialogInterface;
//    Activity activity;

void setup() {

//    activity=(Activity) this.context;
//    activity.runOnUiThread(Runnable r) ;

//Create the AlertDialog
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
//Set the title
alertDialog.setTitle("Alert");
//Set the message
alertDialog.setMessage("This is an AlertDialog!");
//Add the OK button
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
  public void onClick(DialogInterface dialog, int which) {
    //The OK button has been clicked
 }});
//Display the AlertDialog
alertDialog.show();
}

void draw() {
}

I get this error :

FATAL EXCEPTION: Animation Thread java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() at android.os.Handler.(Handler.java:121) at android.app.Dialog.(Dialog.java:101) at android.app.AlertDialog.(AlertDialog.java:67) at android.app.AlertDialog$Builder.create(AlertDialog.java:800) at processing.test.popup_test.popup_test.setup(popup_test.java:35) 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:1019)

Would kindly ask for example code that works or any other help or pointer...

Thanks in advance, regards, Bulek.

Answers

  • edited March 2014

    You must run the code on the UI thread - it looks like you have an attempt to do this commented out... try this instead:

    void setup() {
      //Run code on the UI thread...
      runOnUiThread(new Runnable() {
        @ Override
        public void run() {
          //Create the alert dialog
          createAlertDialog();
        }
      }
    }
    
    //Empty draw()...
    void draw() {}
    
    //Function for creating alert dialog
    //We need the separate function so that "this" behaves properly
    void createAlertDialog() {
      //Create the AlertDialog
      AlertDialog alertDialog = new AlertDialog.Builder(this).create();
      //Set the title
      alertDialog.setTitle("Alert");
      //Set the message
      alertDialog.setMessage("This is an AlertDialog!");
      //Add the OK button
      alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
          //The OK button has been clicked
       }});
      //Display the AlertDialog
      alertDialog.show();
    }
    
  • edited March 2014

    Hi,

    thanks for help. With some slight corrections this works now... I guess in this way I could use also some more widgets from Android world.

    Can I do that? If yes, how would I get information back - for instance if I use Radio Button - how I can read selected value ?

    Thanks in advance, regards, Bulek.

    Current code :

    //Import required classes
    import android.app.AlertDialog;
    import android.content.DialogInterface;
    
    
    int Counter = 0;
    String Title = "Alert Dialog!";
    String Message = "this is a message!";
    
    void setup() {
    }
     
    //Empty draw()...
    void draw() {}
     
    //Function for creating alert dialog
    //We need the separate function so that "this" behaves properly
    void createAlertDialog(String Title, String Message) {
      //Create the AlertDialog
      AlertDialog alertDialog = new AlertDialog.Builder(this).create();
      //Set the title
      alertDialog.setTitle(Title);
      //Set the message
      alertDialog.setMessage(Message);
      //Add the OK button
      alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
          //The OK button has been clicked
       }});
      //Display the AlertDialog
      alertDialog.show();
    }
    
    
    
    // Touch pauses (freezes) app and draws real spectrogram (really slow - yet to figure out)
    void mousePressed()
    {
      Counter++;
      
      //Run code on the UI thread...
      runOnUiThread(new Runnable() {
        @ Override
        public void run() {
          //Create the alert dialog
          createAlertDialog(Title,Message + Counter);
        }
      });
    }
    
  • You cannot integrate widgets directly into the layout of your sketch. However, it may be possible to have popup windows that contain radio buttons, similar to the alert dialog above. The use largely depends on how you choose to implement these widgets.

  • Pop up is fine for my need... I guess it is pretty similar to above example with AlertDialog. But I still don't know how to get data back from widget - for instance if I run radio button in pop up - but how do I get information from widget back (for instance if radio button is checked or not) ?

    If there is any example, I'd love to study it ...

    Thanks in advance, regards, Bulek.

  • Again, the solution depends on your implementation. Generally, you have a reference to the UI elements in the popup as you are constructing it - there, you would attach listeners to detect changes. There are many tutorials available, such as the official Android one (Note: You will want to ignore the XML, but the ideas are still the same...).

  • I got same problem and gave up , thank you for information

  • edited March 2014

    Hi,

    I'm trying and I think I'm quite near the solution... I'm adding choices to dialog (have found a way to add positive and negative buttons).. Now I get error :

    [javac] C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\android7661853605103653922sketch\src\processing\test\alertdialog_apidemo\AlertDialog_APIDemo.java:55: error: cannot find symbol [javac] alertDialog.setItems(items, new DialogInterface.OnClickListener() { [javac] ^ [javac] symbol: method setItems(CharSequence[],) [javac] location: variable alertDialog of type AlertDialog [javac] 1 error

    Any advice how to finish that ?

    The source code is :

    
    //Import required classes
    import android.app.AlertDialog;
    import android.app.AlertDialog.Builder;
    import android.app.AlertDialog.Builder.*;
    import android.app.AlertDialog.*;
    import android.content.DialogInterface;
    
    final CharSequence[] items = {"Red", "Green", "Blue"};
    int Counter = 0;
    String Title = "Alert Dialog!";
    String Message = "this is a message!";
    
    void setup() {
    }
     
    //Empty draw()...
    void draw() {}
     
    //Function for creating alert dialog
    //We need the separate function so that "this" behaves properly
    void createAlertDialog(String Title, String Message) {
      //Create the AlertDialog
      AlertDialog alertDialog = new AlertDialog.Builder(this).create();
      //Set the title
      alertDialog.setTitle(Title);
      //Set the message
      alertDialog.setMessage(Message);
      
      alertDialog.setTitle("Pick a choice");
      alertDialog.setItems(items, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
          println ("Choice " + which + " !");
       }});
      
      //Add the OK button
      alertDialog.setButton( DialogInterface.BUTTON_POSITIVE,"OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
          //The OK button has been clicked
          println ("OK Pressed!");
       }});
    
      alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE,"Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
          //The OK button has been clicked
          println ("Cancel Pressed!");
       }});
      //Display the AlertDialog
      alertDialog.show();
    }
    
    
    // Touch pauses (freezes) app and draws real spectrogram (really slow - yet to figure out)
    void mousePressed()
    {
      Counter++;
      
        //Run code on the UI thread...
      runOnUiThread(new Runnable() {
        @ Override
        public void run() {
          //Create the alert dialog
          createAlertDialog("This is AlertDialog","You've started me for " + Counter + " times already!");
        }
      });
    
        //Run code on the UI thread...
      runOnUiThread(new Runnable() {
        @ Override
        public void run() {
          //Create the alert dialog
          createAlertDialog(Title,Message + Counter);
        }
      });
    }
  • It looks like you want AlertDialog.Builder instead of an AlertDialog - this allows you to specify parameters (non-xml) for the creation of an AlertDialog. Indeed, setItems() is an AlertDialog.Builder function! The Android Developers Dialogs reference has a decent example of how to do this.

  • Hi,

    thanks for info. Now I got past that error, but there is still something missing.... Message now doesn't appear, only empty rectangle without any text or single-choice list...

    Can you please help me to get this working ?

    
    //Import required classes
    import android.app.AlertDialog;
    import android.app.AlertDialog.Builder;
    import android.app.AlertDialog.Builder.*;
    import android.app.AlertDialog.*;
    import android.content.DialogInterface;
    
    final CharSequence[] items = {"Red", "Green", "Blue"};
    int Counter = 0;
    String Title = "Alert Dialog!";
    String Message = "this is a message!";
    boolean[] checkedItems= new boolean[3];
    void setup() {
    }
     
    //Empty draw()...
    void draw() {}
     
    //Function for creating alert dialog
    //We need the separate function so that "this" behaves properly
    void createAlertDialog(String Title, String Message) {
      //Create the AlertDialog.Builder
      AlertDialog.Builder alertDialogB = new AlertDialog.Builder(this);
      alertDialogB.setItems(items, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
          println ("Choice " + which + " !");
       }});
      
      alertDialogB.setMessage(Message);
      //Create the AlertDialog
    //  AlertDialog alertDialog = new AlertDialog.Builder(this).create();
      AlertDialog alertDialog = alertDialogB.create();
      //Set the title
      alertDialog.setTitle(Title);
      //Set the message
      alertDialog.setMessage(Message);
      
      //Add the OK button
      alertDialog.setButton( DialogInterface.BUTTON_POSITIVE,"OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
          //The OK button has been clicked
          println ("OK Pressed!");
       }});
    
      alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE,"Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
          //The OK button has been clicked
          println ("Cancel Pressed!");
       }});
      //Display the AlertDialog
      alertDialog.show();
    }
    
    
    // Touch pauses (freezes) app and draws real spectrogram (really slow - yet to figure out)
    void mousePressed()
    {
      Counter++;
      
        //Run code on the UI thread...
      runOnUiThread(new Runnable() {
        @ Override
        public void run() {
          //Create the alert dialog
          createAlertDialog("This is AlertDialog","You've started me for " + Counter + " times already!");
        }
      });
    
        //Run code on the UI thread...
      runOnUiThread(new Runnable() {
        @ Override
        public void run() {
          //Create the alert dialog
          createAlertDialog(Title,Message + Counter);
        }
      });
    }
    
  • I haven't tested your code yet, but one thing that stands out is how you mix the AlertDialog and AlertDialog.Builder methods. Typically, you send all of the information to the builder, and then you don't touch the actual AlertDialog that it gives you other than to show it (see lines 29 and 36 in your code for a comparison). Some of the functions don't necessarily have an exact match, but the API is still very similar. There is more information at the dialogs link I provided above.

  • Hi,

    thanks for response and info.

    I've successfully created simple OK/Cancel dialog with message without using Builder - only AlertDialog.

    And then I've tried to expand then code also for other types of AlertDialogs (single choice, multi-choice, ...), but it seems I have to keep those in separate functions (some are using AlertDialog only, others using AlertDialog.Builder).

    Will check this in more details,

    thanks.

    Bulek.

  • edited June 2016

    I am just learning how to use Activities in Processing, Tried using Android Developers page, but the confuse me a lot by using XML files. (L try hard tough). Suggest me some links, if you know where I should start from.

    ` //Import required classes import android.app.AlertDialog; import android.content.DialogInterface; import android.app.Activity;

    Activity act;

    AlertDialog alertDialog;

    void setup() { act = this.getActivity(); //Create the AlertDialog alertDialog = new AlertDialog.Builder(this).create(); }

    //Empty draw()... void draw() {}

    void mousePressed() { showpopup(); }

    void showpopup() { //Run code on the UI thread... act.runOnUiThread(new Runnable() { // @ Override public void run() { //Create the alert dialog createAlertDialog(); } }); }

    //Function for creating alert dialog //We need the separate function so that "this" behaves properly void createAlertDialog() {

    //Set the title alertDialog.setTitle("Alert"); //Set the message alertDialog.setMessage("This is an AlertDialog!"); //Add the OK button alertDialog.setButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { //The OK button has been clicked }}); //Display the AlertDialog alertDialog.show(); } `

    The above code belongs to calsign I have just tweaked it a bit. I still get the error

    ` 1. ERROR in C:\Users\BPAS\AppData\Local\Temp\android5786700555396990168sketch\src\processing\test\notifypopup\NotifyPOPUP.java (at line 35) alertDialog = new AlertDialog.Builder(this).create(); ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

    The constructor AlertDialog.Builder(NotifyPOPUP) is undefined

    1 problem (1 error)

    BUILD FAILED C:\Users\BPAS\AppData\Local\Temp\android5786700555396990168sketch\build.xml:15: The following error occurred while executing this line: C:\Users\BPAS\AppData\Local\Temp\android5786700555396990168sketch\build.xml:28: Compile failed; see the compiler error output for details.

    Total time: 1 second `

    I tried hard solving it on my own, but I was not able to do it. So brought it to you guys. Please help .

  • edited June 2016

    @Aswinth_Raj===

    There are 3 errors in your code + 1 problem

    • You are (P3) in a fragment, use "my" act instead of "this"
    • You have to call Looper.prepare() in setup()
    • Your code for creating the alert dialog is deprecated (see the other example or go to android docs)

    problem is that your ok button can do nothing till now, though it seems is doing

  • Thanks for the guidance, But I only got one error and one problem when I Ran it n my device. I have shown it below '---------- 1. ERROR in C:\Users\BPAS\AppData\Local\Temp\android8028517109403082643sketch\src\processing\test\notifypopup\NotifyPOPUP.java (at line 35) alertDialog = new AlertDialog.Builder(my).create(); ^^

    my cannot be resolved to a variable

    1 problem (1 error)

    BUILD FAILED C:\Users\BPAS\AppData\Local\Temp\android8028517109403082643sketch\build.xml:15: The following error occurred while executing this line: C:\Users\BPAS\AppData\Local\Temp\android8028517109403082643sketch\build.xml:28: Compile failed; see the compiler error output for details.

    Total time: 1 second '

    Can I get an example program which is working

  • @Aswinth_Raj===

    you get only 1 error because the error is at the very beginning of compiler! and the error is "my cannot be resolved to a variable"... i guess that using my post you have put "my" instead of "this"! but i was thinking (&& refering) to others code example i have given to yo; in these examples i have an "act" variable which is an instance of the Activity class!!! - Look at these examples!

Sign In or Register to comment.