Can we limit the number of character in edittext of SearchView in Android?
Asked Answered
A

5

12

Is there any api to set the limit no. of characters in SearchView ?

Agrapha answered 6/6, 2012 at 12:48 Comment(7)
Edittext's XML attribute android:maxLength=15 will allow only 15 character. Is this only you need?Scarab
This may not work... SearchView doesn't extend EditText. I know that inputType doesn't work on SearchView for some reason either.Agateware
@AlexLockwood May i know what's the SearchView Is that like AutocomplteTextViewScarab
No, AutoCompleteTextView extends EditText; SearchView extends LinearLayout. The backend for SearchView is much more complex. Although it looks almost exactly like an EditText, it is much more complicated and is meant to be used with a search interface.Agateware
This might be a bit 'hackey' (is that a word?) but could you not put an edit text overtop and use textChangedWatcher to update the searchView? I know it's wouldn't be ideal but would it work? Sometimes the only way is with a bigger hammer.Quicksilver
@Alex Lockwood Do u know how to do this stuff.Agrapha
I asked a similar question here and never got an answer. This question sounds similar... so I'm not so sure if there's an easy way to do it. :/Agateware
B
13

I am using following code snippet:

TextView et = (TextView) searchView.findViewById(R.id.search_src_text);
        et.setFilters(new InputFilter[]{new InputFilter.LengthFilter(10)});
Boar answered 11/4, 2016 at 11:59 Comment(0)
T
7

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;
            }
        });
Threw answered 16/4, 2013 at 9:28 Comment(0)
H
7
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}) });
Herrle answered 22/8, 2014 at 23:54 Comment(0)
M
0

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))
Malcolmmalcom answered 23/2 at 6:55 Comment(0)
N
-5

In XML layout try this..

 android:maxLength="20" //20 is number of characters.
Novobiocin answered 6/6, 2012 at 13:1 Comment(3)
Have you seen this CommentScarab
In SearchView there is nothing to set character length.Agrapha
Search view does not have "maxLength"Kafiristan

© 2022 - 2024 — McMap. All rights reserved.