Is there any api to set the limit no. of characters in SearchView ?
Can we limit the number of character in edittext of SearchView in Android?
Asked Answered
I am using following code snippet:
TextView et = (TextView) searchView.findViewById(R.id.search_src_text);
et.setFilters(new InputFilter[]{new InputFilter.LengthFilter(10)});
Use this code it will do search only if length is less or equal to 5.You can make it change accordingly like returning true from onQueryTextChange() when text>5.
final SearchView searchView = (SearchView) mSearchMenuItem.getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(mActivity.getSearchComponentName()));
searchView.setIconifiedByDefault(true);
searchView.setSubmitButtonEnabled(true);
searchView.setOnQueryTextListener(new OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String arg0) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onQueryTextChange(String arg0) {
if(arg0.length()>5){
System.out.println("Text character is more than 5");
searchView.setQuery(arg0.substring(0,5), false);
}
return false;
}
});
EditText et = (EditText)searchView.findViewById(searchView.getContext().getResources()
.getIdentifier("android:id/search_src_text", null, null));
et.setFilters(new InputFilter[] { new InputFilter.LengthFilter({max_text_length}) });
I am using the following code snippet in case R.id.search_src_text unresolved:
val searchEdt = searchView.findViewById<EditText>(androidx.appcompat.R.id.search_src_text)
searchEdt.filters = arrayOf(InputFilter.LengthFilter(10))
In XML layout try this..
android:maxLength="20" //20 is number of characters.
Have you seen this Comment –
Scarab
In SearchView there is nothing to set character length. –
Agrapha
Search view does not have "maxLength" –
Kafiristan
© 2022 - 2024 — McMap. All rights reserved.
Edittext
's XML attributeandroid:maxLength=15
will allow only 15 character. Is this only you need? – ScarabSearchView
doesn't extendEditText
. I know thatinputType
doesn't work onSearchView
for some reason either. – AgatewareSearchView
Is that likeAutocomplteTextView
– ScarabAutoCompleteTextView
extendsEditText
;SearchView
extendsLinearLayout
. The backend forSearchView
is much more complex. Although it looks almost exactly like anEditText
, it is much more complicated and is meant to be used with a search interface. – Agateware