If you haven’t already done so, then you may want to read, Capturing user input with Android’s TextFields
Did you know?
There are three things you should take care of when using a soft keyboard:
- Specify the input type. What’s the user going to type in? Plain text, numbers, email addresses?
- Specify which action buttons should be displayed
- Decide whether the window should resize when the keyboard appears or should its contents pan so that the user can see what they’re typing?
What’s your type?
You should specify the input type for each text field. The system will then display the correct keyboard for that field.
If you don’t supply an input type, then the system guesses the type of content and tries to display an appropriate keyboard layout.
There are several types of input. Here are some common ones:
- text – the field accepts plain text. A normal text keyboard is displayed
- numbers – the field accepts only numbers. A number keyboard is displayed
- textEmailAddress – the field accepts text to be used as an email address. A normal text keyboard is displayed and will include the @ character and possibly a .com key
Use the inputType attribute to define the input type in your layout, like this:
You can also specify how the keyboard behaves when text is entered. Common behaviours that you can include are:
- Capitalise all characters as they are entered
- Autocorrect the text as it’s entered
You can use the inputType attribute to specify the keyboard’s behaviour on its own. Here’s an example:
You can also combine different input types and behaviours like this:
See the documentation for more inputType attribute values.
Most soft keyboards have a user action button. The user presses this once they’ve finished entering text into a text field. You can listen for when they do this to trigger some code of your own.
You can also specify which action button to use. Here are some options:
- actionNone – displays the Carriage Return button
- actionGo – displays a Go button
- actionNext – displays a Next button
- actionDone – displays a Done button
If you don’t specify an action button, then the system guesses which one to show or shows the default action button. If there is following focusable field then it shows the Next button. If not, then it will show the Done button.
Use the imeOptions attribute in your layout to specify which action button to use:
Tip: include an inputType else the action button may not show!
If you need a text field for multiple lines of input then you’re probably going to use the textMultiLine input type. If you do, then be aware that you can’t set the action button. The default Carriage Return button will be shown.
Setting the inputType attribute to textMultiLine will always display the Carriage Return action button:
The textMultiLine input type shows the Carriage Return action button
Wait, there’s more!
You can specify extra keyboard behaviours by adding additional flags to the imeOptions attribute. Here are two examples:
Include the flagNoAccessoryAction flag in the imeOptions attribute and the additional action button on the right of the text input field wont’ show when in full screen mode
Include the flagNoExtractUi flag in the imeOptions attribute and the keyboard won’t show its text input field but will instead use the apps UI text field for the input
You can use the OnEditorActionListener interface to listen for when the user presses the action button.
The interface’s onEditorAction() method is triggered when the action button is pressed. You can then use its parameters:
- to get the text input for that text field
- to check which action button was pressed
See the documentation for more imeOptions attribute values.
Resize or Pan?
The keyboard takes up screen space and can end up hiding parts of your User Interface.
Although the system tries to guess how to adjust the available empty space to show your User Interface, it doesn’t always get it right.
It’s best that you specify how the system should adjust the window to accommodate both the keyboard and your User Interface. You use the Window adjustment mode values in the manifest to do this.
Here are the values that you can use:
- adjustUnspecified – the window resize and pan adjustments have not been specified so the system chooses depending on whether there are any scrolling views in the layout. If there are then the window resizes, making room for the keyboard
- adjustResize – resize the window to make room for the keyboard
- adjustPan – don’t resize the window but pan the contents as focus changes from one input field to another
- adjustNothing – don’t resize the window or pan the contents
Use the windowSoftInputMode attribute in the manifest to specify your adjustment setting. Here’s an example:
You can combine settings separated by the (|)
Showing the keyboard
On your marks, get set, Go!
You can automatically show the keyboard when the activity starts.
If the main purpose of your activity is to get user input, then you can specify that the keyboard shows as soon as the activity starts.
Set it up in your app’s AndroidManifest.xml file using the windowSoftInputMode attribute. Any settings you apply here will override the defaults.
Some of the options are:
- stateVisible – makes the keyboard appear
- stateAlwaysVisible – the keyboard is always visible when this window has input focus
- stateUnspecified – it’s not specified whether the keyboard should show or not. It’s left up to the system to decide. This is the default
Here’s an example of how you apply it in your manifest:
Show me them keys!
You can also decide when to show the keyboard.
Use the windowSoftInputMode attribute in the manifest to hide the keyboard.
Then use the InputMethodManager in the method where you want the keyboard to appear, like this:
Where’s it gone?
Don’t dismiss the keyboard programmatically. The system will hide it when the input task is finished.
The user can also choose to hide it by pressing the back button
See the documentation for more windowSoftInputMode attribute values.
You may also want to check out the soft keyboard tutorial, Android's soft keyboard. A tutorial
I hope that you have found this tutorial helpful.