MenuInflateFromXmlpublic class MenuInflateFromXml extends android.app.Activity Demonstrates inflating menus from XML. There are different menu XML resources
that the user can choose to inflate. First, select an example resource from
the spinner, and then hit the menu button. To choose another, back out of the
activity and start over. |
Fields Summary |
---|
private static final int[] | sMenuExampleResourcesDifferent example menu resources. | private static final String[] | sMenuExampleNamesNames corresponding to the different example menu resources. | private android.widget.Spinner | mSpinnerLets the user choose a menu resource. | private android.widget.TextView | mInstructionsTextShown as instructions. | private android.view.Menu | mMenuSafe to hold on to this. |
Methods Summary |
---|
protected void | onCreate(android.os.Bundle savedInstanceState)
super.onCreate(savedInstanceState);
// Create a simple layout
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
// Create the spinner to allow the user to choose a menu XML
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, sMenuExampleNames);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinner = new Spinner(this);
// When programmatically creating views, make sure to set an ID
// so it will automatically save its instance state
mSpinner.setId(R.id.spinner);
mSpinner.setAdapter(adapter);
// Add the spinner
layout.addView(mSpinner,
new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
// Create help text
mInstructionsText = new TextView(this);
mInstructionsText.setText(getResources().getString(
R.string.menu_from_xml_instructions_press_menu));
// Add the help, make it look decent
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(10, 10, 10, 10);
layout.addView(mInstructionsText, lp);
// Set the layout as our content view
setContentView(layout);
| public boolean | onCreateOptionsMenu(android.view.Menu menu)
// Hold on to this
mMenu = menu;
// Inflate the currently selected menu XML resource.
MenuInflater inflater = getMenuInflater();
inflater.inflate(sMenuExampleResources[mSpinner.getSelectedItemPosition()], menu);
// Disable the spinner since we've already created the menu and the user
// can no longer pick a different menu XML.
mSpinner.setEnabled(false);
// Change instructions
mInstructionsText.setText(getResources().getString(
R.string.menu_from_xml_instructions_go_back));
return true;
| public boolean | onOptionsItemSelected(android.view.MenuItem item)
switch (item.getItemId()) {
// For "Title only": Examples of matching an ID with one assigned in
// the XML
case R.id.jump:
Toast.makeText(this, "Jump up in the air!", Toast.LENGTH_SHORT).show();
return true;
case R.id.dive:
Toast.makeText(this, "Dive into the water!", Toast.LENGTH_SHORT).show();
return true;
// For "Groups": Toggle visibility of grouped menu items with
// nongrouped menu items
case R.id.browser_visibility:
// The refresh item is part of the browser group
final boolean shouldShowBrowser = !mMenu.findItem(R.id.refresh).isVisible();
mMenu.setGroupVisible(R.id.browser, shouldShowBrowser);
break;
case R.id.email_visibility:
// The reply item is part of the email group
final boolean shouldShowEmail = !mMenu.findItem(R.id.reply).isVisible();
mMenu.setGroupVisible(R.id.email, shouldShowEmail);
break;
// Generic catch all for all the other menu resources
default:
// Don't toast text when a submenu is clicked
if (!item.hasSubMenu()) {
Toast.makeText(this, item.getTitle(), Toast.LENGTH_SHORT).show();
return true;
}
break;
}
return false;
|
|