Home > Back-end >  Show Hide / Toggle Soft Keyboard and get what has been typed
Show Hide / Toggle Soft Keyboard and get what has been typed

Time:04-11

I am currently developing a simple library for making games on Android. I am almost done and only want to add a few more features. One feature I would like to add is a method that would show / hide the onscreen-keyboard (soft keyboard). I want to get whatever has been typed and return it using another method.

My codebase consists of an Activity that gets launched and a SurfaceView that does the rendering and touch input (and some other classes for the gameloop, sprites, sound and so on). As this is supposed to be a small library I don't want to mess with the XML.

How would I go on about showing hiding/toggling the keyboard and receiving it's inputs?

CodePudding user response:

Keyboards work via an InputConnection. I you focus a View in your app, the framework will call getInputConnection on that view. If the view returns non-null, it will send all the data from the keyboard and map it to function calls on the InputConnection. So all you need to do is override that function on your SurfaceView and have it return a custom InputConnection.

InputConnection is a large api, but the most important ones for you to implement are commitText (which sends a string of text from the keyboard to the view) and deleteSurroundingText (used for deletion). The rest of the api should be implemented as well, but those are the main ones.

  • Related