How to set SearchView input type to numeric

I currently have a SearchView in my ActionBar application (from which I populate from an XML layout file), and I'm trying to get it to accept only numeric data. I tried to set the android:inputType="number" attribute in XML, but it has no effect.

Does anyone know the source of this problem?

XML menu resource:

 <menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/search" android:title="@string/menu_search" android:actionViewClass="android.widget.SearchView" android:icon="@drawable/ic_menu_search_holo_light" android:inputType="number" /> </menu> 
+9
android android-actionbar android-view
Mar 03 '12 at 5:01
source share
4 answers

If you install it in a compatibility library fragment, you need to use SearchViewCompat.setInputType(View searchView, int inputType) :

 @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { inflater.inflate(R.menu.searchable, menu); MenuItem searchMenuItem = menu.findItem(R.id.action_search); if (searchMenuItem != null) { SearchView searchView = (SearchView) searchMenuItem.getActionView(); if (searchView != null) { SearchViewCompat.setInputType(searchView, InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_POSTAL_ADDRESS); } } } 
+6
Jun 28 '14 at 1:22
source share

setInputType in searchView was introduced in API 14. To set it to try number:

 searchView.setInputType(InputType.TYPE_CLASS_NUMBER); 
+9
Dec 17 '12 at 19:47
source share

This is not quite the answer, but keep in mind that the usefulness of input type filters may depend on the IME you use; some keyboards are not easy to obey these types of input ... I found out that it is difficult .: (

With that in mind, have you tried using other input types to make sure they stick to it? If they stick, this is probably an IME issue. If this is not the case, there is probably a problem with the way you are trying to provide input like input.

Now, for the shot in response:

You can try onCreateOptionsMenu to search by the identifier of this menu item, cast in SearchView and set the input type in the code:

 @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater menuInflater = getMenuInflater(); menuInflater.inflate(R.menu.default_menu, menu); if (MyApplication.SUPPORTS_HONEYCOMB) { // quick check for API level // If we have the honeycomb API, set up the search view MenuItem searchItem = menu.findItem(R.id.search); SearchView search = (SearchView) searchItem.getActionView(); // your code here. something like: search.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_NORMAL); // you also likely want to set up your search listener here. } // I'm using ActionBarCompat, in which case // calling super after populating the menu is necessary here to ensure that the // action bar helpers have a chance to handle this event. return super.onCreateOptionsMenu(menu); } 
+2
Mar 03 '12 at 15:55
source share

When using SearchView from android.support.v7.widget you can use:

 searchView.setInputType(InputType.TYPE_CLASS_NUMBER); 
+1
Mar 07 '16 at 8:51
source share



All Articles