Is there a way to just open the soft keyboard, without an actual dialog nor an input field, and get the input value in a string? the keyboard itself already has a "done" button; can I just: press a button, keyboard opens with its own builtin inputbox, enter value, press "done", get result in a variable.
Asked
Active
Viewed 393 times
2 Answers
1
I would add an editText an make it invisible and put on the focus on it. To show it explicitly:
editText = (EditText)findViewById(R.id.myTextViewId);
editText.requestFocus();
InputMethodManager imm = (InputMethodManager)getSystemService(this.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,InputMethodManager.HIDE_IMPLICIT_ONLY);
and hide it again
InputMethodManager imm = (InputMethodManager) getSystemService(this.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);

Tobias Lukoschek
- 355
- 1
- 3
- 20
-
1How do I handle "end of input", to get the input value and hide the keyboard? – kekk0 Jul 19 '21 at 19:41
-
you can use a TextChangeListener on the editText objekt. like editText.addTextChangedListener(new TextWatcher() { ... } – Tobias Lukoschek Jul 19 '21 at 19:45
-
It opens the keyboard, but without the builtin input box and the "done" button, so it doesn't show the input value; also it seems to ignore any attempt to restrict to numeric input. – kekk0 Jul 19 '21 at 20:09
0
I designed a different solution, inspired by Tobias suggestions plus this answer. I created an AlertDialog popup with an EditText in it. Then I added a delayed "touch" into the edittext to open the soft keyboard, then handled the "done" event to get the input value and also close the underlying popup. Here is some sample code:
//Open an AlertDialog popup
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
//create a simple EditText
final EditText input = new EditText(activity);
input.setInputType(InputType.TYPE_CLASS_NUMBER);
input.setImeOptions(EditorInfo.IME_ACTION_DONE);
builder.setView(input);
//touch into the EditText to open the softkeyboard (600ms delay)
new Handler().postDelayed(new Runnable() {
public void run() {
input.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN , 0, 0, 0));
input.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP , 0, 0, 0));
}
}, 600);
alertDialog = builder.show();
AlertDialog finalAlertDialog = alertDialog;
//Handle Keyboard event to get value and close the popup
input.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (actionId <= EditorInfo.IME_ACTION_PREVIOUS)) {
doOperation();
finalAlertDialog.cancel();
}
return false;
}
});

kekk0
- 47
- 7