12

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

vikash singh
  • 141
  • 1
  • 6
  • 2
    `Edittext`'s XML attribute `android:maxLength=15` will allow only 15 character. Is this only you need? – Praveenkumar Jun 06 '12 at 12:49
  • This may not work... `SearchView` doesn't extend `EditText`. I know that `inputType` doesn't work on `SearchView` for some reason either. – Alex Lockwood Jun 06 '12 at 12:51
  • @AlexLockwood May i know what's the `SearchView` Is that like `AutocomplteTextView` – Praveenkumar Jun 06 '12 at 12:54
  • 1
    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. – Alex Lockwood Jun 06 '12 at 12:56
  • 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. – cstrutton Jun 06 '12 at 13:39
  • @Alex Lockwood Do u know how to do this stuff. – vikash singh Jun 07 '12 at 05:09
  • I asked a similar question [**here**](http://stackoverflow.com/questions/9543620/how-to-set-a-searchviews-input-type-to-numeric) and never got an answer. This question sounds similar... so I'm not so sure if there's an easy way to do it. :/ – Alex Lockwood Jun 07 '12 at 05:11

4 Answers4

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)});
Alexei Volkov
  • 851
  • 9
  • 11
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;
            }
        });
himb2001
  • 1,371
  • 1
  • 10
  • 7
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}) });
Alexander Zhak
  • 9,140
  • 4
  • 46
  • 72
-5

In XML layout try this..

 android:maxLength="20" //20 is number of characters.
halfer
  • 19,824
  • 17
  • 99
  • 186
deepa
  • 2,496
  • 1
  • 26
  • 43