Print this page

Android's soft keyboard. A tutorial.

  • Written by  Clive

Softly, softly. Using Android’s soft keyboard: A tutorial

Android soft keyboard tutorial icon

In the introduction, we covered the basics of using soft keyboards.  If you haven’t read it yet, you’ll find it here: Android’s soft keyboard. An introduction.

In this tutorial, we’ll show you how to use the soft keyboard in your apps.

If you haven’t already done so, then you may want to read, Capturing user input with Android’s TextFields

Our Tutorial: an overview

The main activity

Our tutorial app starts by displaying a number of text fields.

Android soft keyboard tutorial screenshot

Touching a text field causes the keyboard to show. The text fields pan up the screen as focus moves to the next field

Each text field is set to accept a different text type:

  • textPassword
  • textPersonName
  • textEmailAddress
  • phone
  • textMultiLine

Touching a text field displays the keyboard.

The user enters the input, presses the Next action button and focus moves to the next field.

Pressing the Down key (Back button) hides the keyboard.

Pressing the Save button displays the input in the LogCat and resets the text fields.

Pressing the Start Keyboard Activity button starts a new activity.

The second activity

The user can press the Show Keyboard button to show the keyboard. The system adjusts the space not occupied by the keyboard to display the text field and the Show Keyboard button.

Android soft keyboard tutorial screenshot show keyboard

Pressing the Show Keyboard button displays the keyboard. The space not occupied by the keyboard is adjusted to accommodate the User Interface

Our tutorial: in depth

The Main activity

The MyActivity activity starts when the app launches and displays a number of input fields.

The keyboard is initially hidden and only pops up when one of the text fields is touched. The user can then enter their input, pressing the Next action button moves the focus to the next text field which scrolls up.

Defining the EditText fields in the layout file

The input fields are defined in the activity’s layout file, activity_my_activity.xml. Here’s how we defined the password’s EditText field:

Android soft keyboard tutorial  textPassword

Note the following:

  • We have not included an imeOptions attribute for the action button. The system automatically detects that there is a following input field, so it displays a Next action button
  • We use the inputType attribute to set:
    • The type of input to textPassword
    • The visibility of the password: that it should be visible as it’s entered

And here’s our multi-line EditText view:

Android soft keyboard tutorial  textMultiLine

Note the following:

  • We set the maximum width of the field to be 4 ems. An em is a typographical unit of measure. 1 em is the width of the current font’s largest letter m
  • The input type is set to textMultiLine allowing a number of lines of text to be entered
  • The maximum number of characters that will be accepted is set by the maxLength attribute to 25
  • The maximum number of lines is set by the maxLines attribute to 5
  • There is no imeOptions attribute as the system will automatically display a Carriage Return action button for the textMultiLine input type

Setting the visibility and window adjustment in the manifest

There are two things we need to set in the manifest:

  • We want the keyboard to remain hidden until the user touches one of the text fields
  • the keyboard will take up part of the screen, hiding some of the input fields. So we’ll want the system to pan the text fields as the user moves focus from one to another

We’re able to do this with the windowSoftInputMode attribute in the AndroidManifest.xml file:

Android soft keyboard tutorial  stateHidden adjustPan

Listening for the action button presses

Our MyActivity activity implements the OnEditorActionListener interface. We use its onEditorAction() method to listen for when the user presses the action button.

Here’s part of the code we’ve included in this method:

Android soft keyboard tutorial  onEditorAction()

Note the following:

  • We return handled to indicate whether or not the method has consumed the action. Returning false will move focus to the next field when the Next action button is pressed. Returning true will not
  • We can use the TextView parameter, which is the text field whose action button was clicked, to get the input text. Here we use a switch statement to evaluate the view’s ID to get the view whose action button was clicked. We then use the getText() method to get the input text, converting it to a String with the toString() method
  • You can use the actionId parameter to identify which action button was clicked. Although it’s of no use here, we include it to show how it could be used. You can of course use the switch statement to filter the actionId and then use an if statement to filter the EditText view’s ID

The Save button

Our Save button simulates saving the input data.

We declare an onClick method, saveInput in the layout, and include the method’s code in the activity.

Clicking the button saves the “data”. It also calls the resetTextFields() method where we set all the EditText views to null.

Moving on to the next activity

We include the startKeyboardActivity() method in the activity and declare it as an onClick attribute in the layout file.

Clicking the Start Keyboard Activity button starts the KeyboardActivity activity.

The KeyboardActivity activity

This activity demonstrates how to show the keyboard on demand. We don’t show how to capture the input.

The activity displays a text field and a button. Pressing the button causes the keyboard to display. The keyboard will also show if the user touches the text field.

Notice that the button moves up when the keyboard displays as the screen resizes to accommodate our User Interface.

The manifest settings

There are two things we need to set in the manifest:

  • We want the keyboard to remain hidden until the user presses the button or touches the text field
  • The keyboard takes up part of the screen. So we’ll want the system to adjust the remaining space so that it can display our button

We’re able to do this with the windowSoftInputMode attribute in the AndroidManifest.xml file:

Android soft keyboard tutorial  stateHidden adjustResize

The layout

We declare our EditText view in the activity_keyboard.xml layout file, including the basic attributes:

Android soft keyboard tutorial  show keyboard EditText

The Android system guesses the type of input expected and displays a normal text keyboard with a Carriage Return action button.

If you include the inputType attribute:

Android soft keyboard tutorial  show keyboard EditText inputType

The system will now replace the Carriage Return action button with the Done action button by default as there are no following input text fields. If there were, then it would display a Next action button.

If you include the imeOptions attribute:

Android soft keyboard tutorial  show keyboard EditText imeOptions actionDone

And you don’t include the inputType attribute then the Carriage Return action button is displayed and not the Done action button.

Showing the keyboard

Clicking the Show Keyboard button calls the showKeyboard() method and displays the keyboard.

We declare the Show Keyboard button in the layout file and include the onClick attribute with the value, showKeyboard. The code for this method is included in the activity:

Android soft keyboard tutorial  show keyboard EditText imeOptions actionDone showKeyboard

Note the following:

  • editText - we get a reference to the EditText view that will receive the input
  • we use an if statement to check whether the text field has focus
  • imm – an instance of the InputMethodManager interface giving us access to the input method framework
  • showSoftInput – shows the keyboard. There are two parameters:
    • the text field that will receive the keyboard’s input
    • a flag indicating that this is an implicit request to show the keyboard. It’s not a direct request by the user and therefore may not be shown

Dismissing the keyboard

Pressing the Done action button hides the keyboard.

You can also hide the keyboard by pressing the Back button.

I hope that you have found this tutorial helpful.

This project was created using Android Studio.

You can download the project files here Download icon

Are you using Eclipse or another IDE? Here's how you can use this project's Android Studio files.