Over a million developers have joined DZone.

Android Keyboard Hacks - Hide the Keyboard and Customize Actions

DZone 's Guide to

Android Keyboard Hacks - Hide the Keyboard and Customize Actions

Use these code hacks to control the appearance and behavior of Android's keyboard.

· Mobile Zone ·
Free Resource

This post explains the various code hacks used for controlling the appearance and behavior of the Android soft keyboard.

Most of the form-based applications often need to customize the default behavior of Android soft keyboard. Often it is required to control the type of keyboard you want to present user for data input, or customize the appearance of keyboard Enter key action.

Let us first began with controlling the type keyboard to display while editing the form data.

1. Set TextView Input type

There are different types of keyboard designed for user convenience. For example, if for entering a number you must display the numerical keyboard. This can be controlled by InputType property of TextView. The InputType controls aspects such as the type of data being placed in a text field. Generally you can select a single value, though some can be combined together as indicated. Setting this attribute to anything besides none also implies that the text is editable.

Some of the most used input type constants includes, nonetext,textCapCharacterstextCapWordstextCapSentencestextAutoCorrect,textAutoCompletetextMultiLinetextImeMultiLinetextUri,textEmailAddresstextPasswordtextWebEditTexttextPhonetic,textWebEmailAddressnumberphonedatetimedate, and time.

You can set the inputType property in the layout declaration as follows

   android:inputType="number" />

2. Customize the Input Method

Now that we understand the different keyboard types based on the TextViewinputType property let us customize the appearance of keyboard Enter action.

When the keyboard is visible on screen, the text on the Enter key typically indicates its action based on the order of focusable items in the view. While unspecified, the keyboard will display a “Next” action if there are more focusable in the view to move to, or a “Done” action if the last item is currently focused on. In the case of a multiline field, this action is a line return.

This value can be customized using android:imeOptions value in the TextView XML declaration. The android:imeOptions attribute access the following possible values:

  • actionUnspecified: This property displays action of the device’s choice  Action event is IME_NULL. This is the default keyboard action.
  • actionGo: This displays Go as the Enter key.  Action event is IME_ACTION_GO
  • actionSearch: Displays a search icon as the Enter key  Action event is IME_ACTION_SEARCH
  • actionSend: Displays Send as the Enter key. Action event is IME_ACTION_SEND
  • actionNext: Displays Next as the Enter key. Action event is IME_ACTION_NEXT
  • actionDone: Displays Done as the Enter key. Action event is IME_ACTION_DONE

All the above set of action event constants are defined in EditorInfo class.

Let’s look at an example layout with two editable text fields. The first EditText will display the search icon for the Enter key, and the second will display Go. The resulting output may very depending on current keyboard installed on device.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

        android:singleLine="true" />

        android:singleLine="true" />

Note that, the custom editor options apply only to the appearance of soft input methods. Changing this value will not affect the events that get generated when the user presses on a physical hardware keyboard button.

3. Adding Custom Action

Customizing what happens when the user presses the Enter key can be just as important as adjusting its appearance. For overriding the default behavior we need to attach an OnEditorActionListener to EditText instance.

The following code snippet shows how to create a custom action for EditTexts.

public class MainActivity extends Activity implements OnEditorActionListener {
    public void onCreate(Bundle savedInstanceState) {


        //Add the listener to the views
        EditText editText1 = (EditText) findViewById(R.id.text1);

        EditText editText2 = (EditText) findViewById(R.id.text2);

    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
            //Handle search key click
            return true;
        if (actionId == EditorInfo.IME_ACTION_GO) {
            //Handle go key click
            return true;
        return false;

The boolean return value of onEditorAction()  tells the system whether your implementation has consumed the event or whether it should be passed on to the next possible responder, if any.

It is important for you to return true when you handles the event yourself, so no other processing occurs. You can return false when you are not handling the event so your application does not steal key events from the rest of the system.

4. Hide the Soft Keyboard

The following code snippet will help you to hide or dismiss the soft keyboard from the screen.

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);

The hideSoftInputFromWindow() takes an IBinder  window token as a parameter. This can be retrieved from any View object currently attached to the window via View.getWindowToken().

mobile ,android

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}