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