Search code examples
androidsearchview

How to create a visible hint in SearchView


I want to create a hint for the user in my Search-view, however the hint isn't visible until the user presses the magnifying glass icon. Is the there a way to show the hint even if the user didn't click anything just like with Edit-text? Moreover I want the borders of the search view to be visible and not just the icon, is there a way to achieve it? Thanks in advance.

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/header_image_id"
            android:orientation="vertical"
            android:layoutDirection="rtl">

            <TextView
                android:id="@+id/text_view_id"
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:textAlignment="center"
                android:text="Enter your search request here?"/>


            <SearchView
                android:id="@+id/search_view_id"
                android:layout_width="250dp"
                android:layout_height="wrap_content"
                android:queryHint="This is a hint" />


        </LinearLayout>

Solution

  • We have attribute for that setIconifiedByDefault (boolean iconified)

    Sets the default or resting state of the search field. If true, a single search icon is shown by default and expands to show the text field and other buttons when pressed. Also, if the default state is iconified, then it collapses to that state when the close button is pressed. Changes to this property will take effect immediately.

    The default value is true. https://developer.android.com/reference/android/widget/SearchView.html#setIconifiedByDefault(boolean)

    What you need to do:

    <SearchView
            android:id="@+id/search_view_id"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:queryHint="This is a hint"
            android:iconifiedByDefault="false"/>