By Ricket


2010-07-08 15:36:41 8 Comments

I can't figure this out. Some apps have a EditText (textbox) which, when you touch it and it brings up the on-screen keyboard, the keyboard has a "Search" button instead of an enter key.

I want to implement this. How can I implement that Search button and detect press of the Search button?

Edit: found how to implement the Search button; in XML, android:imeOptions="actionSearch" or in Java, EditTextSample.setImeOptions(EditorInfo.IME_ACTION_SEARCH);. But how do I handle the user pressing that Search button? Does it have something to do with android:imeActionId?

6 comments

@kaMChy 2017-03-16 08:50:23

Hide keyboard when user clicks search. Addition to Robby Pond answer

private void performSearch() {
    editText.clearFocus();
    InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    in.hideSoftInputFromWindow(searchEditText.getWindowToken(), 0);
    //...perform search
}

@iAmSauravSharan 2019-05-28 06:06:05

This answer is for TextInputEditText :

In the layout XML file set your input method options to your required type. for example done.

<com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:imeOptions="actionGo"/>

Similarly, you can also set imeOptions to actionSubmit, actionSearch, etc

In the java add the editor action listener.

textInputLayout.getEditText().setOnEditorActionListener(new 

    TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_GO) {
                performYourAction();
                return true;
            }
            return false;
        }
    });

If you're using kotlin :

textInputLayout.editText.setOnEditorActionListener { _, actionId, _ ->
    if (actionId == EditorInfo.IME_ACTION_GO) {
        performYourAction()
    }
    true
}

@Null Pointer Exception 2019-03-07 04:25:10

by XML:

 <EditText
        android:id="@+id/search_edit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/search"
        android:imeOptions="actionSearch"
        android:inputType="text" />

By Java:

 editText.clearFocus();
    InputMethodManager in = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    in.hideSoftInputFromWindow(searchEditText.getWindowToken(), 0);

@Shaon 2018-08-28 06:02:03

In Kotlin

evLoginPassword.setOnEditorActionListener { _, actionId, _ ->
    if (actionId == EditorInfo.IME_ACTION_DONE) {
        doTheLoginWork()
    }
    true
}

Partial Xml Code

 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
       <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            android:layout_marginBottom="8dp"
            android:layout_marginTop="8dp"
            android:paddingLeft="24dp"
            android:paddingRight="24dp">

            <EditText
                android:id="@+id/evLoginUserEmail"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/email"
                android:inputType="textEmailAddress"
                android:textColor="@color/black_54_percent" />
        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:layout_marginTop="8dp"
            android:paddingLeft="24dp"
            android:paddingRight="24dp">

            <EditText
                android:id="@+id/evLoginPassword"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/password"
                android:inputType="textPassword"
                android:imeOptions="actionDone"
                android:textColor="@color/black_54_percent" />
        </android.support.design.widget.TextInputLayout>
</LinearLayout>

@Braj Bhushan Singh 2016-11-29 13:15:40

In xml file, put imeOptions="actionSearch" and inputType="text", maxLines="1":

<EditText
    android:id="@+id/search_box"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/search"
    android:imeOptions="actionSearch"
    android:inputType="text"
    android:maxLines="1" />

@Robby Pond 2010-07-08 15:44:26

In the layout set your input method options to search.

<EditText
    android:imeOptions="actionSearch" 
    android:inputType="text" />

In the java add the editor action listener.

editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
            performSearch();
            return true;
        }
        return false;
    }
});

@ozmank 2011-11-15 13:36:13

what if we want to get the keys on which the user clicks, like a,b,c?

@thanhbinh84 2011-12-30 15:03:21

On os 2.3.6 it doesn't work until I put android:inputType="text" attribute.

@Carol 2012-03-02 21:01:21

Shouldn't both of those TextView's say EditText instead?

@AMH 2012-06-09 15:40:11

what if I want to change the name to login, search, done

@ccyrille 2012-06-14 17:22:07

android:inputType="text" was also required for me on Android 2.3.5 and 4.0.4

@IronBlossom 2013-05-08 06:58:11

I found if the keyboard is in Swype mode, search button doesn't appear, at least for Galaxy SII (2.3.4)

@redDragonzz 2013-09-11 03:37:30

I think you need to add a listener as well to the text edit otherwise the keyboard does not appear properly just by the actionSearch IME option in the layout file.

@howettl 2013-09-19 17:21:38

@Carol EditText is a sub class of TextView.

@Patrick Boos 2014-01-15 06:51:35

Probably better is even android:inputType="textFilter". (Which seems to work for me on 2.3.7)

@user818455 2014-01-21 13:06:50

android:inputType="text" is also required for 4.4.0 - 4.4.2 (Android Kitkat).

@lionelmessi 2015-12-30 19:32:33

Yup, android:inputType="text" is still needed in 5.0 :)

@Jyotman Singh 2016-08-02 10:10:20

I also had to add android:singleLine="true" for the search icon to appear.

@MortenSickel 2016-08-29 11:43:18

Have not tried with inputtype text, but it works fine with inputtype numberdecimal

@Ognev Zair 2017-05-06 08:55:24

android:inputType="text" was also required for me on 7.0 (Android Nougat)

Related Questions

Sponsored Content

39 Answered Questions

[SOLVED] How to check visibility of software keyboard in Android?

16 Answered Questions

[SOLVED] First letter capitalization for EditText

24 Answered Questions

[SOLVED] Android - Handle "Enter" in an EditText

40 Answered Questions

[SOLVED] How to hide soft keyboard on android after clicking outside EditText?

16 Answered Questions

[SOLVED] How to hide Android soft keyboard on EditText

18 Answered Questions

[SOLVED] Fling gesture detection on grid layout

2 Answered Questions

[SOLVED] Handling Enter and search key on the keyboard

1 Answered Questions

Enter button on android keyboard

2 Answered Questions

[SOLVED] Android keyboard enter button to keep keyboard visible

  • 2012-08-30 16:27:24
  • Solenoid
  • 1104 View
  • 0 Score
  • 2 Answer
  • Tags:   android

Sponsored Content