Search code examples
android-actionbarandroidsearchview

How to use SearchView widget "without" support v.7 library


So according to developer.android documents, this is how one can include a SearchView in ActionBar:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
    <item android:id="@+id/action_search"
          android:title="@string/action_search"
          android:icon="@drawable/ic_action_search"
          yourapp:showAsAction="ifRoom|collapseActionView"
          yourapp:actionViewClass="android.support.v7.widget.SearchView" />
</menu>

The official guide, have only covered the SearchView from the support library, and not the one from Android Framework.

However, I am certain that my users have Android 4+ devices, so I want to use SearchView without appending support library to my app.

What changes should I make to my ActionBar XML menu layout?

I could not find any other tutorial on SearchView without Support Library.


Solution

  • From the documentation of SearchView:

    Note: This class is included in the support library for compatibility with API level 7 and higher. If you're developing your app for API level 11 and higher only, you should instead use the framework SearchView class.

    It basically means that you use the SearchView class from the android.widget package and set the minSdkVersion to 11 in your manifest or build.gradle.

    Update based on the comment I just removed the *yourapp** namespace. This should probably work:

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:id="@+id/action_search"
          android:title="@string/action_search"
          android:icon="@drawable/ic_action_search"
          android:showAsAction="ifRoom|collapseActionView"
          android:actionViewClass="android.widget.SearchView" />
    </menu>
    

    You can visit this SO post for more information.