10

I'm trying to implement a search on my android application using a search bar widget in the action bar.

I am following

http://developer.android.com/guide/topics/search/search-dialog.html http://developer.android.com/guide/topics/ui/actionbar.html#ActionView

tutorials to get this done.

I have two activities involved in searching. SearchBar activity has the action bar and AcceptSearch is my searchable activity. Even though I declared which activity was the searchable activity, the query is not being sent over to AcceptSearch and the activity is not launched.

I configured the search bar such that a search bar hint appears when the widget is empty, but the hint never appears either. Here is my code.

SearchBar

public class SearchBar extends Activity 
{

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.search_bar);
}

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    //Inflate the options menu from XML
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.options_menu, menu);

    // Get the SearchView and set the searchable configuration
    SearchManager searchManager = (SearchManager)getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    searchView.setIconifiedByDefault(false);        
    return true;        
}

AcceptSearch

public class AcceptSearch extends ListActivity
{
@Override
protected void onCreate(Bundle savedInstanceState) 
{       
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash_screen);

    //Get the intent, verify the action and get the query
    Intent intent = getIntent();
    if(Intent.ACTION_SEARCH.equals(intent.getAction()))
    {
        String query = intent.getStringExtra(SearchManager.QUERY);  

        //Start the search
        doMySearch();
    }
}

Searchable.xml

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

options_menu

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >    
<item
    android:title="Help"
    android:id="@+id/menu_help"
    android:showAsAction="always"
/>
<item
    android:title="Categories"
    android:id="@+id/menu_cats"
    android:showAsAction="always"
/>
<item 
    android:id="@+id/menu_search"
    android:title="Search with Searchlet"
    android:icon="@drawable/ic_action_search"
    android:showAsAction="ifRoom|collapseActionView"
    android:actionViewClass="android.widget.SearchView"/>
</menu>

Manifest

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.searchlet"
    android:versionCode="1"
    android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="15" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <activity
        android:name=".SplashScreenActivity"
        android:label="@string/title_activity_splash_screen"
        android:screenOrientation="portrait" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name=".AcceptSearch">
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
        <meta-data android:name="android.app.searchable"
            android:resource="@xml/searchable"/>
    </activity>

    <activity android:name=".SearchBar"
              android:label="@string/app_name">                  
        <intent-filter>
            <action android:name="com.example.searchlet.CLEARSCREEN" />
            <category android:name="android.intent.category.DEFAULT" />

        </intent-filter>
    </activity>

</application>

</manifest>

I beleive thats all the needed information to recreate the situation.

Thank you all in advance. I apprecicate.

myselfesteem
  • 733
  • 1
  • 6
  • 23

7 Answers7

20

The tutorials seems to missing one important part. You have to add <meta-data android:name="android.app.default_searchable" android:value=".MySearchActivityName" /> inside <application> tags.

Indrek Kõue
  • 6,449
  • 8
  • 37
  • 69
  • Thank you, I will try this solution in the morning. (Sry for late reply) – myselfesteem Sep 11 '12 at 04:33
  • 1
    Yea, that wasn't the answer on that one :( – myselfesteem Oct 06 '12 at 05:09
  • This worked for me. I am following this [guide](http://developer.android.com/training/search/setup.html) for the action bar search widget. This was actually included in the [tutorial](http://developer.android.com/guide/topics/search/search-dialog.html) although as part of instructions in a search dialog and not in action bar search widget. – Nico Dumdum Oct 20 '14 at 04:01
  • @ShajeelAfzal Hi, it isn't working for me either. Did you find a solution? May be you can look at my question [here](http://stackoverflow.com/questions/27567796/android-assisted-search-the-search-button-does-not-invoke-the-searchable-activi)? – Solace Dec 22 '14 at 16:47
  • 1
    @Zarah what problem are you facing? – Shajeel Afzal Dec 23 '14 at 12:42
  • @ShajeelAfzal The Searchable Activity just won't start when I press return, when using a Search View. [You can see my test application's code here.](http://stackoverflow.com/questions/27567796/android-assisted-search-the-search-button-does-not-invoke-the-searchable-activi) Thank you for reply, will be grateful if you can help with this. – Solace Dec 24 '14 at 02:45
13

I had a similar problem. I was following grokking's tutorial and the activity never opened. I tried the Indrek's solutions I that works for me.

You should be sure that you have the right meta-data under the right parent. The meta-data

<meta-data
     android:name="android.app.default_searchable"
     android:value=".NameSearchActivity" />

should be under application. The follow meta-data

<meta-data
     android:name="android.app.searchable"
     android:resource="@xml/searchable" />

should be under the search activity parameter inside AndroidManifest.xml.

In the search Activity I did what it say in the tutorial linked before for managing the activity stack. I hope that this solution works for everyone with the same problem.

2

Pau Arlandis Martinez, thank you. Your solution works fine and tutorial with activity stack is helpful. I put into <application> section this meta-data:

<meta-data
 android:name="android.app.default_searchable"
 android:value=".NameSearchActivity" />

And I noticed that @Override onQueryTextSubmit method should return false to start searchable activity. Otherwise searchButton on virtual keyboard doesn't work.

  • Hey, as this is not an answer on the original question it would have been great if you put this as comment to the answer you are referring to, to make things clearer. – ChristianM Jun 10 '15 at 20:12
2

I was having trouble with the SearchView not launching the SearchActivity, it turned out to be a package/directory issue.

If the SearchableActivity is not located in the same directory as the Activity hosting your SearchView, you need to change:

searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

to

searchView.setSearchableInfo(searchManager.getSearchableInfo(new ComponentName(this, SearchableActivity.class)));
Tim Malseed
  • 6,003
  • 6
  • 48
  • 66
2

For anyone else who still has this issue, I also came across this issue, if the Searchable Activity extends AppCompatActivity the activity wont start, I changed it to extend Activity instead and it worked.

Neco Horne
  • 148
  • 2
  • 8
1

You are missing meta-data in '.SearchBar' Activity of Manifest file, which directs to '.AcceptSearch' activity.

Add following meta data to '.SearchBar' activity:

<!-- search widget target activity-->
        <meta-data android:name="android.app.default_searchable"
                    android:value=".AcceptSearch" />

Here is updated code for '.SearchBar' activity manifest.

<activity android:name=".SearchBar"
              android:label="@string/app_name"> 
        <!-- search widget target activity-->
        <meta-data android:name="android.app.default_searchable"
                android:value=".AcceptSearch" />
        <intent-filter>
            <action android:name="com.example.searchlet.CLEARSCREEN" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
Dhanesh
  • 61
  • 2
  • 6
0

Hint and label in the searchable.xml are references, and not hardcoded strings.

 <searchable xmlns:android="http://schemas.android.com/apk/res/android"
      android:hint="@string/search_hint"
      android:label="@string/app_name"/>

This how the Activity which contains the SearchView widget should be declared in the manifest

<activity
    android:name="ACTIVITY PATH WHICH CONTAINS SEARCHVIEW WIDGET">

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

        <meta-data
            android:name="android.app.default_searchable"
            android:value=".SearchableActivity" />

</activity>

Declaration for searchable activity in the manifest.

 <activity android:name=".SearchableActivity">

        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>

        <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable" />

 </activity>