2

I am implementing an activity where I want to add a search bar. I want the current activity to receive the intent back. I followed couple of examples available and have added following code in my application:

AndroidManifest.xml

<activity
        android:name=".search.BrowseCategory"
        android:launchMode="singleTop"
        android:windowSoftInputMode="stateHidden">
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>

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

I added following in the AndroidManifest.xml under application tag:

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

Now in my BrowseCategory activity:

@Override
protected void onNewIntent(Intent intent) {
    Log.d(getPackageName(), "Search intent received!");
    setIntent(intent);
    handleIntent(intent);
}

private void handleIntent(Intent intent) {
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
      String query = intent.getStringExtra(SearchManager.QUERY);
      // Do work using string
      doSearch(query);
    }
}

private void doSearch(String query) {...}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.search_filter, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    switch(item.getItemId())
    {
        case R.id.search:
            startSearch("", false, null, false);
    break;
    }
    return true;
}

Search Menu

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
    android:id="@+id/search"
    android:actionViewClass="android.widget.SearchView"
    android:showAsAction="ifRoom"
    android:title="@string/search"/>
</menu>

Searchable XML

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

With this, I am getting a search field in my action bar. However, my onNewIntent in BrowseCategory activity is not getting call on pressing enter or search icon in soft keyboard. Can someone please help me with this issue? What am I missing here? Thanks a lot!

Keya
  • 859
  • 3
  • 9
  • 17

2 Answers2

4

After searching more, I found a solution to my problem.

Keeping everything else same as I have posted above, I just updated my onCreateOptionsMenu(Menu menu) method in the Searchable activity (BrowseCategory in my case) with the code provided below:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.search_filter, menu);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        SearchManager manager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView search = (SearchView) menu.findItem(R.id.search).getActionView();
        search.setIconifiedByDefault(false);
        search.setSearchableInfo(manager.getSearchableInfo(getComponentName()));
        search.setOnQueryTextListener(new OnQueryTextListener() { 
            @Override 
            public boolean onQueryTextChange(String query) {
                doSearch(query);
                return true; 
            }

            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }
        });
    }
    return true;
}

OnQueryTextListener is the key here! Adding this listeners triggers my search and now even pressing "Search" or "Go" on soft keyboard calls onNewIntent. About the later issue, reading from one of the answers in Cannot get searchview in actionbar to work I believe setting right SearchableInfo to searchview is important. Setting this is now triggering onNewIntent. If anyone knows more about it, please provide explanation. I will update if I find more on this too.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Keya
  • 859
  • 3
  • 9
  • 17
3

In BrowseCategory activity:

add this block

@Override
protected void onNewIntent(Intent intent) {
    if (ContactsContract.Intents.SEARCH_SUGGESTION_CLICKED.equals(intent.getAction())) {
        //handles suggestion clicked query
        String displayName = getDisplayNameForContact(intent);
        resultText.setText(displayName);
    } 
    //this else if is called when you will press Search Icon  or Go (on soft keyboard)
    else if (Intent.ACTION_SEARCH.equals(intent.getAction())) {  
        // handles a search query
        String query = intent.getStringExtra(SearchManager.QUERY);
        resultText.setText("should search for query: '" + query + "'...");
    }
}

Before click

enter image description here

After click

enter image description here

Maveňツ
  • 1
  • 12
  • 50
  • 89
  • Thanks for the reply! I do have an onNewIntent() method overridden in my Activity. However, the intent itself is not getting received. That is the main problem. What else should I do to get the intent fired? – Keya Sep 30 '14 at 07:15
  • Try the way I have suggested :) and show your `searchable.xml` @Keya – Maveňツ Sep 30 '14 at 07:17
  • Updated the question with searchable.xml content. onNewIntent is not getting called at all. – Keya Sep 30 '14 at 07:25
  • Check this http://stackoverflow.com/questions/11699206/cannot-get-searchview-in-actionbar-to-work/ – Maveňツ Sep 30 '14 at 07:33
  • Thanks for the link. Though, doesn't help me much. Though I found a solution from this link http://tpbapp.com/android-development/android-action-bar-searchview-tutorial/ – Keya Sep 30 '14 at 09:02