user790156
user790156

Reputation: 2728

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

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

Upvotes: 68

Views: 106846

Answers (11)

Safal Bhatia
Safal Bhatia

Reputation: 405

In Kotlin File

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

In XML File

android:imeOptions="actionSearch"
           

Upvotes: 0

Kishan Solanki
Kishan Solanki

Reputation: 14618

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.

Upvotes: 1

Marium Jawed
Marium Jawed

Reputation: 419

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"
/>

Upvotes: 0

yousef
yousef

Reputation: 1363

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

Upvotes: 0

Savita Sharma
Savita Sharma

Reputation: 344

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

Upvotes: 3

mehmoodnisar125
mehmoodnisar125

Reputation: 1529

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" />

Upvotes: 0

success_anil
success_anil

Reputation: 3658

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

Upvotes: 112

Rahul Sharma
Rahul Sharma

Reputation: 3677

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

Upvotes: 221

Sujith S Manjavana
Sujith S Manjavana

Reputation: 1576

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

Upvotes: 18

Thep TK
Thep TK

Reputation: 31

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

Upvotes: 3

nicholas.hauschild
nicholas.hauschild

Reputation: 42849

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

Upvotes: 0

Related Questions