Home | History | Annotate | Download | only in searchview
      1 /*
      2  * Copyright (C) 2013 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.example.android.actionbarcompat.searchview;
     18 
     19 import android.os.Bundle;
     20 import android.support.v4.view.MenuItemCompat;
     21 import android.support.v7.app.AppCompatActivity;
     22 import android.support.v7.widget.SearchView;
     23 import android.view.Menu;
     24 import android.view.MenuItem;
     25 
     26 /**
     27  * This sample shows you how to use {@link SearchView} to provide a search function in a single
     28  * Activity, when utilizing ActionBarCompat.
     29  *
     30  * This Activity extends from {@link AppCompatActivity}, which provides all of the function
     31  * necessary to display a compatible Action Bar on devices running Android v2.1+.
     32  */
     33 public class SearchActivity extends AppCompatActivity {
     34 
     35     private AppListFragment mAppListFragment;
     36 
     37     @Override
     38     protected void onCreate(Bundle savedInstanceState) {
     39         super.onCreate(savedInstanceState);
     40         setContentView(R.layout.activity_main);
     41 
     42         // Retrieve the AppListFragment from the layout
     43         mAppListFragment = (AppListFragment) getSupportFragmentManager()
     44                 .findFragmentById(R.id.frag_app_list);
     45     }
     46 
     47     // BEGIN_INCLUDE(create_menu)
     48     /**
     49      * Use this method to instantiate your menu, inflating our {@code main} menu resource.
     50      */
     51     @Override
     52     public boolean onCreateOptionsMenu(Menu menu) {
     53         // Inflate our menu from the resources by using the menu inflater.
     54         getMenuInflater().inflate(R.menu.main, menu);
     55 
     56         return super.onCreateOptionsMenu(menu);
     57     }
     58     // END_INCLUDE(create_menu)
     59 
     60 
     61     /**
     62      * This is called just before the menu is about to be displayed. You should
     63      * use this method to modify your menu or its items. In this example, we
     64      * retrieve the SearchView.
     65      */
     66     @Override
     67     public boolean onPrepareOptionsMenu(Menu menu) {
     68         // BEGIN_INCLUDE(retrieve_view)
     69         // First we retrieve the searchable menu item
     70         MenuItem searchItem = menu.findItem(R.id.menu_search);
     71 
     72         // Now we get the SearchView from the item
     73         final SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
     74         // END_INCLUDE(retrieve_view)
     75 
     76         // Set the hint text which displays when the SearchView is empty
     77         searchView.setQueryHint(getString(R.string.search_hint));
     78 
     79         // BEGIN_INCLUDE(action_expand_listener)
     80         // We now set OnActionExpandListener on the menu item so we can reset SearchView's query
     81         // when it is collapsed.
     82         MenuItemCompat.setOnActionExpandListener(searchItem,
     83                 new MenuItemCompat.OnActionExpandListener() {
     84                     @Override
     85                     public boolean onMenuItemActionExpand(MenuItem menuItem) {
     86                         // Return true to allow the action view to expand
     87                         return true;
     88                     }
     89 
     90                     @Override
     91                     public boolean onMenuItemActionCollapse(MenuItem menuItem) {
     92                         // When the action view is collapsed, reset the query
     93                         searchView.setQuery(null, true);
     94 
     95                         // Return true to allow the action view to collapse
     96                         return true;
     97                     }
     98                 });
     99         // END_INCLUDE(action_expand_listener)
    100 
    101         // BEGIN_INCLUDE(query_text_listener)
    102         // We now set a OnQueryTextListener so we are notified when the query has changed
    103         searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
    104             @Override
    105             public boolean onQueryTextSubmit(String s) {
    106                 // Propagate query to our AppListFragment
    107                 mAppListFragment.setFilterQuery(s);
    108 
    109                 // Return true to signify that we have handled the query submit
    110                 return true;
    111             }
    112 
    113             @Override
    114             public boolean onQueryTextChange(String s) {
    115                 // Propagate query to our AppListFragment
    116                 mAppListFragment.setFilterQuery(s);
    117 
    118                 // Return true to signify that we have handled the query change
    119                 return true;
    120             }
    121         });
    122         // END_INCLUDE(query_text_listener)
    123 
    124         return super.onPrepareOptionsMenu(menu);
    125     }
    126 }
    127