SearchView gets focus and opens the keyboard when returning from another activity

When I return from another action to the main action, searchView gets focus and the keyboard opens, I managed to stop the keyboard opening using:

getWindow().setSoftInputMode( WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 

but this is not enough if the popup menu of the action bar opens and closes, the keyboard reappears because searchView still has focus (remember that I closed the keyboard). I tried to get a link to the searchView and lose focus, but that didn't work.

So basically, I just want SearchView to never have focus or be asked to open the keyboard if the text area of ​​the searchView is not affected.

+8
android searchview keyboard
source share
2 answers

This is because SearchView is the first custom view when an action returns to view - the same problem can be seen for EditText s .

The solution is the same: set an earlier view in the hierarchy to focus. In my case, it was a Toolbar my SearchView was on top:

 <android.support.v7.widget.Toolbar style="@style/ToolbarStyle" android:focusableInTouchMode="true" android:id="@+id/search_activity_toolbar" android:layout_height="?attr/actionBarSize" android:layout_width="match_parent"/> <android.support.v7.widget.SearchView android:iconifiedByDefault="false" ... /> 

Now I can focus on my SearchView when entering activity ( clearFocus(); requestFocus(...); ), and then when you exit the operation using the search result, then return by pressing β€œBack”, the keyboard is in the same state, as with the left.

Please note that some other solutions to the related issue, namely setting windowSoftInputMode="stateUnchanged" in the Activity , did not work for me.

+12
source share

upon initialization

 mSearchView.setIconifiedByDefault(false); 

And after returning, put this in onResume() or onCreateOptionsMenu (if the search is on the menu)

 mSearchView.setIconified(false); 

Then the keyboard will not open when you return until you click on searchview text searchview

0
source share

All Articles