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.apis.app;
17 
18 import android.app.Activity;
19 import android.graphics.drawable.Drawable;
20 import android.os.Bundle;
21 import android.view.Menu;
22 import android.view.MenuInflater;
23 import android.view.MenuItem;
24 import android.widget.SearchView;
25 import android.widget.SearchView.OnQueryTextListener;
26 import android.widget.TextView;
27 import android.widget.Toast;
28 
29 import com.example.android.apis.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 Activity implements OnQueryTextListener {
38     TextView mSearchText;
39     int mSortMode = -1;
40 
41     @Override
onCreate(Bundle savedInstanceState)42     protected void onCreate(Bundle savedInstanceState) {
43         super.onCreate(savedInstanceState);
44         mSearchText = new TextView(this);
45         setContentView(mSearchText);
46     }
47 
48     @Override
onCreateOptionsMenu(Menu menu)49     public boolean onCreateOptionsMenu(Menu menu) {
50         MenuInflater inflater = getMenuInflater();
51         inflater.inflate(R.menu.actions, menu);
52         SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
53         searchView.setOnQueryTextListener(this);
54         return true;
55     }
56 
57     @Override
onPrepareOptionsMenu(Menu menu)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
onOptionsItemSelected(MenuItem item)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.
onSort(MenuItem item)75     public void onSort(MenuItem item) {
76         mSortMode = item.getItemId();
77         // Request a call to onPrepareOptionsMenu so we can change the sort icon
78         invalidateOptionsMenu();
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
onQueryTextChange(String newText)83     public boolean onQueryTextChange(String newText) {
84         newText = newText.isEmpty() ? "" : "Query so far: " + newText;
85         mSearchText.setText(newText);
86         return true;
87     }
88 
onQueryTextSubmit(String query)89     public boolean onQueryTextSubmit(String query) {
90         Toast.makeText(this, "Searching for: " + query + "...", Toast.LENGTH_SHORT).show();
91         return true;
92     }
93 }
94