Code example for Menu

Methods: add, clear

0
            return true; 
        } 
    }; 
     
    private void addFontMenu(Menu menu, int index) {
        MenuItem item = menu.add(0, index, 0, sTypefaceName[index]);
        item.setCheckable(true);
        item.setOnMenuItemClickListener(mFontClickListener);
        item.setChecked(index == mFontIndex);
    } 
     
    private MenuItem.OnMenuItemClickListener mStyleClickListener = new MenuItem.OnMenuItemClickListener() {
        public boolean onMenuItemClick(MenuItem item) { 
            mFontStyle = mFontStyle ^ item.getItemId(); 
            refreshFont(); 
            return true; 
        } 
    }; 
     
    private void addStyleMenu(Menu menu, int style, char shortCut) {
        MenuItem item = menu.add(0, style, 0, (style == Typeface.BOLD) ? "Bold" : "Italic");
        item.setCheckable(true);
        item.setOnMenuItemClickListener(mStyleClickListener);
        item.setChecked((mFontStyle & style) != 0);
 
        item.setVisible(canSupportStyle(sTypeface[mFontIndex], style));
        if (shortCut != 0) {
            item.setAlphabeticShortcut(shortCut);
        } 
    } 
     
    private MenuItem.OnMenuItemClickListener mFlagClickListener = new MenuItem.OnMenuItemClickListener() {
        public boolean onMenuItemClick(MenuItem item) { 
            int mask = item.getItemId(); 
            mColumn1.setPaintFlags(mColumn1.getPaintFlags() ^ mask); 
            mColumn2.setPaintFlags(mColumn2.getPaintFlags() ^ mask); 
            updateTitle(); 
            return true; 
        } 
    }; 
     
    private 
    void addFlagMenu(Menu menu, int paintFlag, String label, char shortCut) {
        MenuItem item = menu.add(0, paintFlag, 0, label);
        item.setCheckable(true);
        item.setOnMenuItemClickListener(mFlagClickListener);
        item.setChecked((mColumn1.getPaintFlags() & paintFlag) != 0);
        if (shortCut != 0) {
            item.setAlphabeticShortcut(shortCut);
        } 
    } 
     
    private static void addListenerMenu(MenuItem item,
                                        MenuItem.OnMenuItemClickListener listener,
                                        char keyChar) {
        item.setOnMenuItemClickListener(listener);
        if (keyChar != '\0') {
            item.setAlphabeticShortcut(keyChar);
        } 
    } 
     
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        return true; 
    } 
     
    @Override public boolean onPrepareOptionsMenu(Menu menu) {
        super.onPrepareOptionsMenu(menu);
        menu.clear();
 
        addFontMenu(menu, FONT_INDEX_SANS);
        addFontMenu(menu, FONT_INDEX_SERIF);
//        addFontMenu(menu, FONT_INDEX_MONO); 
        addStyleMenu(menu, Typeface.BOLD, 'b');
        addStyleMenu(menu, Typeface.ITALIC, 'i');
        addFlagMenu(menu, Paint.SUBPIXEL_TEXT_FLAG, "SubPixel", 's');
        //        addFlagMenu(menu, Paint.DEV_KERN_TEXT_FLAG, "DevKern", 'k'); 
        menu.add(0, 0, 0, "Text").setOnMenuItemClickListener(mTextCallback).setAlphabeticShortcut('t');
         
        return true; 
    } 
 
    protected void onActivityResult(int requestCode, int resultCode,