Add ActionBarCompat SearchView Sample

Change-Id: I63b049e1fa584c20638fe935df42e64a63784bd1
Signed-off-by: Chris Banes <chrisbanes@google.com>

(cherry picked from commit de75431d76a2374621e5122bec3c423b116625b9)
diff --git a/ui/actionbarcompat/SearchView/big_icon.png b/ui/actionbarcompat/SearchView/big_icon.png
new file mode 100644
index 0000000..e4c6ee2
--- /dev/null
+++ b/ui/actionbarcompat/SearchView/big_icon.png
Binary files differ
diff --git a/ui/actionbarcompat/SearchView/build.gradle b/ui/actionbarcompat/SearchView/build.gradle
new file mode 100644
index 0000000..b99b102
--- /dev/null
+++ b/ui/actionbarcompat/SearchView/build.gradle
@@ -0,0 +1,16 @@
+apply plugin: 'android'
+
+dependencies {
+    compile "com.android.support:support-v4:18.0.+"
+    compile "com.android.support:appcompat-v7:18.0.+"
+}
+
+android {
+    compileSdkVersion 17
+    buildToolsVersion "17.0.0"
+
+    defaultConfig {
+        minSdkVersion 7
+        targetSdkVersion 16
+    }
+}
diff --git a/ui/actionbarcompat/SearchView/src/main/AndroidManifest.xml b/ui/actionbarcompat/SearchView/src/main/AndroidManifest.xml
new file mode 100644
index 0000000..b2554d6
--- /dev/null
+++ b/ui/actionbarcompat/SearchView/src/main/AndroidManifest.xml
@@ -0,0 +1,47 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  Copyright 2013 The Android Open Source Project
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+-->
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+    package="com.example.android.actionbarcompat.searchview"
+    android:versionCode="1"
+    android:versionName="1.0">
+
+    <!--
+        ActionBarCompat provides an Action Bar with compatible SearchView
+        from API v7 onwards
+    -->
+    <uses-sdk
+        android:minSdkVersion="7"
+        android:targetSdkVersion="17" />
+
+    <application
+        android:icon="@drawable/ic_launcher"
+        android:label="@string/app_name"
+        android:theme="@style/AppTheme"
+        android:allowBackup="true">
+
+        <activity
+            android:name=".SearchActivity">
+            <!-- Launcher Intent filter -->
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="android.intent.category.LAUNCHER" />
+            </intent-filter>
+        </activity>
+
+    </application>
+
+</manifest>
diff --git a/ui/actionbarcompat/SearchView/src/main/res/drawable-hdpi/ic_action_search.png b/ui/actionbarcompat/SearchView/src/main/res/drawable-hdpi/ic_action_search.png
new file mode 100644
index 0000000..9839a13
--- /dev/null
+++ b/ui/actionbarcompat/SearchView/src/main/res/drawable-hdpi/ic_action_search.png
Binary files differ
diff --git a/ui/actionbarcompat/SearchView/src/main/res/drawable-hdpi/ic_launcher.png b/ui/actionbarcompat/SearchView/src/main/res/drawable-hdpi/ic_launcher.png
new file mode 100644
index 0000000..45afb7d
--- /dev/null
+++ b/ui/actionbarcompat/SearchView/src/main/res/drawable-hdpi/ic_launcher.png
Binary files differ
diff --git a/ui/actionbarcompat/SearchView/src/main/res/drawable-mdpi/ic_action_search.png b/ui/actionbarcompat/SearchView/src/main/res/drawable-mdpi/ic_action_search.png
new file mode 100644
index 0000000..076085c
--- /dev/null
+++ b/ui/actionbarcompat/SearchView/src/main/res/drawable-mdpi/ic_action_search.png
Binary files differ
diff --git a/ui/actionbarcompat/SearchView/src/main/res/drawable-mdpi/ic_launcher.png b/ui/actionbarcompat/SearchView/src/main/res/drawable-mdpi/ic_launcher.png
new file mode 100644
index 0000000..57f2527
--- /dev/null
+++ b/ui/actionbarcompat/SearchView/src/main/res/drawable-mdpi/ic_launcher.png
Binary files differ
diff --git a/ui/actionbarcompat/SearchView/src/main/res/drawable-xhdpi/ic_action_search.png b/ui/actionbarcompat/SearchView/src/main/res/drawable-xhdpi/ic_action_search.png
new file mode 100644
index 0000000..f9272df
--- /dev/null
+++ b/ui/actionbarcompat/SearchView/src/main/res/drawable-xhdpi/ic_action_search.png
Binary files differ
diff --git a/ui/actionbarcompat/SearchView/src/main/res/drawable-xhdpi/ic_launcher.png b/ui/actionbarcompat/SearchView/src/main/res/drawable-xhdpi/ic_launcher.png
new file mode 100644
index 0000000..478f739
--- /dev/null
+++ b/ui/actionbarcompat/SearchView/src/main/res/drawable-xhdpi/ic_launcher.png
Binary files differ
diff --git a/ui/actionbarcompat/SearchView/src/main/res/drawable-xxhdpi/ic_launcher.png b/ui/actionbarcompat/SearchView/src/main/res/drawable-xxhdpi/ic_launcher.png
new file mode 100644
index 0000000..2e14315
--- /dev/null
+++ b/ui/actionbarcompat/SearchView/src/main/res/drawable-xxhdpi/ic_launcher.png
Binary files differ
diff --git a/ui/actionbarcompat/SearchView/src/main/res/layout/activity_main.xml b/ui/actionbarcompat/SearchView/src/main/res/layout/activity_main.xml
new file mode 100644
index 0000000..9158690
--- /dev/null
+++ b/ui/actionbarcompat/SearchView/src/main/res/layout/activity_main.xml
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  Copyright 2013 The Android Open Source Project
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+-->
+<fragment xmlns:android="http://schemas.android.com/apk/res/android"
+    android:id="@+id/frag_app_list"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    android:name="com.example.android.actionbarcompat.searchview.AppListFragment" />
diff --git a/ui/actionbarcompat/SearchView/src/main/res/layout/list_item.xml b/ui/actionbarcompat/SearchView/src/main/res/layout/list_item.xml
new file mode 100644
index 0000000..893796c
--- /dev/null
+++ b/ui/actionbarcompat/SearchView/src/main/res/layout/list_item.xml
@@ -0,0 +1,42 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  Copyright 2013 The Android Open Source Project
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+-->
+
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:orientation="horizontal"
+    android:layout_width="match_parent"
+    android:layout_height="?android:listPreferredItemHeight"
+    android:padding="8dp"
+    android:gravity="center_vertical">
+
+    <ImageView
+        android:id="@+id/icon"
+        android:layout_width="wrap_content"
+        android:layout_height="match_parent"
+        android:layout_marginRight="8dp"
+        android:scaleType="fitCenter"
+        android:contentDescription="@string/content_item_icon"/>
+
+    <TextView
+        android:id="@android:id/text1"
+        android:layout_width="0dp"
+        android:layout_weight="1"
+        android:layout_height="wrap_content"
+        android:textAppearance="?android:textAppearanceMedium"
+        android:maxLines="1"
+        android:ellipsize="end"/>
+
+</LinearLayout>
\ No newline at end of file
diff --git a/ui/actionbarcompat/SearchView/src/main/res/menu/main.xml b/ui/actionbarcompat/SearchView/src/main/res/menu/main.xml
new file mode 100644
index 0000000..1709e8b
--- /dev/null
+++ b/ui/actionbarcompat/SearchView/src/main/res/menu/main.xml
@@ -0,0 +1,40 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  Copyright 2013 The Android Open Source Project
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+-->
+
+<!--
+  As we're using ActionBarCompat, any action item attributes come from ActionBarCompat's XML
+  namespace instead of the android namespace. Here we've added a new support namespace added to
+  the menu element allowing us to use the 'showAsAction' attribute in a backwards compatible way.
+  Any other action item attributes used should be referenced from this namespace too
+  (actionProviderClass, actionViewClass, actionLayout).
+-->
+<menu xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:support="http://schemas.android.com/apk/res-auto">
+
+    <!--
+        Here we create an item, setting the actionViewClass attribute to point to ActionBarCompat's
+        SearchView. We add the collapseActionView flag to showAsAction so that the SearchView is
+        collapsed until the user clicks on the action item.
+    -->
+    <item
+        android:id="@+id/menu_search"
+        android:title="@string/menu_search"
+        android:icon="@drawable/ic_action_search"
+        support:actionViewClass="android.support.v7.widget.SearchView"
+        support:showAsAction="ifRoom|collapseActionView"/>
+
+</menu>
\ No newline at end of file
diff --git a/ui/actionbarcompat/SearchView/src/main/res/values/strings.xml b/ui/actionbarcompat/SearchView/src/main/res/values/strings.xml
new file mode 100644
index 0000000..4a5ab36
--- /dev/null
+++ b/ui/actionbarcompat/SearchView/src/main/res/values/strings.xml
@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  Copyright 2013 The Android Open Source Project
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+-->
+<resources>
+
+    <string name="app_name">ABC Search Demo</string>
+    <string name="applist_empty_text">Search for an app above.</string>
+    <string name="search_hint">Search applications</string>
+    <string name="menu_search">Search</string>
+    <string name="content_item_icon">Item Icon</string>
+
+</resources>
\ No newline at end of file
diff --git a/ui/actionbarcompat/SearchView/src/main/res/values/styles.xml b/ui/actionbarcompat/SearchView/src/main/res/values/styles.xml
new file mode 100644
index 0000000..d9a47e3
--- /dev/null
+++ b/ui/actionbarcompat/SearchView/src/main/res/values/styles.xml
@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  Copyright 2013 The Android Open Source Project
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+-->
+<resources>
+
+    <!-- Application theme. -->
+    <style name="AppTheme" parent="Theme.AppCompat"></style>
+
+</resources>
\ No newline at end of file
diff --git a/ui/actionbarcompat/SearchView/src/main/src/com/example/android/actionbarcompat/searchview/AppListFragment.java b/ui/actionbarcompat/SearchView/src/main/src/com/example/android/actionbarcompat/searchview/AppListFragment.java
new file mode 100644
index 0000000..981eb37
--- /dev/null
+++ b/ui/actionbarcompat/SearchView/src/main/src/com/example/android/actionbarcompat/searchview/AppListFragment.java
@@ -0,0 +1,256 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.android.actionbarcompat.searchview;
+
+import android.content.pm.ApplicationInfo;
+import android.content.pm.PackageManager;
+import android.graphics.drawable.Drawable;
+import android.os.AsyncTask;
+import android.os.Bundle;
+import android.support.v4.app.ListFragment;
+import android.text.TextUtils;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.BaseAdapter;
+import android.widget.ImageView;
+import android.widget.TextView;
+
+import java.lang.ref.WeakReference;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.regex.Pattern;
+
+/**
+ * A ListFragment which displays a list of applications currently on the device, with
+ * filtering functionality.
+ */
+public class AppListFragment extends ListFragment {
+
+    // Stores the full list of applications installed on the device
+    private final List<ApplicationItem> mInstalledApps = new ArrayList<ApplicationItem>();
+
+    // Stores the result of the last query
+    private final  List<ApplicationItem> mFilteredApps = new ArrayList<ApplicationItem>();
+
+    private PackageManager mPackageManager;
+
+    @Override
+    public void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+
+        // Retrieve all of the applications installed on this device
+        mPackageManager = getActivity().getPackageManager();
+
+        // Start an AsyncTask to load the application's installed on the device
+        new LoadApplicationsTask().execute();
+    }
+
+    @Override
+    public void onViewCreated(View view, Bundle savedInstanceState) {
+        super.onViewCreated(view, savedInstanceState);
+
+        // Set the ListView's Adapter to display the filtered applications
+        setListAdapter(new AppAdapter());
+
+        // Set the text which displays when the ListView is empty
+        setEmptyText(getString(R.string.applist_empty_text));
+
+        // If the installed applications is not loaded yet, hide the list
+        if (mInstalledApps.isEmpty()) {
+            setListShown(false);
+        }
+    }
+
+    /**
+     * Set the query used to filter the installed application's names.
+     * @param query Query to filter against.
+     */
+    public void setFilterQuery(String query) {
+        // Fail-fast if the installed apps has not been loaded yet
+        if (mInstalledApps.isEmpty()) {
+            return;
+        }
+
+        // Clear current filtered apps and hide ListView
+        mFilteredApps.clear();
+
+        if (TextUtils.isEmpty(query)) {
+            // If the query is empty, show all install apps
+            mFilteredApps.addAll(mInstalledApps);
+        } else {
+            // Compile Regex Pattern which will match if the app name contains the query
+            final Pattern p = Pattern.compile(".*" + query + ".*", Pattern.CASE_INSENSITIVE);
+
+            // Iterate through the installed apps to see if their label matches the query
+            for (ApplicationItem application : mInstalledApps) {
+                // If the app label matches the query, add it to the filtered apps list
+                if (p.matcher(application.label).matches()) {
+                    mFilteredApps.add(application);
+                }
+            }
+        }
+
+        // Notify the adapter so that it updates the ListView's contents
+        AppAdapter adapter = (AppAdapter) getListAdapter();
+        adapter.notifyDataSetChanged();
+    }
+
+    private class AppAdapter extends BaseAdapter {
+        private final LayoutInflater mInflater;
+
+        AppAdapter() {
+            mInflater = LayoutInflater.from(getActivity());
+        }
+
+        @Override
+        public int getCount() {
+            return mFilteredApps.size();
+        }
+
+        @Override
+        public ApplicationItem getItem(int position) {
+            return mFilteredApps.get(position);
+        }
+
+        @Override
+        public long getItemId(int position) {
+            return position;
+        }
+
+        @Override
+        public View getView(int position, View convertView, ViewGroup parent) {
+            if (convertView == null) {
+                convertView = mInflater.inflate(R.layout.list_item, parent, false);
+            }
+
+            final ApplicationItem item = getItem(position);
+
+            // Set the application's label on the TextView
+            ((TextView) convertView.findViewById(android.R.id.text1))
+                    .setText(item.label);
+
+            // ImageView to display the application's icon
+            ImageView imageView = (ImageView) convertView.findViewById(R.id.icon);
+
+            // Check the item's drawable reference to see if it is available already
+            Drawable icon = item.drawable != null ? item.drawable.get() : null;
+
+            if (icon != null) {
+                imageView.setImageDrawable(icon);
+            } else {
+                // Start a new AsyncTask which will retrieve the application icon and display it in
+                // the ImageView
+                new ApplicationIconTask(imageView).execute(item);
+            }
+
+            return convertView;
+        }
+    }
+
+    /**
+     * Our model object for each application item. Allows us to load the label async and store the
+     * result for use later (filtering and displaying in the adapter).
+     */
+    private static class ApplicationItem {
+        final ApplicationInfo applicationInfo;
+        final CharSequence label;
+        WeakReference<Drawable> drawable;
+
+        ApplicationItem(PackageManager packageManager, ApplicationInfo applicationInfo) {
+            this.applicationInfo = applicationInfo;
+
+            // Load and store the app's label using the PackageManager
+            this.label = applicationInfo.loadLabel(packageManager);
+        }
+    }
+
+    /**
+     * Simple AsyncTask which retrieves the installed applications from the {@link PackageManager}
+     * and stores them locally.
+     */
+    private class LoadApplicationsTask extends AsyncTask<Void, Void, List<ApplicationItem>> {
+
+        @Override
+        protected List<ApplicationItem> doInBackground(Void... voids) {
+            // Load all installed applications on the device
+            List<ApplicationInfo> apps = mPackageManager.getInstalledApplications(0);
+            // Create an ArrayList used to store the result
+            ArrayList<ApplicationItem> loadedApps = new ArrayList<ApplicationItem>();
+
+            // Iterate through the results and create an ApplicationItem instance for each one,
+            // adding them to the returned List.
+            for (ApplicationInfo info : apps) {
+                loadedApps.add(new ApplicationItem(mPackageManager, info));
+            }
+
+            return loadedApps;
+        }
+
+        @Override
+        protected void onPostExecute(List<ApplicationItem> loadedApps) {
+            super.onPostExecute(loadedApps);
+
+            // Add the results to the install apps list
+            mInstalledApps.addAll(loadedApps);
+
+            // Reset the query to update the ListView
+            setFilterQuery(null);
+
+            // Make sure the list is shown
+            setListShown(true);
+        }
+    }
+
+    /**
+     * Simple AsyncTask which loads the given application's logo from the {@link PackageManager} and
+     * displays it in the given {@link ImageView}
+     */
+    private class ApplicationIconTask extends AsyncTask<ApplicationItem, Void, Drawable> {
+        private final ImageView mImageView;
+
+        ApplicationIconTask(ImageView imageView) {
+            mImageView = imageView;
+        }
+
+        @Override
+        protected Drawable doInBackground(ApplicationItem... params) {
+            Drawable icon = null;
+
+            // Check to see that we've been provided one item
+            if (params.length == 1) {
+                ApplicationItem item = params[0];
+
+                // Load the icon from the PackageManager
+                icon = item.applicationInfo.loadIcon(mPackageManager);
+
+                // Store the icon in a WeakReference so we do not cause a OutOfMemoryError.
+                item.drawable = new WeakReference<Drawable>(icon);
+            }
+
+            return icon;
+        }
+
+        @Override
+        protected void onPostExecute(Drawable icon) {
+            super.onPostExecute(icon);
+            // Display the icon in the ImageView
+            mImageView.setImageDrawable(icon);
+        }
+    }
+
+}
diff --git a/ui/actionbarcompat/SearchView/src/main/src/com/example/android/actionbarcompat/searchview/SearchActivity.java b/ui/actionbarcompat/SearchView/src/main/src/com/example/android/actionbarcompat/searchview/SearchActivity.java
new file mode 100644
index 0000000..c0cbdda
--- /dev/null
+++ b/ui/actionbarcompat/SearchView/src/main/src/com/example/android/actionbarcompat/searchview/SearchActivity.java
@@ -0,0 +1,126 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.android.actionbarcompat.searchview;
+
+import android.os.Bundle;
+import android.support.v4.view.MenuItemCompat;
+import android.support.v7.app.ActionBarActivity;
+import android.support.v7.widget.SearchView;
+import android.view.Menu;
+import android.view.MenuItem;
+
+/**
+ * This sample shows you how to use {@link SearchView} to provide a search function in a single
+ * Activity, when utilizing ActionBarCompat.
+ *
+ * This Activity extends from {@link ActionBarActivity}, which provides all of the function
+ * necessary to display a compatible Action Bar on devices running Android v2.1+.
+ */
+public class SearchActivity extends ActionBarActivity {
+
+    private AppListFragment mAppListFragment;
+
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        setContentView(R.layout.activity_main);
+
+        // Retrieve the AppListFragment from the layout
+        mAppListFragment = (AppListFragment) getSupportFragmentManager()
+                .findFragmentById(R.id.frag_app_list);
+    }
+
+    // BEGIN_INCLUDE(create_menu)
+    /**
+     * Use this method to instantiate your menu, inflating our {@code main} menu resource.
+     */
+    @Override
+    public boolean onCreateOptionsMenu(Menu menu) {
+        // Inflate our menu from the resources by using the menu inflater.
+        getMenuInflater().inflate(R.menu.main, menu);
+
+        return super.onCreateOptionsMenu(menu);
+    }
+    // END_INCLUDE(create_menu)
+
+
+    /**
+     * This is called just before the menu is about to be displayed. You should
+     * use this method to modify your menu or its items. In this example, we
+     * retrieve the SearchView.
+     */
+    @Override
+    public boolean onPrepareOptionsMenu(Menu menu) {
+        // BEGIN_INCLUDE(retrieve_view)
+        // First we retrieve the searchable menu item
+        MenuItem searchItem = menu.findItem(R.id.menu_search);
+
+        // Now we get the SearchView from the item
+        final SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
+        // END_INCLUDE(retrieve_view)
+
+        // Set the hint text which displays when the SearchView is empty
+        searchView.setQueryHint(getString(R.string.search_hint));
+
+        // BEGIN_INCLUDE(action_expand_listener)
+        // We now set OnActionExpandListener on the menu item so we can reset SearchView's query
+        // when it is collapsed.
+        MenuItemCompat.setOnActionExpandListener(searchItem,
+                new MenuItemCompat.OnActionExpandListener() {
+                    @Override
+                    public boolean onMenuItemActionExpand(MenuItem menuItem) {
+                        // Return true to allow the action view to expand
+                        return true;
+                    }
+
+                    @Override
+                    public boolean onMenuItemActionCollapse(MenuItem menuItem) {
+                        // When the action view is collapsed, reset the query
+                        searchView.setQuery(null, true);
+
+                        // Return true to allow the action view to collapse
+                        return true;
+                    }
+                });
+        // END_INCLUDE(action_expand_listener)
+
+        // BEGIN_INCLUDE(query_text_listener)
+        // We now set a OnQueryTextListener so we are notified when the query has changed
+        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
+            @Override
+            public boolean onQueryTextSubmit(String s) {
+                // Propagate query to our AppListFragment
+                mAppListFragment.setFilterQuery(s);
+
+                // Return true to signify that we have handled the query submit
+                return true;
+            }
+
+            @Override
+            public boolean onQueryTextChange(String s) {
+                // Propagate query to our AppListFragment
+                mAppListFragment.setFilterQuery(s);
+
+                // Return true to signify that we have handled the query change
+                return true;
+            }
+        });
+        // END_INCLUDE(query_text_listener)
+
+        return super.onPrepareOptionsMenu(menu);
+    }
+}