Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SearchView in ActionBar using support-v7-appcompat

I've been trying very hard to get the SearchView widget to expand in the actionbar using the support-v7 libraries. I've managed to get it working without the support libraries when I target 4.0+ but I want to write the app for 2.3+ so I need to use the support libraries. I created a blank new activity with the following menu.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >

<item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    android:showAsAction="never"
    android:title="@string/action_settings"/>
    
<item
    android:id="@+id/action_search"
    android:icon="@android:drawable/ic_menu_search"
    yourapp:showAsAction="always"
    yourapp:actionViewClass="android.support.v7.widget.SearchView"
    android:title="Search"/>
</menu>

This does not even show the search button, let alone expand it upon clicking. It simply add's the search into the menu instead of showing it in the actionbar. using appcompat library (does not work)

Alternatively I tried the same without the appcompat library , I simply replaced the menu.xml with:

<item
    android:id="@+id/action_search"
    android:icon="@android:drawable/ic_menu_search"
    android:showAsAction="always"
    android:actionViewClass="android.widget.SearchView"
    android:title="Search"/>

And it works perfectly fine, and even expands to the search text input widget upon clicking. enter image description here

I want the searchview as available in the second picture while using the appcompat library, but for some reason it doesn't seem to be working. I'm using eclipse and I've included the Support libraries with resources exactly as specified in Support Library Setup[developer.android.com].

My manifest file has minsdk version as 7, targetsdk version as 18, and the build target is also 18.

I suspect something is amiss in the support library setup, can someone please tell me what I might be doing wrong? Thanks!

like image 830
phininity Avatar asked Jan 21 '14 17:01

phininity


1 Answers

Maybe SearchView wasn't showed because you missed to add a collapseActionView in this line: yourapp:showAsAction="always".

Also, your activity must extends AppCompatActivity. So, add AppCompat library to project

More details you can read on this link

Hope it will help you.

like image 112
Volodymyr Yatsykiv Avatar answered Oct 06 '22 13:10

Volodymyr Yatsykiv