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 do I make a JQuery Slider-like feature on iPhone/Android web app?
  • Using Android Speech Recognition APIs from Google Glass
  • pathPattern to match file extension does not work if a period exists elsewhere in the file name?
  • What versions of GLSL can I use in OpenGL ES 2.0?
  • Android SDK installation doesn't find JDK
  • android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
  • 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.

  • Android - drawing cache - when is it useful?
  • Google Plus tile animation
  • Android copy existing project with new name in Android Studio
  • R.raw.anything cannot be resolved
  • how to do form validation in android
  • What's the real purpose of Android's contentDescription tag?
  • 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.