SearchView.isFocused Always Returns False
Solution 1:
After some researching, I learned that SearchView.isFocused() always returns false because it's some child of the SearchView who really has the focus, not the SearchView itself. So I use the following code to check the focus of a SearchView:
private boolean checkFocusRec(View view) {
if (view.isFocused())
return true;
if (view instanceof ViewGroup) {
ViewGroup viewGroup = (ViewGroup) view;
for (int i = 0; i < viewGroup.getChildCount(); i++) {
if (checkFocusRec(viewGroup.getChildAt(i)))
return true;
}
}
return false;
}
So I call checkFocusRec(searchView) to check for the focus. I'm not sure this is the optimal solution but it works for me.
Solution 2:
Looks like an ugly (or bug) implementation from SearchView. My suggestion is to extend the SearchView and create methods like so:
var isQueryFocused = false
private set
// you can also override hasFocus() and isFocused() methods, if you prefer
fun hasQueryFocus() = isQueryFocused
override fun setOnQueryTextFocusChangeListener(listener: OnFocusChangeListener?) {
super.setOnQueryTextFocusChangeListener { v, hasFocus ->
isQueryFocused = hasFocus
listener?.onFocusChange(v, hasFocus)
}
}
The other answers use recursive calls which is not a very efficient and private resource ID (the inner TextView id) which is a bad practice.
Solution 3:
When looking at this question and its solution, I found some extra information in the resource files included with the Android SDK (in the folder sdk/platforms/android-xx/data/res/layout/search_view.xml).
The subview holding focus has id @+id/search_src_text and is of type android.widget.SearchView$SearchAutoComplete.
I would therefore suggest the following alternative to the previous solution:
private boolean isSearchViewFocused(SearchView sview) {
View et_search = sview.findViewById(R.id.search_src_text);
return et_search.isFocused();
}
Post a Comment for "SearchView.isFocused Always Returns False"