Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Search View: Query Hint visible

Is there a way to have the query hint of a Search view always visible? What I want is even if the search view is not selected, the user can see what is it purpose.

    <SearchView
    android:id="@+id/search"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="7"
    android:queryHint="@string/searchHint" />

I tried searchView.setIconifiedByDefault(false); but it activate the search view by default.

Thanks

like image 769
Ynnad Avatar asked Mar 15 '23 10:03

Ynnad


1 Answers

Here is my solution. It's a bit of a workaround, but so far works for me.

// Boolean to determine if the activity has just been launched

    private boolean activityStartup = true;

// Inside onCreate

    searchView.setIconifiedByDefault(false);
    searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
           @Override
           public void onFocusChange(View v, boolean hasFocus) {
               if (hasFocus) {
                   if (activityStartup) {
                       searchView.clearFocus();
                       activityStartup = false;
                   }
               }
           }
    });

Thus the query hint is always show, and when (and only when) the activity first launches, the SearchView focus is automatically cleared, hiding the keyboard. The keyboard will be shown when the SearchView is tapped/focused manually.

like image 196
akortze Avatar answered Mar 29 '23 11:03

akortze