1 /* 2 * Copyright (C) 2010 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.apis.view; 18 19 import com.example.android.apis.R; 20 21 import android.app.Activity; 22 import android.app.SearchManager; 23 import android.app.SearchableInfo; 24 import android.content.Context; 25 import android.os.Bundle; 26 import android.view.Menu; 27 import android.view.MenuInflater; 28 import android.view.MenuItem; 29 import android.view.View; 30 import android.view.Window; 31 import android.view.MenuItem.OnActionExpandListener; 32 import android.widget.Button; 33 import android.widget.SearchView; 34 import android.widget.TextView; 35 36 import java.util.List; 37 38 /** 39 * This demonstrates the usage of SearchView in an ActionBar as a menu item. 40 * It sets a SearchableInfo on the SearchView for suggestions and submitting queries to. 41 */ 42 public class SearchViewActionBar extends Activity implements SearchView.OnQueryTextListener { 43 44 private SearchView mSearchView; 45 private TextView mStatusView; 46 47 @Override onCreate(Bundle savedInstanceState)48 protected void onCreate(Bundle savedInstanceState) { 49 super.onCreate(savedInstanceState); 50 getWindow().requestFeature(Window.FEATURE_ACTION_BAR); 51 52 setContentView(R.layout.searchview_actionbar); 53 54 mStatusView = (TextView) findViewById(R.id.status_text); 55 } 56 57 @Override onCreateOptionsMenu(Menu menu)58 public boolean onCreateOptionsMenu(Menu menu) { 59 super.onCreateOptionsMenu(menu); 60 61 MenuInflater inflater = getMenuInflater(); 62 inflater.inflate(R.menu.searchview_in_menu, menu); 63 MenuItem searchItem = menu.findItem(R.id.action_search); 64 mSearchView = (SearchView) searchItem.getActionView(); 65 setupSearchView(searchItem); 66 67 return true; 68 } 69 setupSearchView(MenuItem searchItem)70 private void setupSearchView(MenuItem searchItem) { 71 72 if (isAlwaysExpanded()) { 73 mSearchView.setIconifiedByDefault(false); 74 } else { 75 searchItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_IF_ROOM 76 | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW); 77 } 78 79 SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); 80 if (searchManager != null) { 81 List<SearchableInfo> searchables = searchManager.getSearchablesInGlobalSearch(); 82 83 // Try to use the "applications" global search provider 84 SearchableInfo info = searchManager.getSearchableInfo(getComponentName()); 85 for (SearchableInfo inf : searchables) { 86 if (inf.getSuggestAuthority() != null 87 && inf.getSuggestAuthority().startsWith("applications")) { 88 info = inf; 89 } 90 } 91 mSearchView.setSearchableInfo(info); 92 } 93 94 mSearchView.setOnQueryTextListener(this); 95 } 96 onQueryTextChange(String newText)97 public boolean onQueryTextChange(String newText) { 98 mStatusView.setText("Query = " + newText); 99 return false; 100 } 101 onQueryTextSubmit(String query)102 public boolean onQueryTextSubmit(String query) { 103 mStatusView.setText("Query = " + query + " : submitted"); 104 return false; 105 } 106 onClose()107 public boolean onClose() { 108 mStatusView.setText("Closed!"); 109 return false; 110 } 111 isAlwaysExpanded()112 protected boolean isAlwaysExpanded() { 113 return false; 114 } 115 } 116