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?
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;
});
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.
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" />
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
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"
/>