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