Create Menu within your code : Menu « UI « Android

Home
Android
1.2D Graphics
2.Animation
3.Core Class
4.Database
5.Date Type
6.Development
7.File
8.Game
9.Hardware
10.Media
11.Network
12.Security
13.UI
14.User Event
Android » UI » Menu 
Create Menu within your code
  

package app.test;

import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class Test extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button btn = (ButtonfindViewById(R.id.btn1);
    btn.setOnCreateContextMenuListener(this);
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    CreateMenu(menu);
    return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    return MenuChoice(item);
  }

  @Override
  public void onCreateContextMenu(ContextMenu menu, View view,
      ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, view, menuInfo);
    CreateMenu(menu);
  }

  @Override
  public boolean onContextItemSelected(MenuItem item) {
    return MenuChoice(item);
  }

  private void CreateMenu(Menu menu) {
    menu.setQwertyMode(true);
    MenuItem mnu1 = menu.add(000"Item 1");
    {
      mnu1.setAlphabeticShortcut('a');
      mnu1.setIcon(R.drawable.icon);
    }
    MenuItem mnu2 = menu.add(011"Item 2");
    {
      mnu2.setAlphabeticShortcut('b');
      mnu2.setIcon(R.drawable.icon);
    }
    MenuItem mnu3 = menu.add(022"Item 3");
    {
      mnu3.setAlphabeticShortcut('c');
      mnu3.setIcon(R.drawable.icon);
    }
    MenuItem mnu4 = menu.add(033"Item 4");
    {
      mnu4.setAlphabeticShortcut('d');
    }
    menu.add(033"Item 5");
    menu.add(033"Item 6");
    menu.add(033"Item 7");
  }

  private boolean MenuChoice(MenuItem item) {
    switch (item.getItemId()) {
    case 0:
      Toast.makeText(this, "You clicked on Item 1", Toast.LENGTH_LONG)
          .show();
      return true;
    case 1:
      Toast.makeText(this, "You clicked on Item 2", Toast.LENGTH_LONG)
          .show();
      return true;
    case 2:
      Toast.makeText(this, "You clicked on Item 3", Toast.LENGTH_LONG)
          .show();
      return true;
    case 3:
      Toast.makeText(this, "You clicked on Item 4", Toast.LENGTH_LONG)
          .show();
      return true;
    case 4:
      Toast.makeText(this, "You clicked on Item 5", Toast.LENGTH_LONG)
          .show();
      return true;
    case 5:
      Toast.makeText(this, "You clicked on Item 6", Toast.LENGTH_LONG)
          .show();
      return true;
    case 6:
      Toast.makeText(this, "You clicked on Item 7", Toast.LENGTH_LONG)
          .show();
      return true;
    }
    return false;
  }
}
//main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<Button android:id="@+id/btn1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:text = "Tap and hold this for more options..." />     

</LinearLayout>

   
    
  
Related examples in the same category
1.Create Option menu
2.Menu and messages
3.Context menu
4.Custom menu
5.Using Icon in Menu
6.Option Menu selection event
7.Menu Inflation
8.Context Menu event
9.Activity Menu
10.Define menu in xml file
11.Adding submenu
12.Using Icon in menu and submenu
13.Get Menu title
14.onOptionsItemSelected/onPrepareOptionsMenu/onCreateOptionsMenu Event
15.Basics of the Action Bar and how it interoperates with the standard options menu.
16.Demonstration of displaying a context menu from a fragment.
17.Demonstrates how fragments can participate in the options menu.
18.Demonstrates inflating menus from XML.
19.Usage of SearchView in an ActionBar as a menu item.
20.This demo illustrates the use of CHOICE_MODE_MULTIPLE_MODAL
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.