android imf number

Tut#2 Android Input Method Framework : Handle Soft Keyboard in Android

What do we discuss in this article?

  1. How to use android:inputType and its values to customize the soft keyboard?
  2. How to handle events generated by the soft keyboard buttons?
  3. How to hide the soft keyboard for an EditText?

An input method editor (IME) also known as the soft keyboard is shown to the user when they tap an EditText that supports tapping

android imf

The android:inputType attribute takes a class plus modifiers, in a pipe-delimited list (where | is the pipe character) and describes what the user is allowed to input, and this determines the basic set of keys available on the soft keyboard. Here are some of the values which may change depending on the current version of Android.


  • text (the default)
  • number
  • phone
  • datetime
  • date
  • time

When no attributes are specified on your EditText, what you see is the usual keyboard popping up when you tap the EditText. Now let’s take a look at some variations.

Test 1: android:inputType=”textEmailAddress”

Let us create a layout with a sample EditText inside that has its inputType set to textEmailAddress

Let’s see the keyboard in action

android imf textEmailAddress


Notice the @ symbol in the keyboard that appears. Each time you specify a different value for inputType, the keyboard layout changes accordingly. Let’s explore some more options.

Test 2: android:inputType=”number | numberSigned | numberDecimal”

Notice how we used the ‘|’ pipe to separate multiple values specified together. Here is the code

Take a look at the keyboard now


 Test 3: inputType=”date|dateTime”

android imf number

android:inputType=”text|textMultiLine|textAutoCorrect” is another category that lets you insert multiline text. We”ll leave that to you for experimentation.

By choosing the appropriate android:inputType, you can give users a soft keyboard that best suits the type of data they should be entering.

EditText widgets are multiline by default if you do not specify android:inputType

You can control what goes on with that lower-right button ‘Done’ in the above image, called the accessory button.

 B] Handling the Buttons inside the soft keyboard

You can manually control what the accessory button will be labeled via the android:imeOptions attribute. Let’s take some code to see this in action

android imf action send

The last button on the right hand bottom now says “Send” and this is the same way you specify other buttons such as android:imeOptions=”actionDone” Now let us see how to handle these button clicks in our code

Use the setOnEditorActionListener method of the EditText to handle clicks generated by the soft keyboard accessory buttons such as send or done. Here is an example where we handle that.


 In the above example, we let the MainActivity implement the TextView.OnEditorActionListener which has a single method onEditorAction where we handle the click from the done button since we used android:imeOptions=”actionDone” in our layout

 C] How to hide the annoying keyboard sometimes?

Get the InputMethodManager which is responsible for managing interactions between the app and the keyboard

Every window has a unique token assigned to it to be identified by the WindowManager, The reason behind assigning window tokens is to prevent malicious applications from drawing on top of windows that belong to genuine applications. To close the keyboard for an EditText, you need to get the window token of the window where the EditText is placed and supply that to the method hideSoftInputFromWindow()