Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set search hint dynamically

Does anybody knows how to set android search dialog hint dynamically? T have try to do something like:

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
 android:label="@string/search_label"
 android:hint="@string/search_hint"
 android:id="@+id/search_dialog_text">
</searchable>

Somewhere:

@Override
public boolean onSearchRequested() {
  Bundle appSearchData = new Bundle();
  appSearchData.putString("SomeSpecificParam", SomeClass.class.getName());
  startSearch("", false, appSearchData, false);
  EditText  text = (EditText )findViewById(R.id.search_dialog_text);
  text.setHint("Search something else");
  return true;
}

but text is equal null.

So looking forward you suggestions. Thanks.

like image 527
Boris Avatar asked Oct 25 '10 04:10

Boris


2 Answers

This feels pretty hacky but worked for me - not truly dynamic but worked for alternating between the two search hints I needed:

  1. I created a second searchable.xml (as described in the Android search docs), called searchable2.xml, with my second search hint defined.
  2. I created a dummy activity extended from my original activity and overriding nothing.
  3. In the manifest the dummy activity was associated with the new searchable2.xml:

    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
        <meta-data android:name="android.app.searchable"
            android:resource="@xml/searchable"/>
    </activity>
    
    <activity android:name=".DummyActivity">
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
        <meta-data android:name="android.app.searchable"
            android:resource="@xml/searchable2"/>
    </activity>
    
  4. In 'MainActivity' I overrode 'onSearchRequested()' to reference the appropriate searchable activity:

    
    public boolean onSearchRequested()
     {
           SearchManager searchManager = (SearchManager)getSystemService(Context.SEARCH_SERVICE);
    if(searchManager!=null) { // start the search with the appropriate searchable activity // so we get the correct search hint in the search dialog if(/* your condition here */) searchManager.startSearch(null, false,new ComponentName(this, MainActivity.class), null, false); else searchManager.startSearch(null, false,new ComponentName(this, DummyActivity.class), null, false); return true; } return false; }

Nasty. But desperate times call for desperate measures...

AFAIK, and from looking at the Android source the class is only used to look up the metadata, but if anybody knows differently then please let me know.

like image 198
Mister Murakami Avatar answered Oct 28 '22 12:10

Mister Murakami


They've added a note at http://developer.android.com/guide/topics/search/search-dialog.html#SearchableConfiguration stating that

Note: The system uses this file to instantiate a SearchableInfo object, but you cannot create this object yourself at runtime—you must declare the searchable configuration in XML.

So it seems that the answer to your question is that you cannot set the search dialog hint dynamically.

like image 44
Roy Solberg Avatar answered Oct 28 '22 12:10

Roy Solberg