9

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!

phininity
  • 1,673
  • 2
  • 14
  • 9
  • 1
    Please check my answer here: http://stackoverflow.com/questions/18832890/android-nullpointerexception-on-searchview-in-action-bar/18942838#18942838. It may help you. – Szymon Jan 22 '14 at 04:18

1 Answers1

16

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.

Volodymyr Yatsykiv
  • 3,181
  • 1
  • 24
  • 28
  • Thanks! The issue I was having was that I didn't extend from ActionBarActivity , thanks for pointing this out! – phininity Mar 28 '14 at 17:29
  • Hi! Do you guys see the close button (x) when textview is empty? I can't manage to reproduce that behaviour when using appcompat's SearchView but I certainly observe it when using Android's 'android.widget.SearchView'. Anyone having this problem? – acrespo Jun 24 '14 at 18:06