Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android.support.v4.widget.SearchViewCompat example?

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?

like image 688
woodglue Avatar asked Apr 19 '12 05:04

woodglue


Video Answer


3 Answers

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()));
            }
        }
    }
}
like image 59
TouchBoarder Avatar answered Oct 16 '22 16:10

TouchBoarder


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.

like image 27
dnkoutso Avatar answered Oct 16 '22 18:10

dnkoutso


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.

like image 2
M.Kouchi Avatar answered Oct 16 '22 18:10

M.Kouchi