8

This is my code:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    MenuItem searchItem = menu.findItem(R.id.searchMenuItem);
    SearchManager searchManager =
        (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
    // searchView.set
    searchView.setSearchableInfo(
        searchManager.getSearchableInfo(getComponentName()));
    searchView.setIconifiedByDefault(false);
}

My minimum API is 8.
I have this compile error

Call requires API level 11 (current min is 8):
    android.widget.SearchView#setSearchableInfo

Note, that I can't change the minimum SDK.

JJD
  • 50,076
  • 60
  • 203
  • 339
user2059935
  • 971
  • 4
  • 12
  • 24
  • This error can actually be ignored. There's a setting in Eclipse to turn it off. Or if you're in any other file in Eclipse when you hit the debug button it won't stop the build. – Gabe Sechan Feb 17 '14 at 07:05
  • use a editText instead of searchview. what is the min sdk in manifest? – Raghunandan Feb 17 '14 at 07:05
  • what do you mean by cant change minimum sdk? – Vishal Santharam Feb 17 '14 at 07:06
  • @VishalSantharam i mean that I want to support devices 2.2 and later – user2059935 Feb 17 '14 at 07:08
  • @GabeSechan so if i ignored that error, my app still work on android 2.2 and later? – user2059935 Feb 17 '14 at 07:08
  • @Raghunandan can u give me an example please? – user2059935 Feb 17 '14 at 07:09
  • Then you must use android support library.http://developer.android.com/tools/support-library/index.html – Vishal Santharam Feb 17 '14 at 07:11
  • @user2059935 you can use search view from the support library with `android.support.v7.widget.SearchView` using `AppCompat` Look @ http://developer.android.com/guide/topics/ui/actionbar.html – Raghunandan Feb 17 '14 at 07:11
  • Yes. It would only throw an exception if it actually tried to execute that function, which shouldn't happen due to the if statement. I've used this pattern in the past and it worked fine. Although as someone else suggested using the support library may be an even better solution to give your 2.2 users more functionality. IMO though 2.2 really isn't worth the time to support- less than 2% of all apps downloaded on Play last month were downloaded with a 2.2 device. – Gabe Sechan Feb 17 '14 at 07:11

1 Answers1

9

You can use android.support.v7.widget.SearchView

instead of android.widget.SearchView

Your code could look like this:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                MenuItem searchItem = menu.findItem(R.id.searchMenuItem);
                SearchManager searchManager =
                        (SearchManager) getSystemService(Context.SEARCH_SERVICE);
                android.support.v7.widget.SearchView searchView = (android.support.v7.widget.SearchView) MenuItemCompat.getActionView(searchItem);

                searchView.setSearchableInfo(
                        searchManager.getSearchableInfo(getComponentName()));
                searchView.setIconifiedByDefault(false);

            }

Also please read more about SearchViewCompat

William Kinaan
  • 28,059
  • 20
  • 85
  • 118