20

I have a search view and I have a query hint attribute. However the hint appears only after clicking on the search view. Is there a way to make it appear before it has been clicked?

<SearchView
        android:id="@+id/searchView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/textView4"
        android:layout_marginBottom="14dp"
        android:layout_marginTop="30dp"
        android:queryHint="Search" />

enter image description here

enter image description here

I have seen another SO question with a similar query, but it wasn't solved. That's why I am asking again.

ichigo gyuunyuu
  • 693
  • 2
  • 6
  • 18
  • 2
    because it is in "iconified" state – Selvin Nov 06 '15 at 12:34
  • Here my solution: https://stackoverflow.com/questions/46681500/android-4-3-not-focus-searchview-not-show-hint?noredirect=1#comment80355499_46681500 – Alex Oct 12 '17 at 08:02

9 Answers9

28

Use this XML:

android:queryHint="hint"
app:queryHint="hint"
app:defaultQueryHint="hint"
android:iconifiedByDefault="false"  // this line
app:iconifiedByDefault="false"

app used for {android.support.v7.widget.SearchView}

Gabriel
  • 377
  • 1
  • 3
  • 15
Ali Bagheri
  • 3,068
  • 27
  • 28
14

Try this:

SearchView searchView = (SearchView) findViewById(R.id.searchView);
searchView.onActionViewExpanded();
searchView.setIconified(true);

and try this to hide focus (Keyboard)

 new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                search.clearFocus();
            }
    }, 300);
Selim YILDIZ
  • 378
  • 3
  • 15
6

You can just add android:iconifiedByDefault="false" it just displays the query hint string by default

Thiru A P
  • 151
  • 1
  • 3
5

This is a complete XML, You can try this.

<androidx.appcompat.widget.SearchView
                android:id="@+id/searchView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:theme="@style/PlayerSearchView"
                android:iconifiedByDefault="false"
                android:paddingStart="-4dp"
                app:iconifiedByDefault="false"
                app:searchIcon="@drawable/ic_person_search"
                app:closeIcon="@drawable/ic_close"
                app:queryBackground="@android:color/transparent"/>

Add this in style

<style name="PlayerSearchView" parent="Widget.AppCompat.SearchView">
        <item name="queryHint">@string/search_player</item>
        <item name="android:textSize">@dimen/_10ssp</item>
        <item name="fontFamily">@font/montserrat_medium</item>
        <item name="android:textStyle">bold</item>
        <item name="android:textColor">@color/colorBlack</item>
        <item name="android:queryHint">@color/colorBlack</item>
    </style>
Md. Shofiulla
  • 2,135
  • 1
  • 13
  • 19
3

make setIconified(false)-that works for me that works,but when it is unclicked,again hint disappears

amyShamna
  • 962
  • 8
  • 8
2

use those attributes

<SearchView
.
.
android:iconifiedByDefault="false"
app:iconifiedByDefault="false"
Mahmoud Aly
  • 578
  • 3
  • 10
0

This worked for me:

searchView.onActionViewExpanded();
new Handler().postDelayed(new Runnable() 
{
    @Override
    public void run() {
          searchView.clearFocus();
    }
}, 300);

Also, don't forget to add these to manifest.xml:

<intent-filter>
    <action android:name="android.intent.action.SEARCH"/>
    <action android:name="android.intent.action.MAIN" />

    <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
Pang
  • 9,564
  • 146
  • 81
  • 122
-1

Try this:

SearchView searchView = new SearchView(getSherlockActivity().getSupportActionBar().getThemedContext());
  searchView.setQueryHint("Search for countries…");
  searchView.setIconified(false);

  menu.add("Search")
      .setIcon(R.drawable.abs__ic_search)
      .setActionView(searchView)
      .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
Arun Shankar
  • 2,603
  • 2
  • 26
  • 36
-4

In xml, You can use this android:queryHint="Search"

Lavanya
  • 41
  • 1
  • 1
  • 3