ichigo gyuunyuu
ichigo gyuunyuu

Reputation: 693

SearchView query hint before clicking it

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.

Upvotes: 20

Views: 16579

Answers (9)

Selim YILDIZ
Selim YILDIZ

Reputation: 378

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);

Upvotes: 14

Mahmoud Aly
Mahmoud Aly

Reputation: 751

use those attributes

<SearchView
.
.
android:iconifiedByDefault="false"
app:iconifiedByDefault="false"

Upvotes: 2

Md. Shofiulla
Md. Shofiulla

Reputation: 2305

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>

Upvotes: 5

India Narrative
India Narrative

Reputation: 3

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);

Upvotes: -1

Ali Bagheri
Ali Bagheri

Reputation: 3419

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}

Upvotes: 30

Thiru A P
Thiru A P

Reputation: 151

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

Upvotes: 6

user1387109
user1387109

Reputation: 1

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>

Upvotes: 0

Lavanya
Lavanya

Reputation: 41

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

Upvotes: -4

amyShamna
amyShamna

Reputation: 972

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

Upvotes: 3

Related Questions