How to set edittext to show search button or enter button on keyboard?

Solution 1:

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;
    }
});

Solution 2:

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;
    }
});

Solution 3:

  android:singleLine="true"
  android:imeOptions="actionSearch"

Solution 4:

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().

Solution 5:

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;
    });