Home | History | Annotate | Download | only in app
      1 /*
      2  * Copyright (C) 2007 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.app;
     18 
     19 import com.example.android.apis.R;
     20 
     21 import android.app.Activity;
     22 import android.os.Bundle;
     23 import android.view.Menu;
     24 import android.view.MenuInflater;
     25 import android.view.MenuItem;
     26 import android.view.View;
     27 import android.widget.AdapterView;
     28 import android.widget.ArrayAdapter;
     29 import android.widget.LinearLayout;
     30 import android.widget.Spinner;
     31 import android.widget.TextView;
     32 import android.widget.Toast;
     33 
     34 /**
     35  * Demonstrates inflating menus from XML. There are different menu XML resources
     36  * that the user can choose to inflate. First, select an example resource from
     37  * the spinner, and then hit the menu button. To choose another, back out of the
     38  * activity and start over.
     39  */
     40 public class MenuInflateFromXml extends Activity {
     41     /**
     42      * Different example menu resources.
     43      */
     44     private static final int sMenuExampleResources[] = {
     45         R.menu.title_only, R.menu.title_icon, R.menu.submenu, R.menu.groups,
     46         R.menu.checkable, R.menu.shortcuts, R.menu.order, R.menu.category_order,
     47         R.menu.visible, R.menu.disabled
     48     };
     49 
     50     /**
     51      * Names corresponding to the different example menu resources.
     52      */
     53     private static final String sMenuExampleNames[] = {
     54         "Title only", "Title and Icon", "Submenu", "Groups",
     55         "Checkable", "Shortcuts", "Order", "Category and Order",
     56         "Visible", "Disabled"
     57     };
     58 
     59     /**
     60      * Lets the user choose a menu resource.
     61      */
     62     private Spinner mSpinner;
     63 
     64     /**
     65      * Shown as instructions.
     66      */
     67     private TextView mInstructionsText;
     68 
     69     /**
     70      * Safe to hold on to this.
     71      */
     72     private Menu mMenu;
     73 
     74     @Override
     75     protected void onCreate(Bundle savedInstanceState) {
     76         super.onCreate(savedInstanceState);
     77 
     78         // Create a simple layout
     79         LinearLayout layout = new LinearLayout(this);
     80         layout.setOrientation(LinearLayout.VERTICAL);
     81 
     82         // Create the spinner to allow the user to choose a menu XML
     83         ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
     84                 android.R.layout.simple_spinner_item, sMenuExampleNames);
     85         adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
     86         mSpinner = new Spinner(this);
     87         // When programmatically creating views, make sure to set an ID
     88         // so it will automatically save its instance state
     89         mSpinner.setId(R.id.spinner);
     90         mSpinner.setAdapter(adapter);
     91         mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
     92             @Override
     93             public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
     94                 invalidateOptionsMenu();
     95             }
     96             @Override
     97             public void onNothingSelected(AdapterView<?> parent) {
     98             }
     99         });
    100 
    101         // Add the spinner
    102         layout.addView(mSpinner,
    103                 new LinearLayout.LayoutParams(
    104                         LinearLayout.LayoutParams.MATCH_PARENT,
    105                         LinearLayout.LayoutParams.WRAP_CONTENT));
    106 
    107         // Create help text
    108         mInstructionsText = new TextView(this);
    109         mInstructionsText.setText(getResources().getString(
    110                 R.string.menu_from_xml_instructions_press_menu));
    111 
    112         // Add the help, make it look decent
    113         LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
    114                 LinearLayout.LayoutParams.MATCH_PARENT,
    115                 LinearLayout.LayoutParams.WRAP_CONTENT);
    116         lp.setMargins(10, 10, 10, 10);
    117         layout.addView(mInstructionsText, lp);
    118 
    119         // Set the layout as our content view
    120         setContentView(layout);
    121     }
    122 
    123     @Override
    124     public boolean onCreateOptionsMenu(Menu menu) {
    125         // Hold on to this
    126         mMenu = menu;
    127 
    128         // Inflate the currently selected menu XML resource.
    129         MenuInflater inflater = getMenuInflater();
    130         inflater.inflate(sMenuExampleResources[mSpinner.getSelectedItemPosition()], menu);
    131 
    132         // Change instructions
    133         mInstructionsText.setText(getResources().getString(
    134                 R.string.menu_from_xml_instructions_go_back));
    135 
    136         return true;
    137     }
    138 
    139     @Override
    140     public boolean onOptionsItemSelected(MenuItem item) {
    141         switch (item.getItemId()) {
    142             // For "Title only": Examples of matching an ID with one assigned in
    143             //                   the XML
    144             case R.id.jump:
    145                 Toast.makeText(this, "Jump up in the air!", Toast.LENGTH_SHORT).show();
    146                 invalidateOptionsMenu();
    147                 return true;
    148 
    149             case R.id.dive:
    150                 Toast.makeText(this, "Dive into the water!", Toast.LENGTH_SHORT).show();
    151                 return true;
    152 
    153             // For "Groups": Toggle visibility of grouped menu items with
    154             //               nongrouped menu items
    155             case R.id.browser_visibility:
    156                 // The refresh item is part of the browser group
    157                 final boolean shouldShowBrowser = !mMenu.findItem(R.id.refresh).isVisible();
    158                 mMenu.setGroupVisible(R.id.browser, shouldShowBrowser);
    159                 break;
    160 
    161             case R.id.email_visibility:
    162                 // The reply item is part of the email group
    163                 final boolean shouldShowEmail = !mMenu.findItem(R.id.reply).isVisible();
    164                 mMenu.setGroupVisible(R.id.email, shouldShowEmail);
    165                 break;
    166 
    167             // Generic catch all for all the other menu resources
    168             default:
    169                 // Don't toast text when a submenu is clicked
    170                 if (!item.hasSubMenu()) {
    171                     Toast.makeText(this, item.getTitle(), Toast.LENGTH_SHORT).show();
    172                     return true;
    173                 }
    174                 break;
    175         }
    176 
    177         return false;
    178     }
    179 
    180 
    181 
    182 }
    183