Improve alert function

edited December 2017 in Android Mode

I needed to display alerts in processing android so I got some code from another post and boiled it down to this

import android.app.AlertDialog;

String alertMessage;

void setup() {
  alertMessage = "This is the message";
  createAlert();
}

void createAlert() {
  runOnUiThread(new Runnable() {
    @ Override
      public void run() {
      AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity());
      alertDialog.setTitle("Alert");
      alertDialog.setMessage(alertMessage);
      alertDialog.setPositiveButton("OK", null);
      alertDialog.show();
    }
  }
  );
}

Which works great but now to display some text in an alert I have to change the alertMessage and then call the function, there must be a way to turn it into a function that takes in a string, something like this createAlert("This is the message"); but I cant figure out how to do it with the UiThread being inside the function.

Any advice?

Tagged:

Answers

  • Something like what ketai does: http://ketai.org/reference/ui/ketaialertdialog/

    You can always explore their source code. On the side, possible relevant code, specially if you are looking for the missing imports:

    https://forum.processing.org/two/discussion/3715/i-d-like-to-have-simple-alertdialog-in-my-processing-android-sketch

    which are (some of them redundant):

    //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;
    

    Kf

  • @schotsl===

    before trying to answer your question, are you sure that the code you have put can run?

  • @akenaton @kfrajer Im sorry I forgot to add the import, the only import you need for this code to run is import android.app.AlertDialog;. Then the code will run

  • Code runs... confirmed!

    Kf

  • edited December 2017

    @kfrajer===

    sorry; as for me line 12 fires: The function "runOnUiThread(Runnable(){})" does not exist s" ;and in order to get rid of it i must add act.run..... (act = Activity getActivity) which seems right as you 're in a fragment.

    @shotsl=== not sure to understand your question; yet have a look to the code below which runs well for me:

        String alertMessage = "";
         import android.app.AlertDialog;
        import android.app.AlertDialog.Builder;
        import android.os.Looper;
        import android.app.Activity;
        import android.content.DialogInterface;
    
    
        Activity act;
    
        void setup() {
          size(1024,768);
          orientation(LANDSCAPE);
          act=this.getActivity();
          Looper.prepare();
    
        };
    
        void draw(){
    
        };
    
        void createAlert() {
          act.runOnUiThread(new Runnable() {
            //@ Override
              public void run() {
    
              AlertDialog.Builder builder = new AlertDialog.Builder(act);
              AlertDialog alertDialog = builder.create();
              alertDialog.setTitle("Alert");
              alertDialog.setMessage(getMessage());
              alertDialog.setButton("OK", new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int which) {
           println("cliqué=======");
    
        }}); 
              alertDialog.show();
    
        }
        });
        };
    
         void mousePressed(){
            setMessage("encore un");// here you can set the message to some String[x];
            createAlert();
          }
    
        void setMessage(String mess){
    
          alertMessage = mess;
        }
    
        String getMessage(){
    
          return alertMessage;
        } 
    
  • edited December 2017

    @akenaton It's weird that it works for me and @kfrajer but not for you right? Thanks for the improvement anyway :P

    My question was If I could turn createAlert into a function that requests a string, createAlert("This is the message"); instead of (what im currently doing):

    alertMessage = "This is the message";
    createAlert();
    

    Altough now I release I could just create a function with something like this

    void displayAlert(String message) {
      alertMessage = message;
      createAlert();
    }
    

    This does feel a bit dodgy though..

Sign In or Register to comment.