Reputation: 342
I'm having big problems opening and closing the keyboard using a searchview in my toolbar as actionbar. When I hit the searchbutton it expends as a action and the edit text appears, but it has no focus and they keyboards doesnt open. I have to manually click on the edittext to open the keyboard. The same for closing the keyboard. When i hit the close searchaction button, they keyboard wont close.
I'm sure that this already worked without using the toolbar.... but I'm not able to fix it with toolbar. Does anybody have an idea ?
This is my menu :
<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="com.example.toolbar.MainActivity" >
<item
android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.widget.SearchView"
android:title="@string/action_search"/>
<item
android:id="@+id/action_contact"
android:icon="@drawable/ic_action_settings"
app:showAsAction="ifRoom"
android:title="@string/action_settings"/>
And this is my onCreateOptionsMenu
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
// Get the SearchView and set the searchable configuration
SearchManager searchManager = (SearchManager)getSystemService(Context.SEARCH_SERVICE);
searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
// Do not iconify the widget;expand it by default
searchView.setIconifiedByDefault(false);
setSearchIcons();
return super.onCreateOptionsMenu(menu);
}
Thanks for any advice. Cheers!
EDIT1:
Well obviously after 10 hours of struggle I ask this question and 30min later I find a solution.
It was the app:showAsAction="ifRoom|collapseActionView" collapseActionView. Deleting it did the trick. But now I have another problem. When the search view expands the settings button looks very bad. The settings button is half visible/ half cut out.
On API 21 Nexus 4 Android 5 the search view cuts out the settings button. On a THL 5000 with Android 4.4.2 the search view seems to work fine, but the icons are not white as on the Nexus but grey. Any idea?
Upvotes: 2
Views: 2850
Reputation: 878
If your searchView is not always in expanded state, then you can try below code:
mSearchView.setIconifiedByDefault(true);
mMenuItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
// Add code here
return false;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
// Add code here
return false;
}
});
To hide keyboard after clicking enter/done on keyboard:
@Override public boolean onQueryTextSubmit(String query) {
mSearchView.clearFocus();
return true;
}
Upvotes: 2