Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 307
Keselme Avatar asked Jan 21 '18 14:01

Keselme


People also ask

How do I add hint in search?

Using the XML configuration. If the hint's desired to be visible in the Non-Active State of SearchView set android:iconifiedByDefault to "false" . If it's undesired set android:iconifiedByDefault to "true" . This is the answer without creating a function or code in the Activity. Just add this in the layout.

How to create search view in android studio?

SearchView widget can be implemented over ToolBar/ActionBar or inside a layout. SearchView is by default collapsible and set to be iconified using setIconifiedByDefault(true) method of SearchView class. For making search field visible, SearchView uses setIconifiedByDefault(false) method.

How to search data in android using SearchView?

Search View widget is used to provide a search interface to user so that the user can enter his search query and submit a request to search provider and get a list of query suggestions or results. It consist of Relative Layout with ListView in it from which data is to be search.


1 Answers

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"/>
like image 146
Pankaj Kant Patel Avatar answered Oct 19 '22 20:10

Pankaj Kant Patel