How to set edittext to show search button or enter button on keyboard?
AndroidKeyboardAndroid Problem Overview
How to set EditText
to show Search button or enter button on keyboard?
Android Solutions
Solution 1 - Android
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 - Android
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
android:singleLine="true"
android:imeOptions="actionSearch"
Solution 4 - Android
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 - Android
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;
});
Solution 6 - Android
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.
Solution 7 - Android
You can change the keyboard available by using the inputType for the EditText
.
<EditText android:inputType="number"/>
...or...
editText.setInputType(int);
Solution 8 - Android
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" />
Solution 9 - Android
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
Solution 10 - Android
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"
/>