2

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?

1 Answer 1

2

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;
}
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for your answer! Well my searchView is not always expanded, only after clicking on the search icon. I just tried setting searchView.setIconifiedByDefault(true) and now I have an extremly strange behaviour. If got a big search icon, and by clicking on that icon a smaller search icon appears and after clicking on the small search icon, every thing works as expected. So it seems my search functionality is behind another button.
Okay. I anyway edited my answer. Hope it helps someone.
Thank you! I definitely can make use of your method. Maybe you have also an idea how to solve the new problem. Thanks again!
You can try adding android:orderInCategory="102" to your settings item in menu.xml
Unfortunately it doesn't help. I tried to remove the action for the settings button but also the default settings button seems to be half overlaid.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.