Example usage for android.view MenuItem getIcon

List of usage examples for android.view MenuItem getIcon

Introduction

In this page you can find the example usage for android.view MenuItem getIcon.

Prototype

public Drawable getIcon();

Source Link

Document

Returns the icon for this item as a Drawable (getting it from resources if it hasn't been loaded before).

Usage

From source file:de.bahnhoefe.deutschlands.bahnhofsfotos.DetailsActivity.java

private void disableMenuItem(Menu menu, int id) {
    MenuItem menuItem = menu.findItem(id).setEnabled(false);
    menuItem.getIcon().mutate();
    menuItem.getIcon().setColorFilter(WHITE, PorterDuff.Mode.SRC_ATOP);
    menuItem.getIcon().setAlpha(ALPHA);//ww w  . j  av  a  2s . c  o m
}

From source file:de.bahnhoefe.deutschlands.bahnhofsfotos.DetailsActivity.java

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    menu.clear();/* ww  w . j  a v a 2  s .c  o  m*/
    getMenuInflater().inflate(R.menu.details, menu);
    MenuItem navToStation = menu.findItem(R.id.nav_to_station);
    navToStation.getIcon().mutate();
    navToStation.getIcon().setColorFilter(WHITE, PorterDuff.Mode.SRC_ATOP);

    if (localFotoUsed) {
        enableMenuItem(menu, R.id.photo_upload);
    } else {
        disableMenuItem(menu, R.id.photo_upload);
    }

    if (localFotoUsed || bahnhof.hasPhoto()) {
        enableMenuItem(menu, R.id.share_photo);
    } else {
        disableMenuItem(menu, R.id.share_photo);
    }

    return super.onPrepareOptionsMenu(menu);
}

From source file:com.sinyuk.jianyimaterial.widgets.FloatingToolbar.java

private void addMenuItems() {

    if (mMenu == null) {
        mMenu = new MenuBuilder(getContext());
        new SupportMenuInflater(getContext()).inflate(mMenuRes, mMenu);
    }/*from   w  ww. ja v  a 2s.com*/

    LinearLayoutCompat.LayoutParams layoutParams = new LinearLayoutCompat.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT, 1);

    setWeightSum(mMenu.size());

    for (int i = 0; i < mMenu.size(); i++) {
        MenuItem item = mMenu.getItem(i);
        AppCompatImageButton imageButton = new AppCompatImageButton(getContext());
        imageButton.setId(genViewId());
        imageButton.setBackgroundResource(mItemBackground);
        imageButton.setImageDrawable(item.getIcon());
        imageButton.setOnClickListener(this);
        imageButton.setOnLongClickListener(this);
        mMenuLayout.addView(imageButton, layoutParams);
    }

}

From source file:com.github.rubensousa.floatingtoolbar.FloatingToolbar.java

/**
 * Place the menu items with icons inside a horizontal LinearLayout
 *//*w w  w .  jav  a  2s  . com*/
private void addMenuItems() {

    if (mMenu == null) {
        mMenu = new MenuBuilder(getContext());
        new SupportMenuInflater(getContext()).inflate(mMenuRes, mMenu);
    }

    LinearLayoutCompat.LayoutParams layoutParams = new LinearLayoutCompat.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT, 1);

    setWeightSum(mMenu.size());

    for (int i = 0; i < mMenu.size(); i++) {
        MenuItem item = mMenu.getItem(i);
        if (item.isVisible()) {
            AppCompatImageButton imageButton = new AppCompatImageButton(getContext());
            //noinspection ResourceType
            imageButton.setId(item.getItemId() == Menu.NONE ? genViewId() : item.getItemId());
            imageButton.setBackgroundResource(mItemBackground);
            imageButton.setImageDrawable(item.getIcon());
            imageButton.setOnClickListener(this);
            imageButton.setOnLongClickListener(this);
            imageButton.setTag(item);
            mMenuLayout.addView(imageButton, layoutParams);
        }
    }
}

From source file:com.cw.litenote.note.Note.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    //      System.out.println("Note / _onCreateOptionsMenu");

    // inflate menu
    getMenuInflater().inflate(R.menu.pager_menu, menu);
    mMenu = menu;//from  w ww.  j a  va 2  s .  c  o m

    // menu item: checked status
    // get checked or not
    int isChecked = mDb_page.getNoteMarking(NoteUi.getFocus_notePos(), true);
    if (isChecked == 0)
        menu.findItem(R.id.VIEW_NOTE_CHECK).setIcon(R.drawable.btn_check_off_holo_dark);
    else
        menu.findItem(R.id.VIEW_NOTE_CHECK).setIcon(R.drawable.btn_check_on_holo_dark);

    // menu item: view mode
    markCurrentSelected(menu.findItem(R.id.VIEW_ALL), "ALL");
    markCurrentSelected(menu.findItem(R.id.VIEW_PICTURE), "PICTURE_ONLY");
    markCurrentSelected(menu.findItem(R.id.VIEW_TEXT), "TEXT_ONLY");

    // menu item: previous
    MenuItem itemPrev = menu.findItem(R.id.ACTION_PREVIOUS);
    itemPrev.setEnabled(viewPager.getCurrentItem() > 0);
    itemPrev.getIcon().setAlpha(viewPager.getCurrentItem() > 0 ? 255 : 30);

    // menu item: Next or Finish
    MenuItem itemNext = menu.findItem(R.id.ACTION_NEXT);
    itemNext.setTitle((viewPager.getCurrentItem() == mPagerAdapter.getCount() - 1)
            ? R.string.view_note_slide_action_finish
            : R.string.view_note_slide_action_next);

    // set Disable and Gray for Last item
    boolean isLastOne = (viewPager.getCurrentItem() == (mPagerAdapter.getCount() - 1));
    if (isLastOne)
        itemNext.setEnabled(false);

    itemNext.getIcon().setAlpha(isLastOne ? 30 : 255);

    return true;
}

From source file:org.mariotaku.twidere.activity.ComposeActivity.java

private void setMenu() {
    final Menu menu = mMenuBar.getMenu();
    final int activated_color = getResources().getColor(R.color.holo_blue_bright);
    final MenuItem itemAddImage = menu.findItem(MENU_ADD_IMAGE);
    final Drawable iconAddImage = itemAddImage.getIcon().mutate();
    if (mIsImageAttached && !mIsPhotoAttached) {
        iconAddImage.setColorFilter(activated_color, Mode.MULTIPLY);
        itemAddImage.setTitle(R.string.remove_image);
    } else {//  w  ww  .j  a  v  a 2  s.co m
        iconAddImage.clearColorFilter();
        itemAddImage.setTitle(R.string.add_image);
    }
    final MenuItem itemTakePhoto = menu.findItem(MENU_TAKE_PHOTO);
    final Drawable iconTakePhoto = itemTakePhoto.getIcon().mutate();
    if (!mIsImageAttached && mIsPhotoAttached) {
        iconTakePhoto.setColorFilter(activated_color, Mode.MULTIPLY);
        itemTakePhoto.setTitle(R.string.remove_photo);
    } else {
        iconTakePhoto.clearColorFilter();
        itemTakePhoto.setTitle(R.string.take_photo);
    }
    final MenuItem itemAttachLocation = menu.findItem(MENU_ADD_LOCATION);
    final Drawable iconAttachLocation = itemAttachLocation.getIcon().mutate();
    final boolean attach_location = mPreferences.getBoolean(PREFERENCE_KEY_ATTACH_LOCATION, false);
    if (attach_location && getLocation()) {
        iconAttachLocation.setColorFilter(activated_color, Mode.MULTIPLY);
        itemAttachLocation.setTitle(R.string.remove_location);
    } else {
        mPreferences.edit().putBoolean(PREFERENCE_KEY_ATTACH_LOCATION, false).commit();
        iconAttachLocation.clearColorFilter();
        itemAttachLocation.setTitle(R.string.add_location);
    }
    final MenuItem itemMore = menu.findItem(R.id.more_submenu);
    final MenuItem itemDrafts = menu.findItem(MENU_DRAFTS);
    final MenuItem itemToggleSensitive = menu.findItem(MENU_TOGGLE_SENSITIVE);
    if (itemMore != null) {
        if (itemDrafts != null) {
            final Cursor drafts_cur = getContentResolver().query(Drafts.CONTENT_URI, new String[0], null, null,
                    null);
            final Drawable iconMore = itemMore.getIcon().mutate();
            final Drawable iconDrafts = itemDrafts.getIcon().mutate();
            if (drafts_cur.getCount() > 0) {
                iconMore.setColorFilter(activated_color, Mode.MULTIPLY);
                iconDrafts.setColorFilter(activated_color, Mode.MULTIPLY);
            } else {
                iconMore.clearColorFilter();
                iconDrafts.clearColorFilter();
            }
            drafts_cur.close();
        }
        if (itemToggleSensitive != null) {
            final boolean has_media = (mIsImageAttached || mIsPhotoAttached) && mImageUri != null;
            itemToggleSensitive.setVisible(has_media);
            if (has_media) {
                final Drawable iconToggleSensitive = itemToggleSensitive.getIcon().mutate();
                if (mIsPossiblySensitive) {
                    itemToggleSensitive.setTitle(R.string.remove_sensitive_mark);
                    iconToggleSensitive.setColorFilter(activated_color, Mode.MULTIPLY);
                } else {
                    itemToggleSensitive.setTitle(R.string.mark_as_sensitive);
                    iconToggleSensitive.clearColorFilter();
                }
            }
        }
    }
    mMenuBar.invalidate();
    invalidateSupportOptionsMenu();
}

From source file:com.egoclean.testpregnancy.util.ActivityHelper.java

/**
 * Adds an action button to the compatibility action bar, using menu information from a
 * {@link android.view.MenuItem}. If the menu item ID is <code>menu_refresh</code>, the menu item's state
 * can be changed to show a loading spinner using
 * {@link ActivityHelper#setRefreshActionButtonCompatState(boolean)}.
 *//*  w w  w . java 2s  .  co  m*/
private View addActionButtonCompatFromMenuItem(final MenuItem item) {
    final ViewGroup actionBar = getActionBarCompat();
    if (actionBar == null) {
        return null;
    }

    // Create the separator
    ImageView separator = new ImageView(mActivity, null, R.attr.actionbarCompatSeparatorStyle);
    separator.setLayoutParams(new ViewGroup.LayoutParams(2, ViewGroup.LayoutParams.FILL_PARENT));

    // Create the button
    ImageButton actionButton = new ImageButton(mActivity, null, R.attr.actionbarCompatButtonStyle);
    actionButton.setId(item.getItemId());
    actionButton.setLayoutParams(new ViewGroup.LayoutParams(
            (int) mActivity.getResources().getDimension(R.dimen.actionbar_compat_height),
            ViewGroup.LayoutParams.FILL_PARENT));
    actionButton.setImageDrawable(item.getIcon());
    actionButton.setScaleType(ImageView.ScaleType.CENTER);
    actionButton.setContentDescription(item.getTitle());
    actionButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            mActivity.onMenuItemSelected(Window.FEATURE_OPTIONS_PANEL, item);
        }
    });

    actionBar.addView(separator);
    actionBar.addView(actionButton);

    if (item.getItemId() == R.id.menu_refresh) {
        // Refresh buttons should be stateful, and allow for indeterminate progress indicators,
        // so add those.
        int buttonWidth = mActivity.getResources().getDimensionPixelSize(R.dimen.actionbar_compat_height);
        int buttonWidthDiv3 = buttonWidth / 3;
        ProgressBar indicator = new ProgressBar(mActivity, null, R.attr.actionbarCompatProgressIndicatorStyle);
        LinearLayout.LayoutParams indicatorLayoutParams = new LinearLayout.LayoutParams(buttonWidthDiv3,
                buttonWidthDiv3);
        indicatorLayoutParams.setMargins(buttonWidthDiv3, buttonWidthDiv3, buttonWidth - 2 * buttonWidthDiv3,
                0);
        indicator.setLayoutParams(indicatorLayoutParams);
        indicator.setVisibility(View.GONE);
        indicator.setId(R.id.menu_refresh_progress);
        actionBar.addView(indicator);
    }

    return actionButton;
}

From source file:com.veniosg.dir.android.fragment.SimpleFileListFragment.java

@Override
public void onPrepareOptionsMenu(Menu menu) {
    if (getActivity() == null) {
        return;//from w w w.  j a v  a2 s .  c  o m
    }

    boolean showMediaScanMenuItem = getMediaScanFromPreference(getActivity());
    if (hasScanner() && !isScannerRunning() && showMediaScanMenuItem) {
        // We only know about ".nomedia" once scanning is finished.
        boolean noMedia = isMediaScannerDisabledForPath();
        menu.findItem(R.id.menu_media_scan_include).setVisible(noMedia);
        menu.findItem(R.id.menu_media_scan_exclude).setVisible(!noMedia);
    } else {
        menu.findItem(R.id.menu_media_scan_include).setVisible(false);
        menu.findItem(R.id.menu_media_scan_exclude).setVisible(false);
    }

    CopyHelper copyHelper = ((FileManagerApplication) getActivity().getApplication()).getCopyHelper();
    MenuItem pasteAction = menu.findItem(R.id.menu_paste);
    if (copyHelper.canPaste()) {
        int stringResource = (copyHelper.getOperationType() == COPY ? R.plurals.menu_copy_items_to
                : R.plurals.menu_move_items_to);
        pasteAction.setTitle(getResources().getQuantityString(stringResource, copyHelper.getItemCount(),
                copyHelper.getItemCount()));

        pasteAction.getIcon().setLevel(copyHelper.getItemCount());
        pasteAction.setVisible(true);
        menu.findItem(R.id.menu_clear_clipboard).setVisible(true);
    } else {
        pasteAction.setVisible(false);
        menu.findItem(R.id.menu_clear_clipboard).setVisible(false);
    }
}

From source file:com.chatwing.whitelabel.managers.ChatboxModeManager.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    AppCompatActivity activity = mActivityDelegate.getActivity();
    final DrawerLayout drawerLayout = mActivityDelegate.getDrawerLayout();
    activity.getMenuInflater().inflate(R.menu.chatbox_menu, menu);
    MenuItem onlineUsersItem = menu.findItem(R.id.online_users);
    mediaAddItem = menu.findItem(R.id.audio_add);

    /**/*ww  w  . j a v a  2  s . c o m*/
     * Create badge view for online user item
     */
    ImageButton iconView = new ImageButton(activity, null, R.style.Widget_AppCompat_ActionButton);
    iconView.setImageDrawable(onlineUsersItem.getIcon());
    iconView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (drawerLayout.isDrawerOpen(DRAWER_GRAVITY_ONLINE_USER)) {
                drawerLayout.closeDrawer(DRAWER_GRAVITY_ONLINE_USER);
            } else {
                drawerLayout.openDrawer(DRAWER_GRAVITY_ONLINE_USER);
            }
        }
    });

    // The badge view requires target view (iconView in this case)
    // to have a ViewGroup parent
    LinearLayout container = new LinearLayout(activity);
    container.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT));
    container.addView(iconView);

    Resources res = activity.getResources();
    mOnlineUsersBadgeView = new BadgeView(activity, iconView);
    mOnlineUsersBadgeView.setBadgePosition(BadgeView.POSITION_TOP_RIGHT);
    mOnlineUsersBadgeView.setTextSize(TypedValue.COMPLEX_UNIT_SP,
            res.getDimension(R.dimen.badge_view_text_size));
    mOnlineUsersBadgeView.setBadgeMargin(res.getDimensionPixelSize(R.dimen.default_margin));
    onlineUsersItem.setActionView(container);
    mOnlineUsersBadgeView
            .setBadgeBackgroundColor(mActivityDelegate.getActivity().getResources().getColor(R.color.accent));

    return true;
}

From source file:org.totschnig.myexpenses.fragment.TransactionList.java

@Override
public void onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);
    if (mAccount == null || getActivity() == null) {
        //mAccount seen in report 3331195c529454ca6b25a4c5d403beda
        //getActivity seen in report 68a501c984bdfcc95b40050af4f815bf
        return;/* w w w.  jav  a2s. c o  m*/
    }
    MenuItem searchMenu = menu.findItem(R.id.SEARCH_COMMAND);
    if (searchMenu != null) {
        String title;
        Drawable searchMenuIcon = searchMenu.getIcon();
        if (!mFilter.isEmpty()) {
            if (searchMenuIcon != null) {
                searchMenuIcon.setColorFilter(Color.GREEN, PorterDuff.Mode.MULTIPLY);
            } else {
                AcraHelper.report(new Exception("Search menu icon not found"));
            }
            searchMenu.setChecked(true);
            title = mAccount.label + " ( " + mFilter.prettyPrint() + " )";
        } else {
            if (searchMenuIcon != null) {
                searchMenuIcon.setColorFilter(null);
            } else {
                AcraHelper.report(new Exception("Search menu icon not found"));
            }
            searchMenu.setChecked(false);
            title = mAccount.label;
        }
        ((MyExpenses) getActivity()).setTitle(title);
        SubMenu filterMenu = searchMenu.getSubMenu();
        for (int i = 0; i < filterMenu.size(); i++) {
            MenuItem filterItem = filterMenu.getItem(i);
            boolean enabled = true;
            switch (filterItem.getItemId()) {
            case R.id.FILTER_CATEGORY_COMMAND:
                enabled = mappedCategories;
                break;
            case R.id.FILTER_STATUS_COMMAND:
                enabled = !mAccount.type.equals(AccountType.CASH);
                break;
            case R.id.FILTER_PAYEE_COMMAND:
                enabled = mappedPayees;
                break;
            case R.id.FILTER_METHOD_COMMAND:
                enabled = mappedMethods;
                break;
            case R.id.FILTER_TRANSFER_COMMAND:
                enabled = hasTransfers;
                break;
            }
            Criteria c = mFilter.get(filterItem.getItemId());
            Utils.menuItemSetEnabledAndVisible(filterItem, enabled || c != null);
            if (c != null) {
                filterItem.setChecked(true);
                filterItem.setTitle(c.prettyPrint());
            }
        }
    } else {
        AcraHelper.report(new Exception("Search menu not found"));
    }
}