13

I am trying to use SearchViewCompat with ActionBarSherlock in an API 8 app.

public boolean onCreateOptionsMenu(Menu menu) {
    MenuItem item = menu.add("Search")
        .setIcon(isLight ? R.drawable.ic_search_inverse : R.drawable.ic_search)
        .setActionView(R.layout.collapsible_edittext);
    item.setShowAsAction(
        MenuItem.SHOW_AS_ACTION_ALWAYS | 
        MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);

    // To use SearchViewCompat, I need to add it to the Menu item as well:
    View searchView = SearchViewCompat.newSearchView(this);
    // ...
    SearchViewCompat.setOnQueryTextListener(...);
    // ...
    item.setActionView(searchView);

Please note that both the top and bottom code needs to call setActionView(). Does that mean it is not possible to do search?

JJD
  • 50,076
  • 60
  • 203
  • 339
woodglue
  • 223
  • 2
  • 11
  • 1
    Instead of asking for code you can add the code you've got so far to your question. Perhaps people can help you fix your problem. – THelper Apr 19 '12 at 07:47
  • 1
    Thanks THelper. Also looks like my local document is out of date and I found a specific developer topic: http://developer.android.com/training/search/backward-compat.html – woodglue Apr 20 '12 at 05:22

4 Answers4

6

If you are using the ActionBarSherlock Library ver 4.2, you can replace the API 11 SearchView Widget with a ActionBarSherlock SearchView Widget to make it backward compatible:

search.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/menu_search"
        android:icon="@drawable/ic_action_search"
        android:title="@string/description_search"
        android:orderInCategory="0"
        android:actionViewClass="com.actionbarsherlock.widget.SearchView"
        android:showAsAction="ifRoom|collapseActionView" /> 
</menu>

Activity class

//IMPORTANT!!!
import com.actionbarsherlock.widget.SearchView;

...

@Override 
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    getSupportMenuInflater().inflate(R.menu.search, menu);
    setupSearchMenuItem(menu);
    return true;
}

private void setupSearchMenuItem(Menu menu) {
    MenuItem searchItem = menu.findItem(R.id.menu_search);
    if (searchItem != null) {
        SearchView searchView = (SearchView) searchItem.getActionView();
        if (searchView != null) {
            SearchManager searchManager = 
                 (SearchManager) getSystemService(SEARCH_SERVICE);
            searchView.setSearchableInfo(
                searchManager.getSearchableInfo(getComponentName()));
            }
        }
    }
}
JJD
  • 50,076
  • 60
  • 203
  • 339
TouchBoarder
  • 6,422
  • 2
  • 52
  • 60
2

What is the actual problem? SearchViewCompat will return null for pre-HC devices since the SearchView widget does not exist. This means you will have to provide your own custom action view that imitates the HC SearchView.

You can also backport the SearchView component from the Android sources and use that.

Otherwise, you can just use the existing search interfaces Android has, in which case for HC+ devices you use the action view to perform a search but on Froyo and Gingerbread devices the user clicks on the search icon and a search bar animates from the top.

Hope this helps.

dnkoutso
  • 6,041
  • 4
  • 37
  • 58
2

At some point in your Activity:

public class HomeActivity extends SherlockFragmentActivity implements 
    SearchView.OnQueryTextListener {

// ...    
SearchView searchView = (com.actionbarsherlock.widget.SearchView) 
    actionBarCustom.findViewById(R.id.search);
SearchManager sm = (SearchManager)getSystemService(SEARCH_SERVICE);
searchView.setSearchableInfo(sm.getSearchableInfo(getComponentName()));
searchView.setSubmitButtonEnabled(true);
searchView.setOnQueryTextListener(this);

And then filter your list adapter:

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

@Override
public boolean onQueryTextChange(String newText) {
    mAdapter.getFilter().filter(newText.trim());
    return false;
}

This way, your list adapter must implement filterable.

JJD
  • 50,076
  • 60
  • 203
  • 339
M.Kouchi
  • 800
  • 7
  • 12
0

Better to use MenuItemCompat,I think this is helpful for you

    getMenuInflater().inflate(R.menu.main, menu);
    MenuItem searchItem = menu.findItem(R.id.search);
    SearchManager searchManager =(SearchManager)getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView)MenuItemCompat.getActionView(searchItem);
    SearchableInfo info =  searchManager.getSearchableInfo(getComponentName());
    searchView.setSearchableInfo(info);
chaman
  • 37
  • 6