Edit text box Processing for Android

Another utility for your Android’s Processing sketch based on Alert Dialog. It can be handy to write a file name to be saved or changing a variable.
Please comment if any error.

import android.content.DialogInterface;
import android.app.Activity;
import android.app.AlertDialog;
import android.text.Editable;
import android.widget.EditText;

Activity act;

void setup() {
  background(#F9C454);
  fill(#4A3A16);
  textSize(38);
  textAlign(CENTER);
  text("Click on screen", width/2, height/3);
}

void draw() {
}  

void dialogBox() {
  act = this.getActivity();
  act.runOnUiThread(new Runnable() {
    public void run() {
      AlertDialog.Builder builder = new AlertDialog.Builder(act);
      final EditText input = new EditText(act); 
      builder.setView(input); 
      builder.setTitle("strokeWeight:");
      builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int whichButton) { 
          background(#F9C454);
          text("Click on screen", width/2, height/3);
          String value = input.getText().toString().trim(); 
          text(value, width/2, height/2);
        }
      }
      );
      builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int whichButton) { 
          dialog.cancel();
        }
      }
      ); 
      builder.show();
    }
  }
  );
}

void mousePressed() {
  dialogBox();
}

1 Like

as this is very good work i wanted to say
it should be in the “examples”
https://android.processing.org/
OOPS not have that ???
but you could pack it:
tutorial
https://android.processing.org/tutorials/index.html
WIKI
https://github.com/processing/processing-android/wiki .

and if you want to set default value to the input text you can set

      AlertDialog.Builder builder = new AlertDialog.Builder(actDialog);
      final EditText input = new EditText(actDialog); 
      builder.setView(input); 
      builder.setTitle("Pocket Smartboard main ip :");
// try to set default value
      if (!HostIP.equals("")) input.setText(HostIP);  

In addition, adding these lines of code will permit the user to enter only numbers:

import android.text.InputType;
...
 builder.setTitle("Enter a number:");
 input.setInputType(InputType.TYPE_CLASS_NUMBER);