Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 :

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.

like image 450
Rohan Avatar asked Nov 19 '13 21:11

Rohan


1 Answers

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

like image 60
Stanislav Bajt Avatar answered Oct 01 '22 08:10

Stanislav Bajt