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
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" . This is the answer without creating a function or code in the Activity. Just add this in the layout.
To make the SearchView expanded by default, call setIconifiedByDefault(false) on it when you initialise it (e.g. in onCreateOptionsMenu(..) or onPrepareOptionsMenu(..) ). I've found in most cases this will give it focus automatically, but if not simply call requestFocus() on it too.
You can use setQuery() to change the text in the textbox. However, setQuery() method triggers the focus state of a search view, so a keyboard will show on the screen after this method has been invoked. To fix this problem, just call searchView.
To add SearchView hint text use the following code :
search.setQueryHint("Custom Search Hint");
try to use app:queryHint="@string/search" app:defaultQueryHint="@string/search"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With