41

I'm trying to display hint text in a search view in my Main Activity. The onqueryTextSubmit launches another activity called SearchResultsActivity which will display the results of the query. My problem is that I cannot display the hint text. My searchable.xml code

<searchable xmlns:android="http://schemas.android.com/apk/res/android"
        android:hint="@string/search_hint"
        android:label="@string/app_name"
/>

and the manifest code

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <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>

        <meta-data
            android:name="android.app.default_searchable1"
            android:value=".SearchResultsActivity"
            android:resource="@xml/searchable"/>
    </activity>
    <activity android:name=".SearchResultsActivity">
        <meta-data android:name="android.app.searchable"
                   android:resource="@xml/searchable">
        </meta-data>
    </activity>
</application>
</manifest>

and finaly my onQueryTextSubmit code

@Override
        public boolean onQueryTextSubmit(String query)
        {

            Anniversaries tempResults;
            tempResults = mainAnniversariesManager.searchManager.searchAnniversaries(query);

            if (tempResults.entries.size() == 0)
            {
                snackbar = Snackbar.make(findViewById(R.id.coordinator), getString(R.string.no_results_found) + query, Snackbar.LENGTH_INDEFINITE);
                snackbar.show();
                return true;
            } else
            {
                snackbar = null;
                if (!searchView.isIconified())
                {
                    searchView.setIconified(true);
                }
                menuItem.collapseActionView();

                Intent intent=new Intent(getApplicationContext(),SearchResultsActivity.class);
                Bundle args=new Bundle();
                args.putSerializable("serialized_Anniversaries",tempResults);
                intent.putExtra("args",args);
                startActivity(intent);

                return false;
            }
        }

Thanks in advance

yian.athanasiadis
  • 527
  • 1
  • 5
  • 6

6 Answers6

51

To add SearchView hint text use the following code :

search.setQueryHint("Custom Search Hint");
AmanSinghal
  • 2,404
  • 21
  • 22
  • 1
    I know that, I was just thinking doing it using the xml configuration. Thanks anyway. – yian.athanasiadis Jun 20 '16 at 11:48
  • `mSearchView = (SearchView) view.findViewById(R.id.product_search_view); mSearchView.setQueryHint("Search Product");` giving me `Attempt to invoke virtual method 'void android.widget.SearchView.setQueryHint(java.lang.CharSequence)' on a null object reference` – Moeez May 05 '21 at 07:24
  • @yian.athanasiadis Check https://stackoverflow.com/a/61428756/6269691 for implementing **using the xml configuration**. – Saharsh Mar 18 '22 at 07:05
29

try to use app:queryHint="@string/search" app:defaultQueryHint="@string/search"

Alexander
  • 411
  • 3
  • 4
19

Using the XML configuration.

This worked for me :

<SearchView
            .
            .
            .
            android:queryHint="@string/search_hint"
            android:iconifiedByDefault="false"    />  

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".

Saharsh
  • 383
  • 2
  • 11
3

You can use the below two lines of code to show the hint in SearchView.

1) You can simply add a hint by adding the below line.

android:queryHint="Search Medicines here..."

when you will use the above line then the hint will only appear when you click the search icon.

2) If you want to show a hint without clicking the search icon simple add the below line in the search view

android:iconifiedByDefault="false"
Abubakar Khalid
  • 109
  • 2
  • 5
2

for me non of the solutions did not work.

I get an instance of the edittext of searchview and then, I set a hint for that.

EditText et= (EditText) mSearchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
et.setHint(context.getString(R.string.search));
saleh sereshki
  • 2,402
  • 3
  • 17
  • 18
1

According Create a Searchable Configuration

If you're using a SearchView as an action view in the app bar, you should associate the searchable configuration with the SearchView by calling setSearchableInfo(SearchableInfo).

SearchManager sm = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView.setSearchableInfo(sm.getSearchableInfo(getComponentName()));
Chaos
  • 120
  • 11