I Stimuli, your entertainment hub for movies, music and books.

Search The Movie Database and The Open Movie Database for movies and TV shows.

Watch movies on Amazon and Youtube.

Search Spotify for music. Play music on Spotify.

Buy books, music and movies on Amazon.

Android's soft keyboard. An introduction.

  • Written by  Clive

Softly, softly. Using Android’s soft keyboard: An introduction

Android soft keyboard introduction icon

Need to capture text input in your app?

Android supports an on-screen soft keyboard as well as attached hardware keyboards.

The soft keyboard appears when a text field is in focus and disappears when it’s out of focus. The keyboard positions itself at the bottom of the screen over the app window. It has its own editing area.

In this introduction, we’ll cover the basics of using soft keyboards. Later, in a follow up 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

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:

Android soft keyboard introduction emailType inputType

Behave yourself!

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:

Android soft keyboard introduction inputType textCapChars

You can also combine different input types and behaviours like this:

Android soft keyboard introduction inputType combineBehaviors

See the documentation for more inputType attribute values.

Action Stations!

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:

Android soft keyboard introduction imeOptions

Tip: include an inputType else the action button may not show!

Watch out!

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:

Android soft keyboard introduction inputType textMultiLine

Android soft keyboard introduction inputType textMultiLine carriageReturn

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:

Android soft keyboard introduction inputType flag screenshot

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

Android soft keyboard introduction inputType flag screenshot

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

Taking action

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:

Android soft keyboard introduction windowSoftInputMode adjustResize

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:

Android soft keyboard introduction windowSoftInputMode adjustResize stateVisible

Show me them keys!

You can also decide when to show the keyboard.

Use the windowSoftInputMode attribute in the manifest to hide the keyboard.

Android soft keyboard introduction windowSoftInputMode adjustResize stateHidden

Then use the InputMethodManager in the method where you want the keyboard to appear, like this:

Android soft keyboard introduction inputMethodManager

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.