bpylearner
bpylearner

Reputation: 527

Why do SearchView and android.support.v7.widget.SearchView not focus anymore

I have a simple search input with minor customizations. I previously compiled against Android 6 and behavior was as expected:

When compiling against Android 7.1 (API 25), my SearchView(s) start behaving weirdly.

I migrated from android.support.v7.widget.SearchView to android.widget.SearchView to see if that fixed the problem.

Upvotes: 0

Views: 663

Answers (2)

HuyTTQ
HuyTTQ

Reputation: 388

Here is all my code working with android.support.v7.widget.SearchView

    binding.searchView.setIconified(false);
    binding.searchView.clearFocus();

    EditText searchField = (EditText)
            binding.searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);

    searchField.setFocusableInTouchMode(true);
    searchField.requestFocus();

Upvotes: 0

bpylearner
bpylearner

Reputation: 527

Apparently some internals of the View/Control have changed so the focus behavior has changed as well:

Instead of setting

searchView.ClearFocus();
searchView.Focusable = false;

(Apparently ClearFocus() was needed to keep the keyboard from popping up all the time.)

I now set

searchView.SetIconifiedByDefault(false);
searchView.Iconified = false;
searchView.RequestFocusFromTouch();

to have it expanded by default and accept inputs again.

Original helping hint was buried here:

https://stackoverflow.com/a/29876075/5872586

Hope this saves somebody some time!

PS: If somebody has an explanation of what's really going on please share your knowledge and I will accept yours as answer.

Upvotes: 2

Related Questions