Hint in Search Widget within Action Bar is not showing up

I have a Search Widget within an Action Bar. I have set up an android:hint value however It is not showing up when I click on the Search Icon.

Relevant Files :

  • How to install/replace on Android without using Eclipse
  • Bluetooth Low Energy Sensor Simulator
  • java.lang.IllegalStateException: ScrollView can host only one direct child
  • Long.getLong() failing, returning null to valid string
  • How much of the core Java APIs are present in Android API
  • TextInputLayout: Different color for hint label when not focused
  • MainActivity.java

     package com.example.myApp;
    
    import android.os.Bundle;
    import android.app.ActionBar;
    import android.app.Activity;
    import android.app.SearchManager;
    import android.content.Context;
    import android.view.Menu;
    import android.widget.SearchView;
    
    public class MainActivity extends Activity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        //creates an action bar. 
        ActionBar actionBar = getActionBar();
        //sets the homebutton. 
        actionBar.setDisplayHomeAsUpEnabled(true);
    }
    
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
    
        // Associate searchable configuration with the SearchView
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        return true;
      }   
    }
    

    Main.xml => res/menu/Main.xml

    <menu xmlns:android="http://schemas.android.com/apk/res/android">
    
    
        <item android:id="@+id/search"
              android:title="@string/search_title"
              android:icon="@drawable/ic_search"
              android:showAsAction="collapseActionView|ifRoom"
              android:actionViewClass="android.widget.SearchView"       
              />
    </menu>
    

    searchable.xml => res/XML/searchable.XMl

     <?xml version="1.0" encoding="UTF-8"?>
    
        <searchable xmlns:android="http://schemas.android.com/apk/res/android"
            android:label="@string/app_name"
            android:hint="search"
            android:textColorHint= "#FF0000"      
        />  
    

    Mainfest.XMl

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.MyApp"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="11"
            android:targetSdkVersion="17" />
    
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.example.MyApp.MainActivity"
                android:label="@string/app_name" >
    
                 <meta-data android:name="android.app.searchable"
                android:resource="@xml/searchable" />
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    
    </manifest>
    

    My attempt :

    My action-bar has a black background so I assumed that I initially could not see the :hint value as the hint text color is black is as well. However even after changing the hintTextColor, I still can not see the hint value.

    Any ideas?

    Thanks.

    Related posts:

    Jacoco with Gradle 0.10.0: Remote object doesn&#039;t exist
    Android Deep linking: Use the same link for the app and the play store
    MPAndroidChart hide background grid
    Android how to turn on do not disturb (dnd) programmatically
    WRAP_CONTENT not working after dynamically adding views
    Vimeo video stops playing on Android 6 devices
  • Android: WebView's method goBack() shows a blank page
  • How to get a contact's facebook id or url from native contacts / content resolver?
  • Android LinearGradient XML
  • Android Studio Stop Button not Stopping
  • Resetting Autoincrement in Android SQLite
  • How to Write consecutive Characteristic fast and stable for BLE in Android?
  • 6 Solutions collect form web for “Hint in Search Widget within Action Bar is not showing up”

    For some reason you need to put <action android:name="android.intent.action.SEARCH"/> inside your tag. That’s what fixed the problem for me.
    But be careful to put it in this order

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

    otherwise your activity won’t be recognized as launcher

    No one has been able to answer my question yet…

    I have found a way to change the hint value in the .java file but not in the .xml file.

    Within the onCreateOptionsMenu(Menu menu) method of MainActivity.java, I can do:

    searchView.setQueryHint("HINT TEXT...");
    

    I believe I should still be able to preset a hint value of a SearchWidget in XML just like a simple EditText.

    Your have to provide a string resource for the hint attribute. Hardcoded text won’t work. Instead of

    android:hint="search"
    

    use a string resource

    android:hint="@string/search_hint"
    

    The doc is clear about it: http://developer.android.com/guide/topics/search/searchable-config.html

    android:hint="string resource"
    

    If you want to skip to the solution, just scroll down

    Explanation

    It appears this would be a bug in SearchView.

    Edit: I reported this bug here.
    Edit 2 : seems to be fixed for a future release, see here.

    In the constructor, it assigns a value to the field mQueryHint.

    public SearchView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        ...
        final CharSequence queryHint = a.getText(R.styleable.SearchView_queryHint);
        if (!TextUtils.isEmpty(queryHint)) {
            setQueryHint(queryHint);
        }
        ...
    }
    
    public void setQueryHint(CharSequence hint) {
        mQueryHint = hint;
        updateQueryHint();
    }
    

    Later, when you set the SearchableInfo (you know, the object it should read the hint from), the following happens:

    public void setSearchableInfo(SearchableInfo searchable) {
        mSearchable = searchable;
        if (mSearchable != null) {
            ...
            updateQueryHint();
        }
        ...
    }
    

    All seems ok until now. It saves the searchableInfo in a field called mSearchable. Let’s see what updateQueryHint does:

    private void updateQueryHint() {
        if (mQueryHint != null) {
            mSearchSrcTextView.setHint(getDecoratedHint(mQueryHint));
        } else if (IS_AT_LEAST_FROYO && mSearchable != null) {
            CharSequence hint = null;
            int hintId = mSearchable.getHintId();
            if (hintId != 0) {
                hint = getContext().getString(hintId);
            }
            if (hint != null) {
                mSearchSrcTextView.setHint(getDecoratedHint(hint));
            }
        } else {
            mSearchSrcTextView.setHint(getDecoratedHint(""));
        }
    }
    

    What? So if mQueryHint already has a value, we never even read the searchable info? That explains why we never see our hint, because mQueryHint is assigned to a default value in the SearchView constructor (see above).

    Solution

    So how do we work around this bug?

    Two options:

    1. Use setQueryHint with the new value.

    Example:

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the options menu from XML
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.your_menu, menu);
    
        // Get the SearchView and set the searchable configuration
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
        searchView.setOnQueryTextListener(this);
        // Assumes current activity is the searchable activity
        SearchableInfo searchableInfo = searchManager.getSearchableInfo(getComponentName());
        searchView.setSearchableInfo(searchableInfo);
    
        // Override the hint with whatever you like
        searchView.setQueryHint(getResources().getString(R.strings.your_hint));
    
        return true;
    }
    
    1. Use setQueryHint with null BEFORE you set the SearchableInfo

    Example:

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the options menu from XML
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.your_menu, menu);
    
        // Get the SearchView and set the searchable configuration
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
        searchView.setOnQueryTextListener(this);
        // Assumes current activity is the searchable activity
        SearchableInfo searchableInfo = searchManager.getSearchableInfo(getComponentName());
    
        // DO THIS BEFORE setSearchableInfo !!!
        searchView.setQueryHint(null);
    
        searchView.setSearchableInfo(searchableInfo);
    
        return true;
    }
    

    Both confirmed working with appcompat v7.

    Accepted answer does indeed work, but only for the MAIN activity. If you would want to use it in another activity it would not work.

    The below code is taken from android developer site

    <activity
        android:name="com.example.Activities.MainActivity"
        android:label="@string/app_name">
    
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
    
        <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable"/>
    
    </activity>
    

    You can use

    android:queryHint="Hint text"
    

    in your Search Widget in XML.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.