Open numeric keyboard on Android

Hello,

Is it possible to open numeric keyboard on Android phone ?
I use openKeyboard() in Android mode, it open text keyboard.
I put android:reqKeyboardType=“twelvekey” in AndroidManifest.xml but always the text keyboard appears.
Is there a method to do that ?

Thanks by advance
Yves

Hi
Full function virtual keyboard

https://android.processing.org/reference/keyboard/virtual

One more


import android.view.inputmethod.InputMethodManager;
import android.content.Context;

void toggleKeyboard() {
  InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
  imm.toggleSoftInput(0, 0);
}

/*
 * This example includes a function to toggle (show/hide) the software keyboard 
 * and illustrates how to edit a string using the keyboard.
 *
 * Pressing delete/backspace will delete one character; if none are left it will hide the keyboard.
 * Pressing return will hide the keyboard.
 *
 * In Android, you can also use the back arrow to close the keyboard. As this sketch doesn't explicitly
 * open or close the keyboard (just toggles its state), you may come to an inconsistent behaviour.
 *
 * The function is in a separate tab, so it may easily be copied to another sketch.
 *
 * NOTE: this sketch does not work well with autocomplete, which should be off by default.
 *
 * Tiago Martins 2017-2020
 * https://github.com/tms-martins/processing-androidExamples
 */


// the message to be edited and displayed on screen
String message = "Tap the screen to toggle the keyboard and edit this message.";


void setup() {
  fullScreen();
  orientation(PORTRAIT);
  
  // set the text size and drawing parameters
  textSize(24 * displayDensity);
  textAlign(CENTER, TOP);
  fill(0);
  noStroke();
}


void draw() {
  background(255);
  text(message, 10, height/6, width-20, height);
}


void mousePressed() {
  toggleKeyboard();
}


void keyPressed() {
  if (key == CODED) {
    println("Pressed keycode " + (int) keyCode);

    // the user presses the "delete" key, delete the last letter
    // or close the keyboard, if there are no characters to delete
    if (keyCode == 67) {
      if (message.length() > 0) {
        message = message.substring(0, message.length()-1);
      }
      else {
        toggleKeyboard();
      }
    }
  }
  else {
    println("Pressed key " + (int) key);
    
    // if the user input a character, add it to the message;
    // otherwise if she pressed "return" close the keyboard
    if (isCharacterNumberOrSign(key)) {
      message += (char)key;
    }
    else if (key == 10) {
      toggleKeyboard();
    }
  }
}


// returns true for characters, numbers or signs which we may want to write
// you are welcome to add/remove according to your needs, or make a different version of this function
boolean isCharacterNumberOrSign(int keyPress) {
  // lowercase characters (a to z)
  if (keyPress >= 'a' && keyPress <= 'z') return true;
  // uppercase characters (A to Z)
  if (keyPress >= 'A' && keyPress <= 'Z') return true;
  // digits (0 to 9)
  if (keyPress >= '0' && keyPress <= '9') return true;
  // signs and other special characters
  if (keyPress == ' ' || keyPress == '-' || keyPress == '_' || keyPress == '.' ||  keyPress == ',' || keyPress == ';' || keyPress == '?' || keyPress == '!' || keyPress == '\'') return true;    
 
  return false;
}

Thanks for reply, but toggleKeyboard(), like openKeyboard() open it only in text mode not in numeric mode.
I see something with EditText but i don’t understand how to use EditText in Processing.
Thanks

You are correct that you need to use EditText to get a numeric keypad. This may be accomplished by adding a single line to your EditText code:

editText.setInputType(InputType.TYPE_CLASS_NUMBER);

Example code is shown below:
Reference:https://stackoverflow.com/questions/13817521/how-do-you-set-the-edittext-keyboard-to-only-consist-of-numbers-on-android

import android.app.Activity;
import android.content.Context;  
import android.view.View;   
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.view.ViewGroup.LayoutParams;
import android.text.Editable;
import android.view.inputmethod.InputMethodManager;
import android.view.KeyEvent;
import android.view.InputEvent;
import android.widget.FrameLayout;
import android.R;
import android.text.InputType;

color BLUE = color(64,124,188);
color LTGRAY = color(185,180,180);
color YELLOW = color(245,250,13);
color RED = color(255,0,0);
color BLACK = color(0,0,0);
color WHITE = color(255,255,255);
color GREEN = color(32,175,47);
color ORANGE = color(237, 147, 29);

Activity activity;
Context ctx;
EditText editText;

void editText(int x, int y, int w, int h, color bkgrnd, color txtColor, float txtSize) {
  editText = new EditText(ctx);
  editText.setLayoutParams (new RelativeLayout.LayoutParams (RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
  editText.setTextColor(txtColor);
  editText.setBackgroundColor(bkgrnd);
  editText.getLayoutParams().width = w;
  editText.getLayoutParams().height = h;
  editText.setX(x);
  editText.setY(y);
  editText.setTextSize(txtSize);
  editText.setInputType(InputType.TYPE_CLASS_NUMBER);
  editText.setOnKeyListener(new View.OnKeyListener() {
    @ Override
      public boolean onKey(View v, int keyCode, KeyEvent event) {
      if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode()== KeyEvent.KEYCODE_ENTER) {
       InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
        String value = editText.getText().toString();
        println(value);
        return true;
      }
      return false;
    }
  }
  );
  FrameLayout layout = (FrameLayout)activity.findViewById(R.id.content);
  layout.addView(editText);
}


void setup() {
  fullScreen();
  orientation(LANDSCAPE);
  background(BLUE);
  activity = this.getActivity();
  ctx = activity.getApplicationContext();
   runOnUiThread(new Runnable() {
      void run() {
      editText(300, 300, 800, 100, ORANGE, BLACK, 28.0);
    }
  }
  );
  
}

void draw() {
  
}

Thanks a lot Svan, it works fine.
I didn’t know i had to import so much librairies, it is like include in C .
It works but i don’t know how get the string from the editText ?

Cheers
Yves

See revision. You may not need all those imports; depends on what you are trying to do. Experiment to see which ones you don’t need.

Thanks for your replies,
I read the return of editText by

if(editText.getText().toString().equals("XXXXX"))
I put the editText declaration in second folder so i  use it in first folder by
 runOnUiThread(new Runnable() {
      void run() {
     editText(40, 40, 640, 100, WHITE, BLACK, 28);
    }
  }
  );
 editText.setVisibility(View.INVISIBLE); 

at startup and visible when needed.

It works fine, but seems complicated by comparaison to traditionnal C language.