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