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.os.Bundle; 23 import android.text.TextUtils; 24 import android.view.Window; 25 import android.widget.ArrayAdapter; 26 import android.widget.ListView; 27 import android.widget.SearchView; 28 29 30 /** 31 * Shows a list that can be filtered in-place with a SearchView in non-iconified mode. 32 */ 33 public class SearchViewFilterMode extends Activity implements SearchView.OnQueryTextListener { 34 35 private static final String TAG = "SearchViewFilterMode"; 36 37 private SearchView mSearchView; 38 private ListView mListView; 39 private ArrayAdapter<String> mAdapter; 40 41 private final String[] mStrings = Cheeses.sCheeseStrings; 42 43 @Override 44 protected void onCreate(Bundle savedInstanceState) { 45 super.onCreate(savedInstanceState); 46 getWindow().requestFeature(Window.FEATURE_ACTION_BAR); 47 48 setContentView(R.layout.searchview_filter); 49 50 mSearchView = (SearchView) findViewById(R.id.search_view); 51 mListView = (ListView) findViewById(R.id.list_view); 52 mListView.setAdapter(mAdapter = new ArrayAdapter<String>(this, 53 android.R.layout.simple_list_item_1, 54 mStrings)); 55 mListView.setTextFilterEnabled(true); 56 setupSearchView(); 57 } 58 59 private void setupSearchView() { 60 mSearchView.setIconifiedByDefault(false); 61 mSearchView.setOnQueryTextListener(this); 62 mSearchView.setSubmitButtonEnabled(false); 63 mSearchView.setQueryHint(getString(R.string.cheese_hunt_hint)); 64 } 65 66 public boolean onQueryTextChange(String newText) { 67 if (TextUtils.isEmpty(newText)) { 68 mListView.clearTextFilter(); 69 } else { 70 mListView.setFilterText(newText.toString()); 71 } 72 return true; 73 } 74 75 public boolean onQueryTextSubmit(String query) { 76 return false; 77 } 78 } 79