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:maimeng.yodian.app.client.android.chat.activity.ChatActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_chat_menu, menu);
    if ("hx_admin".equals(chatUser.getChatName())) {
        MENU_ID_CONTACT = 1001;/*  w  w w.  j  a v  a2s. c  om*/
        menu.add(0, MENU_ID_CONTACT, 0, "").setIcon(R.mipmap.ic_contact_path)
                .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
    }
    return super.onCreateOptionsMenu(menu);
}

From source file:com.example.android.bluepayandroidsdk.MainActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    itemStartSC = menu.add(0, START_SWIPE_CARD, Menu.NONE, "Swipe Card");
    itemStartSC.setEnabled(true);/*w ww .  ja  v a 2  s  .c  om*/
    sub = menu.addSubMenu(0, SETTINGS_ITEM, Menu.NONE, "Settings");
    itemSubSaveLog = sub.add(0, SUB_SAVE_LOG_ITEM, Menu.NONE, "Save Log option");
    itemSubUseAutoConfigProfile = sub.add(1, SUB_USE_AUTOCONFIG_PROFILE, Menu.NONE, "Use AutoConfig profile");
    itemSubSelectReader = sub.add(1, SUB_SELECT_READER, Menu.NONE, "Change reader type");
    itemSubLoadXML = sub.add(1, SUB_LOAD_XML, Menu.NONE, "Reload XML");
    itemSubStartAutoConfig = sub.add(4, SUB_START_AUTOCONFIG, Menu.NONE, "Start AutoConfig");
    itemSubStopAutoConfig = sub.add(6, SUB_STOP_AUTOCONFIG, Menu.NONE, "Stop AutoConfig");
    sub.add(Menu.NONE, SUB_ATTACHED_TYPE, Menu.NONE, "Get attached type");
    sub.add(Menu.NONE, SUB_SUPPORT_STATUS, Menu.NONE, "Get support status");
    itemSubSaveLog.setCheckable(true);
    itemSubUseAutoConfigProfile.setCheckable(true);
    itemSubLoadXML.setEnabled(true);
    itemSubStartAutoConfig.setEnabled(true);
    itemSubStopAutoConfig.setEnabled(true);
    itemDelLogs = menu.add(0, DELETE_LOG_ITEM, Menu.NONE, "Delete Logs");
    itemDelLogs.setEnabled(true);
    itemAbout = menu.add(0, ABOUT_ITEM, Menu.NONE, "About");
    itemAbout.setEnabled(true);
    itemExitApp = menu.add(0, EXIT_IDT_APP, Menu.NONE, "Exit");
    itemExitApp.setEnabled(true);
    return super.onCreateOptionsMenu(menu);
}

From source file:github.daneren2005.dsub.fragments.SubsonicFragment.java

protected void recreateContextMenu(Menu menu) {
    List<MenuItem> menuItems = new ArrayList<MenuItem>();
    for (int i = 0; i < menu.size(); i++) {
        MenuItem item = menu.getItem(i);
        if (item.isVisible()) {
            menuItems.add(item);/* www.  ja v a2s . co m*/
        }
    }
    menu.clear();
    for (int i = 0; i < menuItems.size(); i++) {
        MenuItem item = menuItems.get(i);
        menu.add(tag, item.getItemId(), Menu.NONE, item.getTitle());
    }
}

From source file:com.native5.plugins.ActionBarPlugin.java

private boolean buildMenu(Menu menu, JSONArray definition, String menu_var) {
    // Sadly MenuItem.setIcon and SubMenu.setIcon have conficting return types (for chaining), thus this can't be done w/ generics :(
    class GetMenuItemIconTask extends AsyncTask<String, Void, Drawable> {
        public final MenuItem item;
        public Exception exception = null;

        GetMenuItemIconTask(MenuItem item) {
            this.item = item;
        }/*from  ww w.  ja  v  a 2 s .  c om*/

        @Override
        protected Drawable doInBackground(String... uris) {
            return getDrawableForURI(uris[0]);
        }

        @Override
        protected void onPostExecute(Drawable icon) {
            if (icon != null) {
                item.setIcon(icon);
            }
        }
    }
    ;

    class GetSubMenuIconTask extends AsyncTask<String, Void, Drawable> {
        public final SubMenu item;
        public Exception exception = null;

        GetSubMenuIconTask(SubMenu item) {
            this.item = item;
        }

        @Override
        protected Drawable doInBackground(String... uris) {
            return getDrawableForURI(uris[0]);
        }

        @Override
        protected void onPostExecute(Drawable icon) {
            if (icon != null) {
                item.setIcon(icon);
            }
        }
    }
    ;

    try {
        for (int i = 0; i < definition.length(); ++i) {
            final JSONObject item_def = definition.getJSONObject(i);
            final String text = item_def.isNull("text") ? "" : item_def.getString("text");

            if (!item_def.has("items")) {
                MenuItem item = menu.add(0, i, i, text);
                item.setTitleCondensed(text);
                if (item_def.isNull("icon") == false) {
                    GetMenuItemIconTask task = new GetMenuItemIconTask(item);

                    synchronized (task) {
                        task.execute(item_def.getString("icon"));
                    }
                }

                // Default to MenuItem.SHOW_AS_ACTION_IF_ROOM, otherwise take user defined value.
                item.setShowAsAction(item_def.has("show") ? item_def.getInt("show")
                        : MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);

                menu_callbacks.put(item,
                        "var item = " + menu_var + "[" + i + "]; if(item.click) item.click();");
            } else {
                SubMenu submenu = menu.addSubMenu(0, i, i, text);
                if (item_def.isNull("icon") == false) {
                    GetSubMenuIconTask task = new GetSubMenuIconTask(submenu);

                    synchronized (task) {
                        task.execute(item_def.getString("icon"));
                    }
                }

                // Set submenu header
                if (item_def.has("header")) {
                    JSONObject header = item_def.getJSONObject("header");

                    if (header.has("title")) {
                        submenu.setHeaderTitle(header.getString("title"));
                    }

                    if (header.has("icon")) {
                        submenu.setHeaderIcon(getDrawableForURI(header.getString("icon")));
                    }
                }

                // Build sub-menu
                buildMenu(submenu, item_def.getJSONArray("items"), menu_var + "[" + i + "].items");
            }
        }
    } catch (JSONException e) {
        return false;
    }

    return true;
}

From source file:com.kll.collect.android.activities.FormEntryActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    Collect.getInstance().getActivityLogger().logInstanceAction(this, "onCreateOptionsMenu", "show");
    super.onCreateOptionsMenu(menu);

    CompatibilityUtils.setShowAsAction(/* w  w w . ja v  a 2 s.  c o m*/
            menu.add(0, MENU_SAVE, 0, R.string.save_all_answers).setIcon(android.R.drawable.ic_menu_save),
            MenuItem.SHOW_AS_ACTION_IF_ROOM);

    CompatibilityUtils.setShowAsAction(
            menu.add(0, MENU_HIERARCHY_VIEW, 0, R.string.view_hierarchy).setIcon(R.drawable.ic_menu_goto),
            MenuItem.SHOW_AS_ACTION_IF_ROOM);

    CompatibilityUtils.setShowAsAction(menu.add(0, MENU_LANGUAGES, 0, R.string.change_language)
            .setIcon(R.drawable.ic_menu_start_conversation), MenuItem.SHOW_AS_ACTION_NEVER);

    CompatibilityUtils.setShowAsAction(menu.add(0, MENU_PREFERENCES, 0, R.string.general_preferences)
            .setIcon(R.drawable.ic_menu_preferences), MenuItem.SHOW_AS_ACTION_NEVER);
    return true;
}

From source file:cm.aptoide.pt.MainActivity.java

@Override
public boolean onPrepareOptionsMenu(cm.aptoide.com.actionbarsherlock.view.Menu menu) {
    menu.clear();/*w w w . j  av  a  2 s. c o  m*/
    // menu.add(Menu.NONE, EnumOptionsMenu.SEARCH.ordinal(),
    // EnumOptionsMenu.SEARCH.ordinal(), "Search")
    // .setIcon(R.drawable.ic_search)
    // .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS |
    // MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
    menu.add(Menu.NONE, EnumOptionsMenu.LOGIN.ordinal(), EnumOptionsMenu.LOGIN.ordinal(), R.string.my_account)
            .setIcon(android.R.drawable.ic_menu_edit);
    menu.add(Menu.NONE, EnumOptionsMenu.DISPLAY_OPTIONS.ordinal(), EnumOptionsMenu.DISPLAY_OPTIONS.ordinal(),
            R.string.menu_display_options).setIcon(android.R.drawable.ic_menu_sort_by_size);
    menu.add(Menu.NONE, EnumOptionsMenu.SCHEDULED_DOWNLOADS.ordinal(),
            EnumOptionsMenu.SCHEDULED_DOWNLOADS.ordinal(), R.string.setting_schdwntitle)
            .setIcon(android.R.drawable.ic_menu_agenda);
    menu.add(Menu.NONE, EnumOptionsMenu.SETTINGS.ordinal(), EnumOptionsMenu.SETTINGS.ordinal(),
            R.string.settings_title_bar).setIcon(android.R.drawable.ic_menu_manage);

    if (ApplicationAptoide.PARTNERID == null) {
        // menu.add(Menu.NONE, EnumOptionsMenu.ABOUT.ordinal(),
        // EnumOptionsMenu.ABOUT.ordinal(), R.string.about).setIcon(
        // android.R.drawable.ic_menu_help);
        menu.add(Menu.NONE, EnumOptionsMenu.FOLLOW.ordinal(), EnumOptionsMenu.FOLLOW.ordinal(),
                R.string.social_networks).setIcon(android.R.drawable.ic_menu_share);
    }

    menu.add(Menu.NONE, EnumOptionsMenu.DOWNLOADMANAGER.ordinal(), EnumOptionsMenu.DOWNLOADMANAGER.ordinal(),
            R.string.download_manager).setIcon(android.R.drawable.ic_menu_save);

    return super.onPrepareOptionsMenu(menu);
}

From source file:de.ub0r.android.websms.WebSMS.java

/**
 * {@inheritDoc}//from w w  w . j a  v  a2s  .  com
 */
@Override
public final boolean onPrepareOptionsMenu(final Menu menu) {
    final ConnectorSpec[] connectors = getConnectors(ConnectorSpec.CAPABILITIES_SEND,
            ConnectorSpec.STATUS_READY | ConnectorSpec.STATUS_ENABLED, true /*isIncludePseudoConnectors*/);

    boolean isPrefsConnectorOk = prefsConnectorSpec != null && prefsSubConnectorSpec != null
            && prefsConnectorSpec.hasStatus(ConnectorSpec.STATUS_ENABLED);
    menu.findItem(R.id.item_connector)
            .setVisible(connectors.length > 1
                    || (connectors.length == 1 && connectors[0].getSubConnectorCount() > 1)
                    || (connectors.length == 1 && !isPrefsConnectorOk));

    boolean hasText = this.etText != null && !TextUtils.isEmpty(this.etText.getText());
    menu.findItem(R.id.item_savechars).setVisible(hasText);
    // only allow to save drafts on API18-
    menu.findItem(R.id.item_draft).setVisible(Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT && hasText);
    final boolean showRestore = !TextUtils
            .isEmpty(PreferenceManager.getDefaultSharedPreferences(this).getString(PREFS_BACKUPLASTTEXT, null));
    try {
        menu.removeItem(ITEM_RESTORE);
    } catch (Exception e) {
        Log.w(TAG, "error removing item: " + ITEM_RESTORE, e);
    }
    if (showRestore) {
        menu.add(0, ITEM_RESTORE, android.view.Menu.NONE, R.string.restore_);
        menu.findItem(ITEM_RESTORE).setIcon(android.R.drawable.ic_menu_revert);
    }
    return true;
}