2

Is it possible to limit SearchView input text length? I have added android:maxLength="10" in Menu Item declaration but has no effect.

 <item
    android:id="@+id/app_bar_search"
    android:icon="@drawable/ic_search_white"
    android:title="Search"
    app:actionViewClass="android.support.v7.widget.SearchView"
    app:showAsAction="collapseActionView|ifRoom"
    android:maxLength="10"/>
Ask Too Much
  • 627
  • 4
  • 19
  • 1
    did you [try this link?](https://stackoverflow.com/questions/10914539/can-we-limit-the-number-of-character-in-edittext-of-searchview-in-android/16033389) – Jyoti JK Apr 28 '18 at 07:51
  • This a possible duplicate of the question here: https://stackoverflow.com/a/25457111/2984447 - that has a working solution too:) from [Alexander Zhak](https://stackoverflow.com/users/2653775/alexander-zhak). Answer marked correct in the this post throws a null pointer exception. I wished to only add comment here, but do not have the privileges yet! Apologies. – user2984447 Jul 02 '19 at 17:41

2 Answers2

5

I can achieved this by using SO one answer in the link suggested by Jyoti JK:

TextView et = (TextView) searchView.findViewById(R.id.search_src_text);
et.setFilters(new InputFilter[]{new InputFilter.LengthFilter(10)});
Ask Too Much
  • 627
  • 4
  • 19
2

Try this will help you:

EditText et = (EditText)searchView.findViewById(searchView.getContext().getResources()
        .getIdentifier("android:id/search_src_text", null, null));
et.setFilters(new InputFilter[] { new InputFilter.LengthFilter({10}) });
Hemant N. Karmur
  • 840
  • 1
  • 7
  • 21
  • for me :searchView.findViewById(R.id.search_src_text) and variants didn't work, only your solution work for find edittext after a lot of googling – Vins Mar 09 '23 at 09:21