Skip to content

Instantly share code, notes, and snippets.

@yishai-glide
Created May 28, 2015 10:30
Show Gist options
  • Save yishai-glide/1f9496950d2fbeb8f1ec to your computer and use it in GitHub Desktop.
Save yishai-glide/1f9496950d2fbeb8f1ec to your computer and use it in GitHub Desktop.
toolbar searchview
private void configureSearchView(Menu menu) {
MenuItem searchItem = menu.findItem(R.id.action_search);
searchViewAction = (SearchView) MenuItemCompat.getActionView(searchItem);
searchViewAction.setOnQueryTextListener(this);
searchViewAction.setOnCloseListener(this);
searchViewAction.setSubmitButtonEnabled(false);
searchViewAction.setIconifiedByDefault(true);
EditText editText = (EditText) searchViewAction.findViewById(android.support.v7.appcompat.R.id.search_src_text);
editText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {/** empty method **/}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,int after) {/** empty method **/}
@Override
public void afterTextChanged(Editable s) {
mAdapter.getFilter().filter(s);
}
});
}
<?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" >
<!-- Search, should appear as action button -->
<item
android:id="@+id/action_search"
android:icon="@drawable/abc_ic_search_api_mtrl_alpha"
android:title="@string/fragment_search_text"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always|collapseActionView"/>
<item
android:id="@+id/action_title"
app:showAsAction="always"
android:icon="@drawable/check_icon"
android:title="@string/title"/>
<!-- -->
</menu>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment