1

I am creating a contacts list view and want to add search view to that activity. I have created a searchable.xml and the contents of which is pasted below

<?xml version="1.0" encoding="utf-8"?>
<searchable
xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name"
android:hint="@string/search_hint">
</searchable>

I have also added the meta-data tag in the activity as shown here

<activity
        android:name=".ContactsWindow"
        android:label="@string/title_activity_contacts_window" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable">
        </meta-data>
    </activity>

I have overridden the onCreateOptionsMenu() method

public boolean onCreateOptionsMenu(Menu menu) {
    getActionBar().show();
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_contacts_window, menu);

    SearchManager searchManager = (SearchManager)
            getSystemService(Context.SEARCH_SERVICE);
    searchMenuItem = menu.findItem(R.id.menuSearch);
    searchView = (SearchView) searchMenuItem.getActionView();

    searchView.setSearchableInfo(searchManager.
            getSearchableInfo(getComponentName()));
    searchView.setSubmitButtonEnabled(true);
    searchView.setOnQueryTextListener(this);
    searchView.setIconifiedByDefault(false);


    return true;
}

Here is my menu xml for this activity.

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context="com.gwts.lingua.ContactsWindow">
<item android:id="@+id/menuSearch"
    android:title="Search"
    android:icon="@drawable/find"
    app:showAsAction="ifRoom|collapseActionView"
    android:actionViewClass="android.widget.SearchView" />
<item android:id="@+id/action_settings" android:title="@string/action_settings"
    android:orderInCategory="100" app:showAsAction="always" />
</menu>

When I install the apk to my phone and try to launch the app, I do not see the searchview on the action bar. What could have possibly gone wrong?

kasimoglou
  • 384
  • 3
  • 17
user1223879
  • 123
  • 3
  • 13

3 Answers3

2

You need to have this:

app:actionViewClass="android.support.v7.widget.SearchView"

instead of this:

android:actionViewClass="android.widget.SearchView"
Rafal Roszak
  • 639
  • 5
  • 12
  • for me it was `app:actionViewClass="android.widget.SearchView"`, but yes, android: must be replaces with app:. – Adrian C. Dec 08 '15 at 10:04
1

If you are using AppCompatActivity or ActionBarActivity as a parent activity than your menu class should work fine, but if you are using Activity or FragmentActivity than you have to use

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menuSearch"
    android:title="Search"
    android:icon="@drawable/find"
    android:showAsAction="always"
    android:actionViewClass="android.widget.SearchView" />

 <item android:id="@+id/action_settings" android:title="@string/action_settings"
    android:orderInCategory="100" app:showAsAction="always" />
</menu>
Haris Qurashi
  • 2,104
  • 1
  • 13
  • 28
1

I would suggest in public boolean onCreateOptionsMenu(Menu menu) return

return super.onCreateOptionsMenu(menu);

Edit:

I reproduce the problem and the solution:

Please change in

\ContactsDisplay\app\src\main\res\menu\menu_contacts_window.xml

app:actionViewClass="android.widget.SearchView"

to

android:actionViewClass="android.widget.SearchView"

Please see explanation about namespaces here

Good tutorial about the action bar here

Community
  • 1
  • 1
Eugene
  • 1,865
  • 3
  • 21
  • 24
  • Haven't tried since it takes ages to start the emulator. So trying on my phone directly :) Does that make any difference? – user1223879 May 11 '15 at 09:40
  • Is it possible to share the whole project? Then I will have chance to debug it. – Eugene May 11 '15 at 10:13
  • How to attach it here? – user1223879 May 11 '15 at 15:23
  • I am not sure that you can attach it here but you can upload to github or other online service and bring the link – Eugene May 11 '15 at 15:42
  • Sorry for the delay. You can download it from [here](https://www.dropbox.com/s/5a1dlnt8fi2ru3e/ContactsDisplay.zip?dl=0) – user1223879 May 12 '15 at 16:33
  • In ContactsWindow file @line no 163 - the searchView Object is becoming null. – user1223879 May 12 '15 at 16:36
  • Yes after the change searchView Object is not null and I see the menu. – Eugene May 13 '15 at 15:13
  • I tried your suggestion and changed it to android:actionViewClass. I see the search menu when i press the phone's menu button but not in the action bar. I need to see it in the action bar. Any idea why this could happen? Also I would like to know the significance of changing the namespace from app to android. – user1223879 May 13 '15 at 16:31