I've a SearchView
for a fragment in ActionBar
. The problem is when I first touch the icon it shifts to the left (Expanded SearchView
). Then I have to touch it again to get the text field. The below pics will clarify more.
requestFocusFromTouch();
requestFocus();
setIconifiedByDeafault(false);
setIconified(); //Works but opens keypad even before touching searchview
onPrepareOptionsMenu() and onCreateOptionsMenu() of Fragment:
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_search, menu);
}
@Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
SearchView searchView = (SearchView)menu.findItem(R.id.search).getActionView();
searchView.setIconified(false);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
mAdapter.getFilter().filter(newText);
return false;
}
});
}
menu_search.xml
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item android:id="@+id/search"
android:title="@string/search_title"
android:icon="@drawable/abc_ic_search_api_mtrl_alpha"
app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="android.widget.SearchView"/>
</menu>
Initially:
Touched once: (Expanded)
Touched again: (Focused)
If you are using appcompat-v7
, you need to use its edition of SearchView
(android.support.v7.widget.SearchView
). Mixing and matching things associated with the native action bar and with the appcompat-v7
backport does not work in general.
What you suggest on using native ActionBar and SearchView?
There are three major reasons why you might consider using appcompat-v7
:
You want an action bar consistently, and your minSdkVersion
is under 11, and so you need a backported action bar.
You want to try to adopt a Material Design-ish look and feel on Android 4.x, as appcompat-v7
will give you an action bar, and some widgets, that look more like their Theme.Material
counterparts.
You have run into problems with the native action bar implementation, perhaps on select devices due to manufacturer bugs, and you want to use a more independent implementation of the action bar to avoid those problems.
If none of those three reasons are compelling, feel free to use the native action bar and the native SearchView
.