Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get input text from soft keyboard

Tags:

java

android

I am launching the soft keyboard like this:

InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

inputMethodManager.toggleSoftInputFromWindow(buttonLayout.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0);

buttonLayout is a simple button on my UI.
How do I extract what the user wrote (without using a EditText field or maybe hiding the EditText) so the user can't see or click on it?

like image 217
J_Strauton Avatar asked Feb 23 '16 01:02

J_Strauton


1 Answers

Without an EditText you're going to have a hard time.

An InputMethod needs to be connected to a view. Whatever view you use, you need to override onCreateInputConnection to return a custom InputConnection object that at a minimum implements commitText (for word input), deleteSurroundingText (for deletes), and sendKeyEvent (for keyboards that assume you're in dumb mode), and all of the completion functions. Input connections are complicated things and you'll screw up 3rd party keyboards like Swiftkey and Swype if you don't get it right. I really don't suggest doing this.

If you want to do this your best chance of getting it right is to claim your window is a TYPE_NULL input type. Most keyboards will dumb themselves down and assume you only accept the simplest commands in that mode. But you can't count on it.

I'd look at the InputConnection returned by the EditText class and copy as much of it as possible.

like image 174
Gabe Sechan Avatar answered Sep 23 '22 23:09

Gabe Sechan