Home > DeveloperSection > Forums > Show hint in search widget actionBar andorid
lois waisbrooker
lois waisbrooker

Total Post:70

Points:494
Posted on    December-24-2015 2:53 AM

 Android Android 
Ratings:


 1 Reply(s)
 245  View(s)
Rate this:
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.

My Activity Class and Xml layouts: 

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


Menu Xml: 
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>

Search Xml
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 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.

Please help, Why it is not showing ?


Mayank Tripathi
Mayank Tripathi

Total Post:397

Points:3117
Posted on    December-24-2015 3:05 AM

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

Don't want to miss updates? Please click the below button!

Follow MindStick