Reputation: 890
My toolbar has a searchView
inside and both ate not registering any touch events on android 4.3 (in fact the event goes to the view below them). It should register homeAsUp
, the searchView
field (requestFocus
) and X button to clear the field. For now what I am thinking is attaching listeners to these but is there a better way.
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
MenuItem menuItem = menu.findItem(R.id.app_bar_search);
SearchView searchView = new SearchView(this);
searchView.setIconifiedByDefault(false);
searchView.setIconified(false);
searchView.requestFocus();
searchView.setMaxWidth(Integer.MAX_VALUE);
searchView.setQueryHint(getString(R.string.search));
ImageView searchViewIcon = (ImageView)searchView.findViewById(android.support.v7.appcompat.R.id.search_mag_icon);
ViewGroup linearLayoutSearchView =(ViewGroup) searchViewIcon.getParent();
linearLayoutSearchView.removeView(searchViewIcon);
menuItem.setActionView(searchView);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) { return false; }
@Override
public boolean onQueryTextChange(String newText) {
presenter.queryChanged(newText);
return true;
}
});
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.search, menu);
return true;
}
menu/search.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/app_bar_search"
app:actionViewClass="android.support.v7.widget.SearchView"
android:icon="@drawable/ic_search_white_24dp"
android:title="@string/search"
app:showAsAction="always" />
</menu>
It works fine in newer versions though (don't know which one is the border between working/not working).
EDIT: Attaching a listener to the searchview doesn't work either.
Upvotes: 0
Views: 803
Reputation: 836
don't make new instance of SearchView fetch it from the menuItem:
replace this line
SearchView searchView = new SearchView(this);
with
SearchView searchView = (SearchView) menuItem .getActionView();
and remove following line:
menuItem.setActionView(searchView);
following code is working fine for me:
@Override
public boolean onCreateOptionsMenu( Menu menu) {
getMenuInflater().inflate( R.menu.menu_main, menu);
MenuItem myActionMenuItem = menu.findItem( R.id.action_search);
final SearchView searchView = (SearchView) myActionMenuItem.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
if (TextUtils.isEmpty(newText)) {
adapter.filter("");
listView.clearTextFilter();
} else {
adapter.filter(newText);
}
return true;
}
});
return true;
}
Upvotes: 3
Reputation: 353
what about doing it using EditText ... it is going to run for all versions.
1- In YourActivity.xml remove android.support.design.widget.AppBarLayout
attribute then add
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="@style/AppTheme.PopupOverlay" >
<EditText
android:id="@+id/search_edit_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="24dp"
android:backgroundTint="[TINT_COLOR]"
android:hint="Search"
android:inputType="[INPUT TYPE]"
android:textColorHint="[HINT_COLOR]"
android:layout_marginEnd="16dp"/>
</android.support.v7.widget.Toolbar>
2-In YourActivity.java code add this listener on search edittext
private TextWatcher searchTextWatcher =
new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
presenter.queryChanged(charSequence.toString());
}
@Override
public void afterTextChanged(Editable editable) {
}
};
Upvotes: 0