Home » Android » java – Search bar widget not starting searchable activity

java – Search bar widget not starting searchable activity

Posted by: admin June 15, 2020 Leave a comment

Questions:

I’m trying to implement a search on my android application using a search bar widget in the action bar.

I am following

http://developer.android.com/guide/topics/search/search-dialog.html
http://developer.android.com/guide/topics/ui/actionbar.html#ActionView

tutorials to get this done.

I have two activities involved in searching. SearchBar activity has the action bar and AcceptSearch is my searchable activity. Even though I declared which activity was the searchable activity, the query is not being sent over to AcceptSearch and the activity is not launched.

I configured the search bar such that a search bar hint appears when the widget is empty, but the hint never appears either. Here is my code.

SearchBar

public class SearchBar extends Activity 
{

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.search_bar);
}

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    //Inflate the options menu from XML
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.options_menu, menu);

    // Get the SearchView and set the searchable configuration
    SearchManager searchManager = (SearchManager)getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    searchView.setIconifiedByDefault(false);        
    return true;        
}

AcceptSearch

public class AcceptSearch extends ListActivity
{
@Override
protected void onCreate(Bundle savedInstanceState) 
{       
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash_screen);

    //Get the intent, verify the action and get the query
    Intent intent = getIntent();
    if(Intent.ACTION_SEARCH.equals(intent.getAction()))
    {
        String query = intent.getStringExtra(SearchManager.QUERY);  

        //Start the search
        doMySearch();
    }
}

Searchable.xml

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

options_menu

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >    
<item
    android:title="Help"
    android:id="@+id/menu_help"
    android:showAsAction="always"
/>
<item
    android:title="Categories"
    android:id="@+id/menu_cats"
    android:showAsAction="always"
/>
<item 
    android:id="@+id/menu_search"
    android:title="Search with Searchlet"
    android:icon="@drawable/ic_action_search"
    android:showAsAction="ifRoom|collapseActionView"
    android:actionViewClass="android.widget.SearchView"/>
</menu>

Manifest

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.searchlet"
    android:versionCode="1"
    android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="15" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <activity
        android:name=".SplashScreenActivity"
        android:label="@string/title_activity_splash_screen"
        android:screenOrientation="portrait" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name=".AcceptSearch">
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
        <meta-data android:name="android.app.searchable"
            android:resource="@xml/searchable"/>
    </activity>

    <activity android:name=".SearchBar"
              android:label="@string/app_name">                  
        <intent-filter>
            <action android:name="com.example.searchlet.CLEARSCREEN" />
            <category android:name="android.intent.category.DEFAULT" />

        </intent-filter>
    </activity>

</application>

</manifest>

I beleive thats all the needed information to recreate the situation.

Thank you all in advance. I apprecicate.

How to&Answers:

I had a similar problem. I was following grokking’s tutorial and the activity never opened. I tried the Indrek’s solutions I that works for me.

You should be sure that you have the right meta-data under the right parent. The meta-data

<meta-data
     android:name="android.app.default_searchable"
     android:value=".NameSearchActivity" />

should be under application. The follow meta-data

<meta-data
     android:name="android.app.searchable"
     android:resource="@xml/searchable" />

should be under the search activity parameter inside AndroidManifest.xml.

In the search Activity I did what it say in the tutorial linked before for managing the activity stack. I hope that this solution works for everyone with the same problem.

Answer:

The tutorials seems to missing one important part. You have to add <meta-data android:name="android.app.default_searchable" android:value=".MySearchActivityName" /> inside <application> tags.

Answer:

Pau Arlandis Martinez, thank you. Your solution works fine and tutorial with activity stack is helpful. I put into <application> section this meta-data:

<meta-data
     android:name="android.app.default_searchable"
     android:value=".NameSearchActivity" />

And I noticed that @Override onQueryTextSubmit method should return false to start searchable activity. Otherwise searchButton on virtual keyboard doesn’t work.

Answer:

For anyone else who still has this issue, I also came across this issue, if the Searchable Activity extends AppCompatActivity the activity wont start, I changed it to extend Activity instead and it worked.

Answer:

You are missing meta-data in ‘.SearchBar’ Activity of Manifest file, which directs to ‘.AcceptSearch’ activity.

Add following meta data to ‘.SearchBar’ activity:

<!-- search widget target activity-->
        <meta-data android:name="android.app.default_searchable"
                    android:value=".AcceptSearch" />

Here is updated code for ‘.SearchBar’ activity manifest.

<activity android:name=".SearchBar"
              android:label="@string/app_name"> 
        <!-- search widget target activity-->
        <meta-data android:name="android.app.default_searchable"
                android:value=".AcceptSearch" />
        <intent-filter>
            <action android:name="com.example.searchlet.CLEARSCREEN" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

Answer:

I was having trouble with the SearchView not launching the SearchActivity, it turned out to be a package/directory issue.

If the SearchableActivity is not located in the same directory as the Activity hosting your SearchView, you need to change:

searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

to

searchView.setSearchableInfo(searchManager.getSearchableInfo(new ComponentName(this, SearchableActivity.class)));

Answer:

Hint and label in the searchable.xml are references, and not hardcoded strings.

 <searchable xmlns:android="http://schemas.android.com/apk/res/android"
      android:hint="@string/search_hint"
      android:label="@string/app_name"/>

This how the Activity which contains the SearchView widget should be declared in the manifest

<activity
    android:name="ACTIVITY PATH WHICH CONTAINS SEARCHVIEW WIDGET">

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

        <meta-data
            android:name="android.app.default_searchable"
            android:value=".SearchableActivity" />

</activity>

Declaration for searchable activity in the manifest.

 <activity android:name=".SearchableActivity">

        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>

        <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable" />

 </activity>