Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I'm having trouble getting the Android Search Interface to work in the Action Bar

I tried following the guide here, but I'm having some problems. I correctly got the search icon to appear in the action bar, but I'm having trouble attaching the searchable configuration to the SearchView.

My res/menu/options_menu.xml file:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/search"
          android:title="@string/search_title"
          android:icon="@drawable/search"
          android:showAsAction="collapseActionView|ifRoom"
          android:actionViewClass="android.widget.SearchView" />
</menu>

My res/xml/searchable.xml file (Note: I had to create an "xml" folder in the res folder because one did not exist. I don't think this should make a difference though?

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

My res/values/strings.xml file:

<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="app_name">Starting To Feel It</string>
<string name="single_post">Single Post View</string>
<string name="search">Search Activity</string>

<string name="action_settings">Settings</string>
<string name = "pagination_last">Last</string>
<string name = "pagination_next">Next</string>

<string name = "player_play">Play</string>
<string name = "player_previous">Previous</string>
<string name = "player_next">Next</string>
<string name = "player_playlist">Playlist</string>
<string name = "player_progress_bar">Progress Bar</string>

<string name="drawer_open">Open navigation drawer</string>
<string name="drawer_close">Close navigation drawer</string>


<string name="search_title">Search STFI</string>
<string name="search_hint">Search STFI</string>


<string name="menu_item_picture">Menu Item Picture</string>

</resources>

My AndroidManifest.xml file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.stfi"
android:versionCode="1"
android:versionName="1.0" >

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:largeHeap="true"
    android:theme="@android:style/Theme.Holo.Light" >


    <activity
        android:name=".StartingToFeelIt"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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


    <activity
        android:name=".activities.SinglePost"
        android:label="@string/single_post">

    </activity>

    <activity 
        android:name=".activities.SearchActivity" 
        android:label="@string/search">

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

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


</application>

</manifest>

All of my activities extend a class I called MenuActivity, and here is the onCreateOptionsMenu function define in MenuActivity.java:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.options_menu, menu);

    // Associate searchable configuration with the SearchView
    SearchManager searchManager =
            (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView =
            (SearchView) menu.findItem(R.id.search).getActionView();

    System.out.println("The component name is " + this.getComponentName().toString());

    SearchableInfo info = searchManager.getSearchableInfo(getComponentName());

    if(info  == null)
    {
        System.out.println("It is null!");
    }

    searchView.setSearchableInfo(info);

    return true;
}

I'm always getting that info is null in this function. I'm not sure what I'm doing wrong to get info is null, and I'm not sure how to fix it. I'm not sure if you need it or not, but below is my SearchActivity class. Right now it doesn't do anything but try to display the search (however, it is never called because the search configuration is not set up correctly). The file is saved in com.stfi.activities.SearchActivity.java.

Another note: When I print out the component name in the function above, I get The component name is ComponentInfo(com.stfi/com.stfi.StartingToFeelIt) and not just com.stfi.StartingToFeelIt. This leads me to believe something is wrong with my AndroidManifest file, but I have no idea if this is the cause as to why I can't attach the search configuration.

public class SearchActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    handleIntent(getIntent());
}

@Override
protected void onNewIntent(Intent intent) {
    handleIntent(intent);
}

private void handleIntent(Intent intent) {

    if (Intent.ACTION_SEARCH.equals(intent.getAction()))
    {
        String query = intent.getStringExtra(SearchManager.QUERY);
        System.out.println("You are searchin " + query);
    }
}
}
like image 798
jas7457 Avatar asked Jan 10 '14 23:01

jas7457


People also ask

How to implement search feature in Android?

Invoking the search dialog For instance, you should add a Search button in your Options Menu or UI layout that calls onSearchRequested() . For consistency with the Android system and other apps, you should label your button with the Android Search icon that's available from the Action Bar Icon Pack.

What is the use of action bar in Android?

The app bar, also known as the action bar, is one of the most important design elements in your app's activities, because it provides a visual structure and interactive elements that are familiar to users.

How do I create a search fragment?

If you want to have a fragment specific search, have all your Fragments extend an interface MyFragment with a startSearch method, and have your Activity 's startSearch method call the current fragment's startSearch method.


1 Answers

I have beeen poking around and have found two things you need to do.

1. Update your Manifest.xml and use <meta to add your Search Interface.

2. In order for it to have searchable configuration you need to add meta data

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

</activity>
like image 69
nathanleachman Avatar answered Oct 11 '22 14:10

nathanleachman