SearchView hint not showing

yian.athanasiadis picture yian.athanasiadis · Jun 20, 2016 · Viewed 26.1k times · Source

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

Answer

AmanSinghal picture AmanSinghal · Jun 20, 2016

To add SearchView hint text use the following code :

search.setQueryHint("Custom Search Hint");