Ynnad
Ynnad

Reputation: 303

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

Upvotes: 4

Views: 2097

Answers (2)

akortze
akortze

Reputation: 41

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.

Upvotes: 4

Sameer Khan
Sameer Khan

Reputation: 637

Did you try using searchView.setQueryHint("Query Hint") ? This inclusion should display the query hint.

Upvotes: 3

Related Questions