Search code examples
androidsearchview

Searchview Iconified without Focus


I Have created a Searchview in android. I want to keep the hint text along the search icon so I have set in xml as

app:iconifiedByDefault="false"

It solves my problem. But it takes the focus when entering to the activity which will trigger the keyboard. Now I want to keep the hidden when entering to activity.

Is there any default solution, other than creating searchview through EditText??


Solution

  • This is intended behavior, when View is created the first focusable View get the focus, however you can create a dummy object to intercept the focus. Easiest solution is to simply add this to the root view of your layout:

    android:focusableInTouchMode="true"
    android:descendantFocusability="beforeDescendants"
    

    P.S: what these lines of code do it to make your root layout focusble and set the focusability of the View before it's descendants so it intercept the focus before moving to child views.