68

How to set EditText to show Search button or enter button on keyboard?

Alexander Farber
  • 21,519
  • 75
  • 241
  • 416
user790156
  • 2,718
  • 5
  • 20
  • 20

11 Answers11

221

In your layout set input method to option to Search

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

and in java code use

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;
    }
});
Nir Duan
  • 6,164
  • 4
  • 24
  • 38
Rahul Sharma
  • 3,637
  • 2
  • 20
  • 18
  • 38
    I had to set `android:inputType="text"` for my edittext to make this work – Bojan Radivojevic Aug 28 '12 at 12:06
  • 3
    yes you need to add `android:inputType="text"` for higher API levels. (For ICS+ I think) – Ajmal Salim Sep 22 '13 at 06:39
  • 1
    Keep in mind if a user is using an external keyboard they probably won't have a search button. If you don't have another way of performing the search you may also want to check for `KeyEvent.KEYCODE_ENTER` – JStephen Mar 11 '16 at 20:32
  • You should make KeyEvent optional in Kotlin, if not this code will cause a crash. Reference: https://stackoverflow.com/questions/47849219/java-lang-illegalargumentexception-parameter-specified-as-non-null-is-null-me – Shahen Kosyan Aug 22 '19 at 12:30
112

Use the code to edit EditText attribute

<EditText android:imeOptions="actionSearch" />

Then do this in your java code:

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;
    }
});
Janusz
  • 187,060
  • 113
  • 301
  • 369
success_anil
  • 3,659
  • 3
  • 27
  • 31
18
  android:singleLine="true"
  android:imeOptions="actionSearch"
Sujith S Manjavana
  • 1,417
  • 6
  • 33
  • 60
3

The following procedure describes how to set up an AutoCompleteTextView that provides suggestions from an array, using ArrayAdapter:

1 - Add the AutoCompleteTextView to your layout. Here's a layout with only the text field:

<?xml version="1.0" encoding="utf-8"?>
<AutoCompleteTextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/autocomplete_country"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

2 - Define the array that contains all text suggestions. For example, here's an array of country names that's defined in an XML resource file (res/values/strings.xml):

    <?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="countries_array">
        <item>Afghanistan</item>
        <item>Albania</item>
        <item>Algeria</item>
        <item>American Samoa</item>
        <item>Andorra</item>
        <item>Angola</item>
        <item>Anguilla</item>
        <item>Antarctica</item>
        ...
    </string-array>
</resources>

3 - In your Activity or Fragment, use the following code to specify the adapter that supplies the suggestions:

// Get a reference to the AutoCompleteTextView in the layout
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete_country);
// Get the string array
String[] countries = getResources().getStringArray(R.array.countries_array);
// Create the adapter and set it to the AutoCompleteTextView 
ArrayAdapter<String> adapter = 
        new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, countries);
textView.setAdapter(adapter);

Here, a new ArrayAdapter is initialized to bind each item in the COUNTRIES string array to a TextView that exists in the simple_list_item_1 layout (this is a layout provided by Android that provides a standard appearance for text in a list). Then assign the adapter to the AutoCompleteTextView by calling setAdapter().

Thep TK
  • 31
  • 1
3

set these two fields for showing search icon on keyboard.

            android:imeOptions="actionSearch"
            android:imeActionLabel="@string/search"

and also if you need to perform some action on keyboard search button click then you have to add the following code.

    etSearch.setOnEditorActionListener((v, actionId, event) -> {
        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
              search();     // you can do anything
            return true;
        }
        return false;
    });
Savita Sharma
  • 344
  • 3
  • 9
1

KOTLIN DEVELOPERS:

1. XML:

        <androidx.appcompat.widget.AppCompatEditText
            android:id="@+id/etSearch"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="@font/sans_medium"
            android:imeOptions="actionSearch"
            android:inputType="text" />

2. Class File:

etSearch.setOnEditorActionListener(object : TextView.OnEditorActionListener {
        override fun onEditorAction(v: TextView?, actionId: Int, event: KeyEvent?): Boolean {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                //hide Keyboard
                //do search here
                return true
            }
            return false
        }
    })

NOTES: With imeOptions="actionSearch" , don't forget to add inputType="text" or whatever you want to set as inputType in your XML. Otherwise you won't be able to see search icon on keyboard open.

Kishan Solanki
  • 13,761
  • 4
  • 85
  • 82
0

Make your edittext like this.

    <EditText
        android:id="@+id/s_search"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#000"
        android:textSize="15dp"
        android:imeOptions="actionSearch"
        android:inputType="text"
        android:hint="Enter search" />
mehmoodnisar125
  • 1,469
  • 18
  • 14
0

use this

<EditText
                android:id="@+id/editSearch"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:imeOptions="actionSearch"
                android:singleLine="true"

must add singleLine=true in your xml i hope my answer help you

yousef
  • 1,345
  • 1
  • 12
  • 20
0

set this attribute "imeOptions" in EditText view:

<EditText
     android:imeOptions="actionSearch"
     android:layout_width="match_parent"
     android:inputType="text"
     android:hint="Enter search"
     android:layout_height="wrap_content"
/>
Marium Jawed
  • 391
  • 4
  • 9
0

You can change the keyboard available by using the inputType for the EditText.

<EditText android:inputType="number"/>

XML documentation

...or...

editText.setInputType(int);

Code documentation

nicholas.hauschild
  • 42,483
  • 9
  • 127
  • 120
0

In Kotlin File

ed_search.setOnEditorActionListener(TextView.OnEditorActionListener { v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
               
            }
            false
        })

In XML File

android:imeOptions="actionSearch"
           
Safal Bhatia
  • 245
  • 2
  • 6