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 package com.example.android.supportv7.app; 17 18 import com.example.android.supportv7.R; 19 20 import android.graphics.drawable.Drawable; 21 import android.os.Bundle; 22 import android.support.v4.view.MenuItemCompat; 23 import android.support.v7.app.ActionBarActivity; 24 import android.support.v7.widget.SearchView; 25 import android.text.TextUtils; 26 import android.view.Menu; 27 import android.view.MenuInflater; 28 import android.view.MenuItem; 29 import android.widget.TextView; 30 import android.widget.Toast; 31 32 /** 33 * This demonstrates idiomatic usage of the Action Bar. The default Honeycomb theme 34 * includes the action bar by default and a menu resource is used to populate the 35 * menu data itself. If you'd like to see how these things work under the hood, see 36 * ActionBarMechanics. 37 */ 38 public class ActionBarUsage extends ActionBarActivity { 39 TextView mSearchText; 40 int mSortMode = -1; 41 42 @Override 43 protected void onCreate(Bundle savedInstanceState) { 44 super.onCreate(savedInstanceState); 45 mSearchText = new TextView(this); 46 setContentView(mSearchText); 47 } 48 49 @Override 50 public boolean onCreateOptionsMenu(Menu menu) { 51 MenuInflater inflater = getMenuInflater(); 52 inflater.inflate(R.menu.actions, menu); 53 SearchView searchView = (SearchView) MenuItemCompat 54 .getActionView(menu.findItem(R.id.action_search)); 55 searchView.setOnQueryTextListener(mOnQueryTextListener); 56 return true; 57 } 58 59 @Override 60 public boolean onPrepareOptionsMenu(Menu menu) { 61 if (mSortMode != -1) { 62 Drawable icon = menu.findItem(mSortMode).getIcon(); 63 menu.findItem(R.id.action_sort).setIcon(icon); 64 } 65 return super.onPrepareOptionsMenu(menu); 66 } 67 68 @Override 69 public boolean onOptionsItemSelected(MenuItem item) { 70 Toast.makeText(this, "Selected Item: " + item.getTitle(), Toast.LENGTH_SHORT).show(); 71 return true; 72 } 73 74 // This method is specified as an onClick handler in the menu xml and will 75 // take precedence over the Activity's onOptionsItemSelected method. 76 // See res/menu/actions.xml for more info. 77 public void onSort(MenuItem item) { 78 mSortMode = item.getItemId(); 79 // Request a call to onPrepareOptionsMenu so we can change the sort icon 80 supportInvalidateOptionsMenu(); 81 } 82 83 // The following callbacks are called for the SearchView.OnQueryChangeListener 84 // For more about using SearchView, see src/.../view/SearchView1.java and SearchView2.java 85 private final SearchView.OnQueryTextListener mOnQueryTextListener = 86 new SearchView.OnQueryTextListener() { 87 @Override 88 public boolean onQueryTextChange(String newText) { 89 newText = TextUtils.isEmpty(newText) ? "" : "Query so far: " + newText; 90 mSearchText.setText(newText); 91 return true; 92 } 93 94 @Override 95 public boolean onQueryTextSubmit(String query) { 96 Toast.makeText(ActionBarUsage.this, 97 "Searching for: " + query + "...", Toast.LENGTH_SHORT).show(); 98 return true; 99 } 100 }; 101 } 102