Example usage for android.view Menu getItem

List of usage examples for android.view Menu getItem

Introduction

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

Prototype

public MenuItem getItem(int index);

Source Link

Document

Gets the menu item at the given index.

Usage

From source file:com.davidmascharka.lips.MainActivity.java

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);

    // Set the display map option to the appropriate check state
    menu.getItem(3).setChecked(displayMap);
    return true;/* w w w .j  a  v  a 2  s. com*/
}

From source file:im.neon.adapters.VectorMessagesAdapter.java

/**
 * The user taps on the action icon./*from w  w w .j a v a2 s. c  om*/
 * @param event the selected event.
 * @param textMsg the event text
 * @param anchorView the popup anchor.
 */
@SuppressLint("NewApi")
private void onMessageClick(final Event event, final String textMsg, final View anchorView) {
    final PopupMenu popup = (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
            ? new PopupMenu(mContext, anchorView, Gravity.END)
            : new PopupMenu(mContext, anchorView);

    popup.getMenuInflater().inflate(R.menu.vector_room_message_settings, popup.getMenu());

    // force to display the icons
    try {
        Field[] fields = popup.getClass().getDeclaredFields();
        for (Field field : fields) {
            if ("mPopup".equals(field.getName())) {
                field.setAccessible(true);
                Object menuPopupHelper = field.get(popup);
                Class<?> classPopupHelper = Class.forName(menuPopupHelper.getClass().getName());
                Method setForceIcons = classPopupHelper.getMethod("setForceShowIcon", boolean.class);
                setForceIcons.invoke(menuPopupHelper, true);
                break;
            }
        }
    } catch (Exception e) {
        Log.e(LOG_TAG, "onMessageClick : force to display the icons failed " + e.getLocalizedMessage());
    }

    Menu menu = popup.getMenu();

    // hide entries
    for (int i = 0; i < menu.size(); i++) {
        menu.getItem(i).setVisible(false);
    }

    menu.findItem(R.id.ic_action_view_source).setVisible(true);
    menu.findItem(R.id.ic_action_vector_permalink).setVisible(true);

    if (!TextUtils.isEmpty(textMsg)) {
        menu.findItem(R.id.ic_action_vector_copy).setVisible(true);
        menu.findItem(R.id.ic_action_vector_quote).setVisible(true);
    }

    if (event.isUploadingMedias(mMediasCache)) {
        menu.findItem(R.id.ic_action_vector_cancel_upload).setVisible(true);
    }

    if (event.isDownloadingMedias(mMediasCache)) {
        menu.findItem(R.id.ic_action_vector_cancel_download).setVisible(true);
    }

    if (event.canBeResent()) {
        menu.findItem(R.id.ic_action_vector_resend_message).setVisible(true);

        if (event.isUndeliverable() || event.isUnkownDevice()) {
            menu.findItem(R.id.ic_action_vector_redact_message).setVisible(true);
        }
    } else if (event.mSentState == Event.SentState.SENT) {

        // test if the event can be redacted
        boolean canBeRedacted = !mIsPreviewMode
                && !TextUtils.equals(event.getType(), Event.EVENT_TYPE_MESSAGE_ENCRYPTION);

        if (canBeRedacted) {
            // oneself message -> can redact it
            if (TextUtils.equals(event.sender, mSession.getMyUserId())) {
                canBeRedacted = true;
            } else {
                // need the mininum power level to redact an event
                Room room = mSession.getDataHandler().getRoom(event.roomId);

                if ((null != room) && (null != room.getLiveState().getPowerLevels())) {
                    PowerLevels powerLevels = room.getLiveState().getPowerLevels();
                    canBeRedacted = powerLevels.getUserPowerLevel(mSession.getMyUserId()) >= powerLevels.redact;
                }
            }
        }

        menu.findItem(R.id.ic_action_vector_redact_message).setVisible(canBeRedacted);

        if (Event.EVENT_TYPE_MESSAGE.equals(event.getType())) {
            Message message = JsonUtils.toMessage(event.getContentAsJsonObject());

            // share / forward the message
            menu.findItem(R.id.ic_action_vector_share).setVisible(true);
            menu.findItem(R.id.ic_action_vector_forward).setVisible(true);

            // save the media in the downloads directory
            if (Message.MSGTYPE_IMAGE.equals(message.msgtype) || Message.MSGTYPE_VIDEO.equals(message.msgtype)
                    || Message.MSGTYPE_FILE.equals(message.msgtype)) {
                menu.findItem(R.id.ic_action_vector_save).setVisible(true);
            }

            // offer to report a message content
            menu.findItem(R.id.ic_action_vector_report)
                    .setVisible(!mIsPreviewMode && !TextUtils.equals(event.sender, mSession.getMyUserId()));
        }

    }

    // e2e
    menu.findItem(R.id.ic_action_device_verification).setVisible(mE2eIconByEventId.containsKey(event.eventId));

    // display the menu
    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(final MenuItem item) {
            // warn the listener
            if (null != mVectorMessagesAdapterEventsListener) {
                mVectorMessagesAdapterEventsListener.onEventAction(event, textMsg, item.getItemId());
            }

            // disable the selection
            mHighlightedEventId = null;
            notifyDataSetChanged();

            return true;
        }
    });

    // fix an issue reported by GA
    try {
        popup.show();
    } catch (Exception e) {
        Log.e(LOG_TAG, " popup.show failed " + e.getMessage());
    }
}

From source file:universe.constellation.orion.viewer.OrionViewerActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu, menu);
    if (!hasActionBar) {
        for (int i = 0; i < 6; i++) {
            SupportMenuItem item = (SupportMenuItem) menu.getItem(i);
            item.setShowAsAction(SupportMenuItem.SHOW_AS_ACTION_NEVER);
        }//  w  w w .j  a  va  2 s  .  co  m
    }
    return true;
}

From source file:cn.edu.wyu.documentviewer.DocumentsActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    getMenuInflater().inflate(R.menu.activity, menu);

    // Actions are always visible when showing as dialog
    if (mShowAsDialog) {
        for (int i = 0; i < menu.size(); i++) {
            menu.getItem(i).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        }//  ww w.  java  2 s .  c  o  m
    }

    final MenuItem searchMenu = menu.findItem(R.id.menu_search);
    mSearchView = (SearchView) searchMenu.getActionView();
    mSearchView.setOnQueryTextListener(new OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            mState.currentSearch = query;
            mSearchView.clearFocus();
            onCurrentDirectoryChanged(ANIM_NONE);
            return true;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            return false;
        }
    });

    searchMenu.setOnActionExpandListener(new OnActionExpandListener() {
        @Override
        public boolean onMenuItemActionExpand(MenuItem item) {
            return true;
        }

        @Override
        public boolean onMenuItemActionCollapse(MenuItem item) {
            if (mIgnoreNextCollapse) {
                mIgnoreNextCollapse = false;
                return true;
            }

            mState.currentSearch = null;
            onCurrentDirectoryChanged(ANIM_NONE);
            return true;
        }
    });

    mSearchView.setOnCloseListener(new SearchView.OnCloseListener() {
        @Override
        public boolean onClose() {
            if (mIgnoreNextClose) {
                mIgnoreNextClose = false;
                return false;
            }

            mState.currentSearch = null;
            onCurrentDirectoryChanged(ANIM_NONE);
            return false;
        }
    });

    return true;
}

From source file:reportsas.com.formulapp.Formulario.java

public void HabilitarParametros(Menu menu) {
    for (int i = 0; i < encuesta.getParametros().size(); i++) {

        switch (encuesta.getParametros().get(i).getIdParametro()) {
        // Captura GPS
        case 1:/*from w w  w .  j  av a  2s .  c o  m*/

            menu.getItem(2).setVisible(true);
            parametroGPS = new ParametrosRespuesta(1);
            manejador = (LocationManager) getSystemService(LOCATION_SERVICE);
            if (!manejador.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                AlertDialog alert = null;
                final AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setMessage("El sistema GPS esta desactivado, Debe activarlo!").setCancelable(false)
                        .setPositiveButton("Activar GPS", new DialogInterface.OnClickListener() {
                            public void onClick(@SuppressWarnings("unused") final DialogInterface dialog,
                                    @SuppressWarnings("unused") final int id) {
                                startActivity(
                                        new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
                            }
                        });
                alert = builder.create();
                alert.show();
            }
            Criteria criterio = new Criteria();
            criterio.setCostAllowed(false);
            criterio.setAltitudeRequired(false);
            criterio.setAccuracy(Criteria.ACCURACY_FINE);
            proveedor = manejador.getBestProvider(criterio, true);
            Location localizacion = manejador.getLastKnownLocation(proveedor);
            capturarLocalizacion(localizacion);
            manejador.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30000, 0, this);
            Toast toast1;
            if (parametroGPS.getValor().equals("Posicion Desconocida")) {
                toast1 = Toast.makeText(this, "Posicion Desconocida.", Toast.LENGTH_SHORT);

            } else {
                toast1 = Toast.makeText(this, "Localizacin obtenida exitosamente.", Toast.LENGTH_SHORT);

            }

            toast1.show();
            break;
        // Captura Imgen
        case 2:

            menu.getItem(0).setVisible(true);
            break;
        // Lectura de Codigo
        case 3:

            menu.getItem(1).setVisible(true);
            break;

        default:

            break;
        }

    }

}

From source file:com.daiv.android.twitter.ui.drawer_activities.DrawerActivity.java

@Override
public boolean onPrepareOptionsMenu(Menu menu) {

    final int DISMISS = 0;
    final int SEARCH = 1;
    final int COMPOSE = 2;
    final int NOTIFICATIONS = 3;
    final int DM = 4;
    final int SETTINGS = 5;
    final int TOFIRST = 6;
    final int TWEETMARKER = 7;

    try {/*from  ww  w.  ja  va 2  s  .  c  om*/
        searchItem = menu.findItem(R.id.menu_search);
    } catch (Exception e) {
        searchItem = null;
    }

    menu.getItem(TWEETMARKER).setVisible(false);

    if (mDrawerLayout.isDrawerOpen(Gravity.RIGHT) || sharedPrefs.getBoolean("open_interactions", false)) {
        menu.getItem(DISMISS).setVisible(true);

        menu.getItem(SEARCH).setVisible(false);
        menu.getItem(COMPOSE).setVisible(false);
        menu.getItem(DM).setVisible(false);
        menu.getItem(TOFIRST).setVisible(false);

        if (settings.pushNotifications && settings.useInteractionDrawer) {
            menu.getItem(NOTIFICATIONS).setVisible(true);
        } else {
            menu.getItem(NOTIFICATIONS).setVisible(false);
        }

    } else {
        menu.getItem(DISMISS).setVisible(false);

        menu.getItem(SEARCH).setVisible(true);
        menu.getItem(COMPOSE).setVisible(true);
        menu.getItem(DM).setVisible(true);

        if (!settings.pushNotifications || !settings.useInteractionDrawer) {
            menu.getItem(NOTIFICATIONS).setVisible(false);
        } else {
            if (settings.floatingCompose || getResources().getBoolean(R.bool.isTablet)) {
                menu.getItem(NOTIFICATIONS).setVisible(true);
            } else {
                menu.getItem(NOTIFICATIONS).setVisible(false);
            }
        }
    }

    // to first button in overflow instead of the toast
    if (MainDrawerArrayAdapter.current > adapter.pageTypes.size() || (settings.uiExtras && settings.useToast)) {
        menu.getItem(TOFIRST).setVisible(false);
    } else {
        menu.getItem(TOFIRST).setVisible(true);
    }

    if (MainActivity.isPopup) {
        menu.getItem(SETTINGS).setVisible(false); // hide the settings button if the popup is up
        menu.getItem(SEARCH).setVisible(false); // hide the search button in popup

        // disable the left drawer so they can't switch activities in the popup.
        // causes problems with the layouts
        mDrawerLayout.setDrawerLockMode(NotificationDrawerLayout.LOCK_MODE_LOCKED_CLOSED, Gravity.START);
        actionBar.setDisplayShowHomeEnabled(false);
        actionBar.setDisplayHomeAsUpEnabled(false);
        actionBar.setHomeButtonEnabled(false);
    }

    noti = menu.getItem(NOTIFICATIONS);

    if (InteractionsDataSource.getInstance(context).getUnreadCount(settings.currentAccount) > 0) {
        setNotificationFilled(true);
    } else {
        setNotificationFilled(false);
    }

    return true;
}

From source file:pl.edu.agh.mindmapex.gui.MainActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MainActivity.menu = menu;/*  w w w.j  av a 2 s .  c om*/
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.map_menu, menu);
    menu.getItem(1).setVisible(false);
    menu.getItem(2).setVisible(false);
    menu.getItem(3).setVisible(false);
    menu.getItem(4).setVisible(false);
    menu.getItem(5).setVisible(false);
    menu.getItem(6).setVisible(false);
    return super.onCreateOptionsMenu(menu);
}

From source file:cw.kop.autobackground.sources.SourceListFragment.java

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

    // Inflate menu and hold reference in toolbarMenu
    inflater.inflate(R.menu.source_list_menu, menu);
    super.onCreateOptionsMenu(menu, inflater);
    toolbarMenu = menu;/*from   ww  w. ja  v  a2  s.  com*/

    // Sets correct colors of toolbar icons
    int colorFilterInt = AppSettings.getColorFilterInt(appContext);
    Drawable refreshIcon = getResources().getDrawable(R.drawable.ic_refresh_white_24dp);
    Drawable storageIcon = getResources().getDrawable(R.drawable.ic_sort_white_24dp);
    refreshIcon.setColorFilter(colorFilterInt, PorterDuff.Mode.MULTIPLY);
    storageIcon.setColorFilter(colorFilterInt, PorterDuff.Mode.MULTIPLY);
    menu.getItem(0).setIcon(refreshIcon);
    menu.getItem(2).setIcon(storageIcon);

    // Recounts images
    new ImageCountTask().execute();
}

From source file:com.klinker.android.twitter.ui.drawer_activities.DrawerActivity.java

@Override
public boolean onPrepareOptionsMenu(Menu menu) {

    final int DISMISS = 0;
    final int SEARCH = 1;
    final int COMPOSE = 2;
    final int NOTIFICATIONS = 3;
    final int DM = 4;
    final int SETTINGS = 5;
    final int TOFIRST = 6;
    final int TWEETMARKER = 7;

    try {//  www . jav  a2 s  .  c  o  m
        searchItem = menu.findItem(R.id.menu_search);
    } catch (Exception e) {
        searchItem = null;
    }

    menu.getItem(TWEETMARKER).setVisible(false);

    if (mDrawerLayout.isDrawerOpen(Gravity.RIGHT) || sharedPrefs.getBoolean("open_interactions", false)) {
        menu.getItem(DISMISS).setVisible(true);

        menu.getItem(SEARCH).setVisible(false);
        menu.getItem(COMPOSE).setVisible(false);
        menu.getItem(DM).setVisible(false);
        menu.getItem(TOFIRST).setVisible(false);

        if (settings.pushNotifications) {
            menu.getItem(NOTIFICATIONS).setVisible(true);
        } else {
            menu.getItem(NOTIFICATIONS).setVisible(false);
        }

    } else {
        menu.getItem(DISMISS).setVisible(false);

        menu.getItem(SEARCH).setVisible(true);
        menu.getItem(COMPOSE).setVisible(true);
        menu.getItem(DM).setVisible(true);

        if (!settings.pushNotifications) {
            menu.getItem(NOTIFICATIONS).setVisible(false);
        } else {
            if (settings.floatingCompose || getResources().getBoolean(R.bool.isTablet)) {
                menu.getItem(NOTIFICATIONS).setVisible(true);
            } else {
                menu.getItem(NOTIFICATIONS).setVisible(false);
            }
        }
    }

    // to first button in overflow instead of the toast
    if (MainDrawerArrayAdapter.current > 2 || (settings.uiExtras && settings.useToast)) {
        menu.getItem(TOFIRST).setVisible(false);
    } else {
        menu.getItem(TOFIRST).setVisible(true);
    }

    if (MainActivity.isPopup) {
        menu.getItem(SETTINGS).setVisible(false); // hide the settings button if the popup is up
        menu.getItem(SEARCH).setVisible(false); // hide the search button in popup

        // disable the left drawer so they can't switch activities in the popup.
        // causes problems with the layouts
        mDrawerLayout.setDrawerLockMode(NotificationDrawerLayout.LOCK_MODE_LOCKED_CLOSED, Gravity.START);
        actionBar.setDisplayShowHomeEnabled(false);
        actionBar.setDisplayHomeAsUpEnabled(false);
        actionBar.setHomeButtonEnabled(false);
    }

    noti = menu.getItem(NOTIFICATIONS);

    if (InteractionsDataSource.getInstance(context).getUnreadCount(settings.currentAccount) > 0) {
        setNotificationFilled(true);
    } else {
        setNotificationFilled(false);
    }

    return true;
}

From source file:com.gelakinetic.mtgfam.FamiliarActivity.java

/**
 * Called whenever we call supportInvalidateOptionsMenu(). This hides action bar items when the drawer is open
 *
 * @param menu The menu to hide or show items in
 * @return True if the menu is to be displayed, false otherwise
 */// w w  w. j  a v  a2  s. c  o m
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    boolean drawerVisible = mDrawerLayout.isDrawerVisible(mDrawerList);
    mIsMenuVisible = !drawerVisible;
    for (int i = 0; i < menu.size(); i++) {
        menu.getItem(i).setVisible(!drawerVisible);
    }
    return super.onPrepareOptionsMenu(menu);
}