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