Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search bar widget not starting searchable activity

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.

like image 947
myselfesteem Avatar asked Aug 08 '12 07:08

myselfesteem


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

like image 65
Pau Arlandis Martinez Avatar answered Oct 12 '22 22:10

Pau Arlandis Martinez


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)));
like image 22
Tim Malseed Avatar answered Oct 12 '22 23:10

Tim Malseed