Using Icon in menu and submenu : Menu « UI « Android






Using Icon in menu and submenu

  

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.MenuInflater;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.widget.TextView;

public class Test extends Activity {
  Menu myMenu = null;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    registerForContextMenu(this.getTextView());
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    this.myMenu = menu;
    addRegularMenuItems(menu);
    add5SecondaryMenuItems(menu);
    this.addSubMenu(menu);
    return true;
  }

  private void addRegularMenuItems(Menu menu) {
    int base = Menu.FIRST; // value is 1

    MenuItem item1 = menu.add(base, base, base, "append");
    menu.add(base, base + 1, base + 1, "item 2");
    menu.add(base, base + 2, base + 2, "clear");

    menu.add(base, base + 3, base + 3, "hide secondary");
    menu.add(base, base + 4, base + 4, "show secondary");

    menu.add(base, base + 5, base + 5, "enable secondary");
    menu.add(base, base + 6, base + 6, "disable secondary");

    menu.add(base, base + 7, base + 7, "check secondary");
    MenuItem item8 = menu
        .add(base, base + 8, base + 8, "uncheck secondary");
    item1.setIcon(R.drawable.icon);
    item8.setIcon(R.drawable.icon);
  }

  private void add5SecondaryMenuItems(Menu menu) {
    int base = Menu.CATEGORY_SECONDARY;

    menu.add(base, base + 1, base + 1, "sec. item 1");
    menu.add(base, base + 2, base + 2, "sec. item 2");
    menu.add(base, base + 3, base + 3, "sec. item 3");
    menu.add(base, base + 3, base + 3, "sec. item 4");
    menu.add(base, base + 4, base + 4, "sec. item 5");
  }

  private void addSubMenu(Menu menu) {
    int base = Menu.FIRST + 100;
    SubMenu sm = menu.addSubMenu(base, base + 1, Menu.NONE, "submenu");
    MenuItem item1 = sm.add(base, base + 2, base + 2, "sub item1");

    sm.add(base, base + 3, base + 3, "sub item2");
    sm.add(base, base + 4, base + 4, "sub item3");
    item1.setIcon(R.drawable.icon);
    sm.setIcon(R.drawable.icon);
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == 1) {
      appendText("\nhello");
    } else if (item.getItemId() == 2) {
      appendText("\nitem2");
    } else if (item.getItemId() == 3) {
      emptyText();
    } else if (item.getItemId() == 4) {
      // hide secondary
      this.appendMenuItemText(item);
      this.myMenu.setGroupVisible(Menu.CATEGORY_SECONDARY, false);
    } else if (item.getItemId() == 5) {
      this.appendMenuItemText(item);
      this.myMenu.setGroupVisible(Menu.CATEGORY_SECONDARY, true);
    } else if (item.getItemId() == 6) {
      this.appendMenuItemText(item);
      this.myMenu.setGroupEnabled(Menu.CATEGORY_SECONDARY, true);
    } else if (item.getItemId() == 7) {
      this.appendMenuItemText(item);
      this.myMenu.setGroupEnabled(Menu.CATEGORY_SECONDARY, false);
    } else if (item.getItemId() == 8) {
      this.appendMenuItemText(item);
      this.myMenu.setGroupCheckable(Menu.CATEGORY_SECONDARY, true, false);
    } else if (item.getItemId() == 9) {
      this.appendMenuItemText(item);
      this.myMenu
          .setGroupCheckable(Menu.CATEGORY_SECONDARY, false, false);
    } else {
      this.appendMenuItemText(item);
    }
    return true;
  }

  @Override
  public void onCreateContextMenu(ContextMenu menu, View v,
      ContextMenuInfo menuInfo) {
    menu.setHeaderTitle("Sample menu");
    menu.add(200, 200, 200, "item1");
  }

  @Override
  public boolean onContextItemSelected(MenuItem item) {
    this.appendMenuItemText(item);
    return true;
  }

  private void loadXMLMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater(); // from activity
    inflater.inflate(R.menu.my_menu, menu);
  }

  private TextView getTextView() {
    TextView tv = (TextView) this.findViewById(R.id.textViewId);
    return tv;
  }

  public void appendText(String text) {
    TextView tv = (TextView) this.findViewById(R.id.textViewId);
    tv.setText(tv.getText() + text);
  }

  private void appendMenuItemText(MenuItem menuItem) {
    String title = menuItem.getTitle().toString();
    TextView tv = (TextView) this.findViewById(R.id.textViewId);
    tv.setText(tv.getText() + "\n" + title + ":" + menuItem.getItemId());
  }

  private void emptyText() {
    TextView tv = (TextView) this.findViewById(R.id.textViewId);
    tv.setText("");
  }
}

//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"
    >
<TextView android:id="@+id/textViewId"  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
    
</LinearLayout>

//menu/my_menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- This group uses the default category. -->
    <group android:id="@+id/menuGroup_Main">

        <item android:id="@+id/menu_testPick"
            android:orderInCategory="5"
            android:title="Test Pick" />
            
        <item android:id="@+id/menu_testGetContent"
            android:orderInCategory="5"
            android:title="Test Get Content" />
            
        <item android:id="@+id/menu_clear"
            android:orderInCategory="10"
            android:title="clear" />
            
        <item android:id="@+id/menu_dial"
            android:orderInCategory="7"
            android:title="dial" />
            
        <item android:id="@+id/menu_test"
            android:orderInCategory="4"
            android:title="@+string/test" />
            
        <item android:id="@+id/menu_show_browser"
            android:orderInCategory="5"
            android:title="show browser" />
    </group>
</menu>

   
    
  








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.Create Menu within your code
10.Activity Menu
11.Define menu in xml file
12.Adding 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