Example usage for android.view Menu add

List of usage examples for android.view Menu add

Introduction

In this page you can find the example usage for android.view Menu add.

Prototype

public MenuItem add(int groupId, int itemId, int order, @StringRes int titleRes);

Source Link

Document

Variation on #add(int,int,int,CharSequence) that takes a string resource identifier instead of the string itself.

Usage

From source file:com.googlecode.android_scripting.facade.ui.UiFacade.java

public boolean onPrepareOptionsMenu(Menu menu) {
    if (mMenuUpdated.getAndSet(false)) {
        menu.removeGroup(MENU_GROUP_ID);
        for (UiMenuItem item : mOptionsMenuItems) {
            MenuItem menuItem = menu.add(MENU_GROUP_ID, Menu.NONE, Menu.NONE, item.mmTitle);
            if (item.mmIcon != null) {
                menuItem.setIcon(mService.getResources().getIdentifier(item.mmIcon, "drawable", "android"));
            }// ww w . j  a  v  a  2  s. c  o  m
            menuItem.setOnMenuItemClickListener(item.mmListener);
        }
        return true;
    }
    return true;
}

From source file:com.itude.mobile.mobbl.core.view.components.tabbar.MBDefaultActionBarBuilder.java

@Override
public void fillActionBar(ActionBar actionBar, Menu menu) {
    _actionBar = actionBar;/* ww w .  jav  a  2 s.c o m*/
    _menu = menu;

    List<MBToolDefinition> tools = MBMetadataService.getInstance().getTools();

    for (MBToolDefinition def : tools) {
        if (def.isPreConditionValid()) {
            String localizedTitle = MBLocalizationService.getInstance().getTextForKey(def.getTitle());
            MenuItem menuItem = menu.add(Menu.NONE, def.getName().hashCode(), tools.indexOf(def),
                    localizedTitle);

            Drawable image = null;
            if (def.getIcon() != null) {
                image = MBResourceService.getInstance().getImageByID(def.getIcon());
                menuItem.setIcon(image);
            }

            MenuItemCompat.setShowAsAction(menuItem, getMenuItemActionFlags(def));

            if ("REFRESH".equals(def.getType())) {
                _refreshToolDef = def;
            } else if ("SEARCH".equals(def.getType())) {
                final SearchView searchView = new SearchView(_context);
                searchView.setTag(def);
                searchView.setOnQueryTextFocusChangeListener(new OnFocusChangeListener() {
                    @Override
                    public void onFocusChange(View v, boolean hasFocus) {
                        if (hasFocus) {
                            Object tag = v.getTag();
                            if (tag instanceof MBToolDefinition) {
                                MBToolDefinition toolDef = (MBToolDefinition) tag;
                                if (toolDef.getOutcomeName() != null) {
                                    handleOutcome(toolDef);
                                }
                            }
                        } else {
                            searchView.setIconified(true);
                        }
                    }
                });

                changeSearchImage(image, searchView);

                SearchManager searchManager = (SearchManager) _context.getSystemService(Context.SEARCH_SERVICE);

                searchView.setSearchableInfo(
                        searchManager.getSearchableInfo(MBViewManager.getInstance().getComponentName()));

                MenuItemCompat.setActionView(menuItem, searchView);
            }
        }
    }
    if (actionBar != null) {
        populateActionBar(actionBar);
    }
}

From source file:com.studyjams.mdvideo.PlayerModule.PlayerActivity.java

private void configurePopupWithTracks(PopupMenu popup, final OnMenuItemClickListener customActionClickListener,
        final int trackType) {
    if (player == null) {
        return;//ww  w. ja  v  a  2 s. c  om
    }
    int trackCount = player.getTrackCount(trackType);
    if (trackCount == 0) {
        return;
    }
    popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            return (customActionClickListener != null && customActionClickListener.onMenuItemClick(item))
                    || onTrackItemClick(item, trackType);
        }
    });
    Menu menu = popup.getMenu();
    // ID_OFFSET ensures we avoid clashing with Menu.NONE (which equals 0).
    menu.add(MENU_GROUP_TRACKS, DemoPlayer.TRACK_DISABLED + ID_OFFSET, Menu.NONE, R.string.off);
    for (int i = 0; i < trackCount; i++) {
        menu.add(MENU_GROUP_TRACKS, i + ID_OFFSET, Menu.NONE,
                buildTrackName(player.getTrackFormat(trackType, i)));
    }
    menu.setGroupCheckable(MENU_GROUP_TRACKS, true, true);
    menu.findItem(player.getSelectedTrack(trackType) + ID_OFFSET).setChecked(true);
}

From source file:com.jecelyin.editor.v2.ui.MainActivity.java

private void initToolbar() {

    Resources res = getResources();

    mToolbar.setNavigationIcon(R.drawable.ic_drawer_raw);
    mToolbar.setNavigationContentDescription(R.string.tab);

    Menu menu = mToolbar.getMenu();
    List<MenuItemInfo> menuItemInfos = MenuFactory.getInstance(this).getToolbarIcon();
    for (MenuItemInfo item : menuItemInfos) {
        MenuItem menuItem = menu.add(MenuDef.GROUP_TOOLBAR, item.getItemId(), Menu.NONE, item.getTitleResId());
        menuItem.setIcon(MenuManager.makeToolbarNormalIcon(res, item.getIconResId()));

        //menuItem.setShortcut()
        menuItem.setOnMenuItemClickListener(this);
        menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
    }/*from  w w w  . j a va2s. c o m*/
    MenuItem menuItem = menu.add(MenuDef.GROUP_TOOLBAR, R.id.m_menu, Menu.NONE, getString(R.string.more_menu));
    menuItem.setIcon(R.drawable.ic_right_menu);
    menuItem.setOnMenuItemClickListener(this);
    menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);

    tabManager = new TabManager(this);
}

From source file:com.acrylicgoat.devchat.MainActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    readDB();// w  w  w . j  ava 2 s  . c  o m
    if (devs != null && devs.size() > 0) {
        for (int i = 0; i < devs.size(); i++) {
            Developer dev = devs.get(i);
            if (i == 0 && (currentOwner == null || currentOwner.equals(""))) {
                currentOwner = dev.getName();
            }
            menu.add(0, MENUITEM, 0, dev.getName()).setIcon(R.drawable.dev);

        }
    }
    getOwner(currentOwner);
    //        SpinnerAdapter mSpinnerAdapter;
    //        if(Build.VERSION.SDK_INT <= 10)
    //        {
    //            mSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.nav_list,android.R.layout.simple_spinner_item);
    //        }
    //        else
    //        {
    //            mSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.nav_list,android.R.layout.simple_spinner_dropdown_item);
    //        }
    //        ActionBar.OnNavigationListener mOnNavigationListener = new ActionBar.OnNavigationListener()
    //        {
    //            // Get the same strings provided for the drop-down's ArrayAdapter
    //            String[] strings = getResources().getStringArray(R.array.nav_list);
    //
    //            @Override
    //            public boolean onNavigationItemSelected(int position, long itemId)
    //            {
    //                switch (position)
    //                {
    //                    case 1:
    //                        Intent devIntent = new Intent(getApplicationContext(), DevActivity.class);
    //                        startActivity(devIntent);
    //                        break;
    //
    //                    case 2:
    //                        Intent reportIntent = new Intent(getApplicationContext(), DataTableActivity.class);
    //                        startActivity(reportIntent);
    //                        break;
    //                }
    //
    //                return true;
    //            }
    //        };
    //        aBar.setListNavigationCallbacks(mSpinnerAdapter, mOnNavigationListener);

    return true;
}

From source file:me.piebridge.prevent.ui.PreventActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    menu.clear();/*from  w  w w.j  a  va2s . c  om*/
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.GINGERBREAD_MR1) {
        preventMenu = menu.add(Menu.NONE, R.string.prevent, Menu.NONE, R.string.prevent);
        preventMenu.setIcon(R.drawable.ic_menu_block);
        preventMenu.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        preventMenu.setVisible(false);
        removeMenu = menu.add(Menu.NONE, R.string.remove, Menu.NONE, R.string.remove);
        removeMenu.setIcon(R.drawable.ic_menu_star);
        removeMenu.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        removeMenu.setVisible(false);
    }
    menu.add(Menu.NONE, R.string.switch_theme, Menu.NONE, R.string.switch_theme);
    menu.add(Menu.NONE, R.string.system_log, Menu.NONE, R.string.system_log);
    menu.add(Menu.NONE, R.string.advanced_settings, Menu.NONE, R.string.advanced_settings);
    menu.add(Menu.NONE, R.string.user_guide, Menu.NONE, R.string.user_guide);
    return super.onCreateOptionsMenu(menu);
}

From source file:com.android.mms.ui.ConversationList.java

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    menu.clear();//  w w w.  java  2  s.  co m

    menu.add(0, MENU_MSG_FOLDER, 0, R.string.menu_msg_folder).setIcon(R.drawable.ic_menu_folder);

    menu.add(0, MENU_COMPOSE_NEW, 0, R.string.menu_compose_new)
            .setIcon(com.android.internal.R.drawable.ic_menu_compose);

    if (mListAdapter.getCount() > 0) {
        menu.add(0, MENU_DELETE_ALL, 0, R.string.menu_delete_all).setIcon(android.R.drawable.ic_menu_delete);
    }

    menu.add(0, MENU_SEARCH, 0, android.R.string.search_go).setIcon(android.R.drawable.ic_menu_search)
            .setAlphabeticShortcut(android.app.SearchManager.MENU_KEY);

    menu.add(0, MENU_PREFERENCES, 0, R.string.menu_preferences).setIcon(android.R.drawable.ic_menu_preferences);

    menu.add(0, MENU_CBSMS, 0, R.string.cell_broadcast_sms).setIcon(R.drawable.menu_cb);

    return true;
}

From source file:org.openremote.android.console.GroupActivity.java

/**
 * If there is no global and local tabbar, create default menu.
 * Contains "setting" and "logout".//from  w  ww  . j  av  a  2  s.  co m
 * 
 * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
 */
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    menu.setQwertyMode(true);
    MenuItem setting = menu.add(-1, Constants.MENU_ITEM_SETTING, 0, R.string.setting);
    setting.setIcon(R.drawable.ic_menu_manage);

    if (AppSettingsModel.isSSLEnabled(getApplicationContext())) {
        MenuItem ssl = menu.add(-1, Constants.MENU_ITEM_SSL, 0, R.string.ssl_clientcert);
        ssl.setIcon(R.drawable.ic_menu_lock);
    }

    MenuItem logout = menu.add(-1, Constants.MENU_ITEM_LOGOUT, 1, R.string.logout);
    logout.setIcon(R.drawable.ic_menu_revert);
    return true;
}

From source file:com.android.mms.ui.SelectRecipientsList.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Load the required preference values
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    mMobileOnly = sharedPreferences.getBoolean(PREF_MOBILE_NUMBERS_ONLY, true);

    menu.add(0, MENU_DONE, 0, R.string.menu_done).setIcon(R.drawable.ic_menu_done)
            .setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT)
            .setVisible(false);/* ww  w.  ja  va2  s .  c  om*/

    menu.add(0, MENU_MOBILE, 0, R.string.menu_mobile).setCheckable(true).setChecked(mMobileOnly)
            .setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_NEVER | MenuItem.SHOW_AS_ACTION_WITH_TEXT);

    boolean useAltNameOrdering = mContactsPreferences
            .getSortOrder() == ContactsPreferences.SORT_ORDER_ALTERNATIVE;
    int nameOrderItemTitleRes = useAltNameOrdering ? R.string.menu_order_by_given_name
            : R.string.menu_order_by_last_name;

    menu.add(0, MENU_NAME_ORDER, 0, nameOrderItemTitleRes)
            .setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_NEVER | MenuItem.SHOW_AS_ACTION_WITH_TEXT);

    return super.onCreateOptionsMenu(menu);
}

From source file:com.grarak.kerneladiutor.fragments.tools.ProfileFragment.java

private void load(List<RecyclerViewItem> items) {
    mProfiles = new Profiles(getActivity());
    List<Profiles.ProfileItem> profileItems = mProfiles.getAllProfiles();
    if (mTaskerMode && profileItems.size() == 0) {
        Snackbar.make(getRootView(), R.string.no_profiles, Snackbar.LENGTH_LONG).show();
        return;//from  w  ww. j a v  a 2 s.c  o m
    }
    for (int i = 0; i < profileItems.size(); i++) {
        final int position = i;
        final CardView cardView = new CardView(getActivity());
        cardView.setOnMenuListener(new CardView.OnMenuListener() {
            @Override
            public void onMenuReady(final CardView cardView, PopupMenu popupMenu) {
                Menu menu = popupMenu.getMenu();
                menu.add(Menu.NONE, 0, Menu.NONE, getString(R.string.append));
                menu.add(Menu.NONE, 1, Menu.NONE, getString(R.string.edit));
                menu.add(Menu.NONE, 2, Menu.NONE, getString(R.string.details));
                final MenuItem onBoot = menu.add(Menu.NONE, 3, Menu.NONE, getString(R.string.on_boot))
                        .setCheckable(true);
                onBoot.setChecked(mProfiles.getAllProfiles().get(position).isOnBootEnabled());
                menu.add(Menu.NONE, 4, Menu.NONE, getString(R.string.export));
                menu.add(Menu.NONE, 5, Menu.NONE, getString(R.string.delete));

                popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        List<Profiles.ProfileItem> items = mProfiles.getAllProfiles();
                        switch (item.getItemId()) {
                        case 0:
                            if (Utils.DONATED) {
                                Intent intent = new Intent(getActivity(), ProfileActivity.class);
                                intent.putExtra(ProfileActivity.POSITION_INTENT, position);
                                startActivityForResult(intent, 2);
                            } else {
                                mDonateDialog = ViewUtils.dialogDonate(getActivity());
                                mDonateDialog.show();
                            }
                            break;
                        case 1:
                            if (Utils.DONATED) {
                                Intent intent = new Intent(getActivity(), ProfileEditActivity.class);
                                intent.putExtra(ProfileEditActivity.POSITION_INTENT, position);
                                startActivityForResult(intent, 3);
                            } else {
                                mDonateDialog = ViewUtils.dialogDonate(getActivity());
                                mDonateDialog.show();
                            }
                            break;
                        case 2:
                            if (items.get(position).getName() != null) {
                                List<Profiles.ProfileItem.CommandItem> commands = items.get(position)
                                        .getCommands();
                                if (commands.size() > 0) {
                                    setForegroundText(items.get(position).getName().toUpperCase());
                                    mDetailsFragment.setText(commands);
                                    showForeground();
                                } else {
                                    Utils.toast(R.string.profile_empty, getActivity());
                                }
                            }
                            break;
                        case 3:
                            onBoot.setChecked(!onBoot.isChecked());
                            items.get(position).enableOnBoot(onBoot.isChecked());
                            mProfiles.commit();
                            break;
                        case 4:
                            mExportProfile = items.get(position);
                            requestPermission(0, Manifest.permission.WRITE_EXTERNAL_STORAGE);
                            break;
                        case 5:
                            mDeleteDialog = ViewUtils.dialogBuilder(getString(R.string.sure_question),
                                    new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialogInterface, int i) {
                                        }
                                    }, new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialogInterface, int i) {
                                            mProfiles.delete(position);
                                            mProfiles.commit();
                                            reload();
                                        }
                                    }, new DialogInterface.OnDismissListener() {
                                        @Override
                                        public void onDismiss(DialogInterface dialogInterface) {
                                            mDeleteDialog = null;
                                        }
                                    }, getActivity());
                            mDeleteDialog.show();
                            break;
                        }
                        return false;
                    }
                });
            }
        });

        final DescriptionView descriptionView = new DescriptionView();
        descriptionView.setSummary(profileItems.get(i).getName());
        descriptionView.setOnItemClickListener(new RecyclerViewItem.OnItemClickListener() {
            @Override
            public void onClick(RecyclerViewItem item) {
                if (mTaskerMode) {
                    mSelectDialog = ViewUtils.dialogBuilder(
                            getString(R.string.select_question, descriptionView.getSummary()),
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                }
                            }, new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    ((ProfileTaskerActivity) getActivity()).finish(
                                            descriptionView.getSummary().toString(),
                                            mProfiles.getAllProfiles().get(position).getCommands());
                                }
                            }, new DialogInterface.OnDismissListener() {
                                @Override
                                public void onDismiss(DialogInterface dialogInterface) {
                                    mSelectDialog = null;
                                }
                            }, getActivity());
                    mSelectDialog.show();
                } else {
                    mApplyDialog = ViewUtils.dialogBuilder(
                            getString(R.string.apply_question, descriptionView.getSummary()),
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                }
                            }, new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    for (Profiles.ProfileItem.CommandItem command : mProfiles.getAllProfiles()
                                            .get(position).getCommands()) {
                                        CPUFreq.ApplyCpu applyCpu;
                                        if (command.getCommand().startsWith("#")
                                                && ((applyCpu = new CPUFreq.ApplyCpu(
                                                        command.getCommand().substring(1)))
                                                                .toString() != null)) {
                                            for (String applyCpuCommand : Service.getApplyCpu(applyCpu,
                                                    RootUtils.getSU())) {
                                                Control.runSetting(applyCpuCommand, null, null, null);
                                            }
                                        } else {
                                            Control.runSetting(command.getCommand(), null, null, null);
                                        }
                                    }
                                }
                            }, new DialogInterface.OnDismissListener() {
                                @Override
                                public void onDismiss(DialogInterface dialogInterface) {
                                    mApplyDialog = null;
                                }
                            }, getActivity());
                    try {
                        mApplyDialog.show();
                    } catch (NullPointerException ignored) {
                    }
                }
            }
        });

        if (mTaskerMode) {
            items.add(descriptionView);
        } else {
            cardView.addItem(descriptionView);
            items.add(cardView);
        }
    }

    if (!mTaskerMode) {
        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getActivity());
        int appWidgetIds[] = appWidgetManager.getAppWidgetIds(new ComponentName(getActivity(), Widget.class));
        appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.profile_list);
        Tile.publishProfileTile(profileItems, getActivity());
    }
}