Programming and Personal Development

Adding Soft Keyboard to your android application

The input method framework (IMF) in android provides a seamless mechanism to handle textual input from the user. Simple example layout for Input Method Framework For devices that do not have a physical keyboard, IMF automatically provides a soft/virtual keyboard whenever the user needs to input data ( say when the cursor is placed on a EditText element).  In addition android lets you customize the  keyboard  based on the type of data that is expected. For example, if the user is entering a name or birthdate , IMF can provide a virtual keyboard that has just the textual keys for the name or just the  number keys for the birthdate and so on. Lets use a simple example to learn the usage.

In this example, we have used the relative layout to construct a form with 5 EditText elements each demonstrating different kinds of soft keyboards possible with the IMF framework.  You use the android:inputType attribute as part of the element definition  to specify the type of soft keyboard desired.

  1. We can combine multiple options for the inputType attribute by separating them with the pipe (“|”) character as shown. In this case we specify that the EditText element can take in multiple lines of text as input . The right most key displays a “return” key for moving the user to the next line. android inputtype textmultiline demo
  2. Here we specify the inputType as “textEmailAddress” which provides a soft keyboard with all the necessary characters for entering a valid email address. In addition we can also specify the android:imeOptions to change the lower-right most button in the keyboard to display other operations like the “Send” action as explained inputtype email address demoandroid inputtype number demo
  3. We specify the inputType as “number” to provide a keyboard with just numbers.
  4. Here inputType is “datetime” for entering the date and time. The keyboards for datetimeandroid inputtype textpassword demo and numbers look identical as shown here.
  5. Finally,  we specify the input type as “textPassword” for entering passwords which automatically hides the characters entered for privacy. Also notice that the right most button now says “Done”.


Android lets us manipulate the lower right most button using the  android:imeOptions attribute. By default, the lower most button displays

  • the “return” action for multi-line text,
  • “Next” action for other types of input. Clicking on this action takes the user to the next field in the list and adjusts the soft keyboard accordingly.
  • “Done”  action if its the last field available for user input. Clicking on this action hides the keyboard.

In addition to the default actions,  you can specify other actions like “actionSend” or   “actionSearch” to provide custom functionality.  You need to set the TextView.OnEditorActionViewListener() on the EditText element using the setOnEditorActionListener() method. This Listener is passed in the type of action as an argument which can then be used to implement the custom action.


Web Analytics