Android SearchManager: How to set minimum number of characters to enable the Search

StackOverflow https://stackoverflow.com/questions/10833157

  •  12-06-2021
  •  | 
  •  

Вопрос

I am using the Android SearchManager. I defined all the needed attributes in the searchable.xml.

Now, I would like to set a limit for minimum numer of characters before the user can make the search. By default, the min is 1.

How can this be changed?

Thank you.

Это было полезно?

Решение

Its the following attribute in searchable.xml

<searchable xmlns:android="http://schemas.android.com/apk/res/android"
  ...
  android:searchSuggestThreshold="2"
  >
</searchable>

Другие советы

if onQueryTextSubmit return false then only search action is performing , we can limit chars like this

    @Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.menu_home, menu);

        // Get the SearchView and set the searchable configuration
        SearchManager searchManager = (SearchManager)getActivity().getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
        // Assumes current activity is the searchable activity
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
        //searchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default

        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return query.length()<4; //number of char to limit
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });

    }
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top