-1

the problem
I've used the Setting Up The Search interface doc from the official Android documentation but I'm not getting far for some strange reason as the SearchView refuses to open in the Toolbar cfr. Android docs

the code
I've added the menu item as such:

<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="...">

    <item
        android:id="@+id/action_search"
        android:title="@string/action_search"
        android:icon="@android:drawable/ic_menu_search"
        app:showAsAction="collapseActionView|ifRoom"
        android:actionViewClass="android.widget.SearchView"/>
</menu>

(Sidenote, I've also tried the v7 SearchView view class, but result is no different)


This is how the toolbar is laid out

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/appbar_padding_top"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        app:theme="@style/AppTheme.Toolbar"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/AppTheme.PopupOverlay">
    </android.support.v7.widget.Toolbar>

</android.support.design.widget.AppBarLayout>

I have also added the SEARCH intent in the manifest and the menu gets properly inflated in the onCreateOptionsMenu method.

edit, manifest.xml and searchable.xml Added these by request although I don't believe they are relevant to the issue.

<activity
    android:name=".HomeActivity"
    android:label="@string/app_name"
    android:theme="@style/AppTheme.NoActionBar">
    <meta-data
        android:name="android.app.searchable"
        android:resource="@xml/searchable" />
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <action android:name="android.intent.action.VIEW" />
        <action android:name="android.intent.action.SEND" />
        <action android:name="android.intent.action.SEARCH" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

Searchable.xml

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

Now according to the Android doc the SearchView is supposed to show up but when I press the search icon in the Toolbar nothing happens.

Any ideas what could be wrong?

Liam Martens
  • 731
  • 7
  • 22

3 Answers3

0

Use menu item as such "android.support.v7.widget.SearchView"

<item
    android:id="@+id/action_search"
    android:icon="@drawable/ic_search"
    android:title="@string/lbl_search"
    app:actionViewClass="android.support.v7.widget.SearchView"
    app:showAsAction="ifRoom" />
A Maharaja
  • 1,227
  • 1
  • 11
  • 20
0

i got the solution my friend try this add your meta tag outside activity tag like this

replace your menu with

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

now in your activity write this line of code

    Toolbar toolbar= (Toolbar) findViewById(R.id.mytoolbar);
    setSupportActionBar(toolbar);



<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="pkgname">
<application
    android:name=".Utils.Application"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
<meta-data
    android:name="android.app.searchable"
    android:resource="@xml/searchable" />
<activity
android:name=".HomeActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">

<intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <action android:name="android.intent.action.VIEW" />
    <action android:name="android.intent.action.SEND" />
    <action android:name="android.intent.action.SEARCH" />
    <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
0

After looking around a lot I've finally found the solution. android:actionViewClass should be app:actionViewClass.

This is never pointed out in the Android documentation.

Liam Martens
  • 731
  • 7
  • 22