I'm trying to determine whether a SearchView
in the ActionBar is focused. But when I call SearchView.isFocused()
I always get false
as a result, even when the view is really focused (there is a cursor inside, and the soft keyboard is shown).
How can I check, whether a SearchView
is focused?
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.