Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SearchView hint not showing

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

like image 239
yian.athanasiadis Avatar asked Jun 20 '16 09:06

yian.athanasiadis


People also ask

How to display hint in SearchView android?

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.

How do I expand SearchView?

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.

How do I set text in SearchView?

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.


2 Answers

To add SearchView hint text use the following code :

search.setQueryHint("Custom Search Hint"); 
like image 65
AmanSinghal Avatar answered Oct 05 '22 15:10

AmanSinghal


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

like image 28
Alexander Avatar answered Oct 05 '22 14:10

Alexander