Java Code Examples for android.view.MenuItem.getIcon()

The following are Jave code examples for showing how to use getIcon() of the android.view.MenuItem class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: GitHub   File: MainActivity.java   View Source Code Vote up 6 votes
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    Drawable drawable = item.getIcon();
    if (drawable instanceof Animatable) {
        ((Animatable) drawable).start();
    }
    switch (item.getItemId()) {
        case R.id.action_cut:
            return true;
        case R.id.action_copy:
            return true;
        case R.id.action_share:
            return true;
        case R.id.action_delete:
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}
 
Example 2
Project: boohee_v5.6   File: MenuPopupHelper.java   View Source Code Vote up 6 votes
public boolean onSubMenuSelected(SubMenuBuilder subMenu) {
    if (subMenu.hasVisibleItems()) {
        MenuPopupHelper subPopup = new MenuPopupHelper(this.mContext, subMenu, this.mAnchorView);
        subPopup.setCallback(this.mPresenterCallback);
        boolean preserveIconSpacing = false;
        int count = subMenu.size();
        for (int i = 0; i < count; i++) {
            MenuItem childItem = subMenu.getItem(i);
            if (childItem.isVisible() && childItem.getIcon() != null) {
                preserveIconSpacing = true;
                break;
            }
        }
        subPopup.setForceShowIcon(preserveIconSpacing);
        if (subPopup.tryShow()) {
            if (this.mPresenterCallback != null) {
                this.mPresenterCallback.onOpenSubMenu(subMenu);
            }
            return true;
        }
    }
    return false;
}
 
Example 3
Project: ReColor   File: ReColor.java   View Source Code Vote up 6 votes
/**
 * this method changes the icon color of a MenuItem.
 */
public ReColor setMenuIconColor(MenuItem menuItem, String startingColor, String endingColor, int duration) {

    try {
        if (duration < 16)
            throw new ReColorException("\n \n      duration must at least be 16ms \n ");
        this.startingColor = getValidColor(startingColor, "start");
        this.endingColor = getValidColor(endingColor, "end");
        if (this.startingColor != null && this.endingColor != null) {
            this.menuItem = menuItem;
            menuItemIcon = menuItem.getIcon();
            if (menuItemIcon == null)
                throw new ReColorException("\n \n      menuItem doesn't have an icon \n ");
            stepCount = duration / colorChangeSpeed;
            colorArray = getColorArray(this.startingColor, this.endingColor, stepCount);
            timerHandler.postDelayed(menuIconColorTimerRunnable, 0);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return this;
}
 
Example 4
Project: Xndroid   File: ReadingActivity.java   View Source Code Vote up 6 votes
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.reading, menu);
    MenuItem invert = menu.findItem(R.id.invert_item);
    MenuItem textSize = menu.findItem(R.id.text_size_item);

    int iconColor = ThemeUtils.getIconThemeColor(this, mInvert);

    if (invert != null && invert.getIcon() != null) {
        invert.getIcon().mutate().setColorFilter(iconColor, PorterDuff.Mode.SRC_IN);
    }

    if (textSize != null && textSize.getIcon() != null) {
        textSize.getIcon().mutate().setColorFilter(iconColor, PorterDuff.Mode.SRC_IN);
    }

    return super.onCreateOptionsMenu(menu);
}
 
Example 5
Project: android-mvp-architecture   File: MainActivity.java   View Source Code Vote up 6 votes
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    Drawable drawable = item.getIcon();
    if (drawable instanceof Animatable) {
        ((Animatable) drawable).start();
    }
    switch (item.getItemId()) {
        case R.id.action_cut:
            return true;
        case R.id.action_copy:
            return true;
        case R.id.action_share:
            return true;
        case R.id.action_delete:
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}
 
Example 6
Project: GitHub   File: MainActivity.java   View Source Code Vote up 5 votes
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    Drawable drawable = item.getIcon();
    if (drawable instanceof Animatable) {
        ((Animatable) drawable).start();
    }
    return super.onOptionsItemSelected(item);
}
 
Example 7
Project: MusicX-music-player   File: ToolbarProcessor.java   View Source Code Vote up 5 votes
@SuppressWarnings("unchecked")
public static void tintMenu(@NonNull Toolbar toolbar, @Nullable Menu menu, final @ColorInt int color) {
    try {
        final Field field = Toolbar.class.getDeclaredField("mCollapseIcon");
        field.setAccessible(true);
        Drawable collapseIcon = (Drawable) field.get(toolbar);
        if (collapseIcon != null) {
            field.set(toolbar, TintHelper.createTintedDrawable(collapseIcon, color));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    // credits: https://snow.dog/blog/how-to-dynamicaly-change-android-toolbar-icons-color/
    final PorterDuffColorFilter colorFilter = new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_IN);
    for (int i = 0; i < toolbar.getChildCount(); i++) {
        final View v = toolbar.getChildAt(i);
        // We can't iterate through the toolbar.getMenu() here, because we need the ActionMenuItemView. ATEActionMenuItemView is overriding the item icon tint color.
        if (v instanceof ActionMenuView) {
            for (int j = 0; j < ((ActionMenuView) v).getChildCount(); j++) {
                final View innerView = ((ActionMenuView) v).getChildAt(j);
                if (innerView instanceof ActionMenuItemView) {
                    int drawablesCount = ((ActionMenuItemView) innerView).getCompoundDrawables().length;
                    for (int k = 0; k < drawablesCount; k++) {
                        if (((ActionMenuItemView) innerView).getCompoundDrawables()[k] != null) {
                            ((ActionMenuItemView) innerView).getCompoundDrawables()[k].setColorFilter(colorFilter);
                        }
                    }
                }
            }
        }
    }

    if (menu == null)
        menu = toolbar.getMenu();
    if (menu != null && menu.size() > 0) {
        for (int i = 0; i < menu.size(); i++) {
            final MenuItem item = menu.getItem(i);
            // We must iterate through the toolbar.getMenu() too, to keep the tint when resuming the paused activity.
            if (item.getIcon() != null) {
                item.setIcon(TintHelper.createTintedDrawable(item.getIcon(), color));
            }
            // Search view theming
            if (item.getActionView() != null && (item.getActionView() instanceof android.widget.SearchView || item.getActionView() instanceof android.support.v7.widget.SearchView)) {
                SearchViewTintUtil.setSearchViewContentColor(item.getActionView(), color);
            }
        }
    }
}
 
Example 8
Project: boohee_v5.6   File: UCropActivity.java   View Source Code Vote up 5 votes
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.ucrop_menu_activity, menu);
    MenuItem next = menu.findItem(R.id.menu_crop);
    Drawable defaultIcon = next.getIcon();
    if (defaultIcon != null) {
        defaultIcon.mutate();
        defaultIcon.setColorFilter(this.mToolbarTextColor, Mode.SRC_ATOP);
        next.setIcon(defaultIcon);
    }
    return true;
}
 
Example 9
Project: VideoDownloader-Android   File: iUtils.java   View Source Code Vote up 5 votes
public static void tintMenuIcon(Context context, MenuItem item, int color) {
    Drawable drawable = item.getIcon();
    if (drawable != null) {
        // If we don't mutate the drawable, then all drawable's with this id will have a color
        // filter applied to it.
        drawable.mutate();
        drawable.setColorFilter(ContextCompat.getColor(context, color), PorterDuff.Mode.SRC_ATOP);
    }
}
 
Example 10
Project: Musicoco   File: SheetDetailActivity.java   View Source Code Vote up 5 votes
private void updateToolbarColor() {

        if (toolbar == null || collapsingToolbarLayout == null) {
            return;
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

            int[] colors = ColorUtils.get2ToolbarTextColors(this);

            int mainTC = colors[0];
            collapsingToolbarLayout.setCollapsedTitleTextColor(mainTC);
            Drawable navD = toolbar.getNavigationIcon();
            if (navD != null) {
                navD.setTint(mainTC);
            }

            int menuCount = menu.size();
            int mainC = colors[0];
            for (int i = 0; i < menuCount; i++) {
                MenuItem item = menu.getItem(i);
                if (item != null) {
                    Drawable icon = item.getIcon();
                    if (icon != null) {
                        icon.setTint(mainC);
                    }
                }
            }
        }
    }
 
Example 11
Project: QuickActionView   File: QuickActionView.java   View Source Code Vote up 5 votes
/**
 * Add actions to the QuickActionView from the given menu resource id.
 *
 * @param menuId menu resource id
 * @return the QuickActionView
 */
public QuickActionView addActions(@MenuRes int menuId) {
    Menu menu = new MenuBuilder(mContext);
    new MenuInflater(mContext).inflate(menuId, menu);
    for (int i = 0; i < menu.size(); i++) {
        MenuItem item = menu.getItem(i);
        Action action = new Action(item.getItemId(), item.getIcon(), item.getTitle());
        addAction(action);
    }
    return this;
}
 
Example 12
Project: MultiTypeRecyclerViewAdapter   File: NormalActivity.java   View Source Code Vote up 5 votes
@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.menu, menu);
    MenuItem searchItem = menu.findItem(R.id.search);
    mSearchItemIcon = searchItem.getIcon();
    mSearchView.setMenuItem(searchItem);
    return true;
}
 
Example 13
Project: chromium-for-android-56-debug-video   File: Preferences.java   View Source Code Vote up 5 votes
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    if (menu.size() == 1) {
        MenuItem item = menu.getItem(0);
        if (item.getIcon() != null) item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
    }
    return super.onPrepareOptionsMenu(menu);
}
 
Example 14
Project: Camera-Roll-Android-App   File: AlbumActivity.java   View Source Code Vote up 5 votes
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.album, menu);
    this.menu = menu;

    if (pick_photos) {
        menu.findItem(R.id.share).setVisible(false);
        menu.findItem(R.id.exclude).setVisible(false);
        menu.findItem(R.id.pin).setVisible(false);
        menu.findItem(R.id.rename).setVisible(false);
        menu.findItem(R.id.copy).setVisible(false);
        menu.findItem(R.id.move).setVisible(false);
    } else if (album != null) {
        setupMenu();
    }

    int sort_by = Settings.getInstance(this).sortAlbumBy();
    if (sort_by == SortUtil.BY_DATE) {
        menu.findItem(R.id.sort_by_date).setChecked(true);
    } else if (sort_by == SortUtil.BY_NAME) {
        menu.findItem(R.id.sort_by_name).setChecked(true);
    }

    MenuItem selectAll = menu.findItem(R.id.select_all);
    Drawable d = selectAll.getIcon();
    DrawableCompat.wrap(d);
    DrawableCompat.setTint(d, accentTextColor);
    DrawableCompat.unwrap(d);

    return super.onCreateOptionsMenu(menu);
}
 
Example 15
Project: AndelaTrackChallenge   File: Easel.java   View Source Code Vote up 5 votes
/**
 * Tint all menu items within a menu to be a certain color. Note that this does not tint the
 * overflow menu. Call {@link #tintOverflow(Toolbar, int)} for that.
 *
 * @param menu  the menu
 * @param color the color
 */
public static void tint(@NonNull Menu menu, @ColorInt int color) {
    for (int i = 0; i < menu.size(); i++) {
        MenuItem menuItem = menu.getItem(i);
        if (menuItem.getIcon() != null) {
            tint(menuItem, color);
        }
    }
}
 
Example 16
Project: minion-android   File: CompileActivity.java   View Source Code Vote up 5 votes
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_compile, menu);
    for (int c = 0; c < menu.size(); c++) {
        MenuItem item = menu.getItem(c);
        Drawable drawable = item.getIcon();
        drawable = DrawableCompat.wrap(drawable);
        DrawableCompat.setTint(drawable, ContextCompat.getColor(this, R.color.colorAccent));
        item.setIcon(drawable);
    }
    return true;
}
 
Example 17
Project: Flashcard-Maker-Android   File: Utils.java   View Source Code Vote up 4 votes
public static void tintMenuIcon(Context context, MenuItem item,@ColorInt int color) {
    Drawable normalDrawable = item.getIcon();
    Drawable wrapDrawable = DrawableCompat.wrap(normalDrawable);
    DrawableCompat.setTint(wrapDrawable, color);
    item.setIcon(wrapDrawable);
}
 
Example 18
Project: Camera-Roll-Android-App   File: MainActivity.java   View Source Code Vote up 4 votes
@SuppressWarnings("unchecked")
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.camera_shortcut:
            Drawable d = item.getIcon();
            if (d instanceof Animatable
                    && !((Animatable) d).isRunning()) {
                ((Animatable) d).start();
                fabClicked(null);
            }
            break;
        case R.id.refresh:
            refreshPhotos();
            break;
        case R.id.hiddenFolders:
            hiddenFolders = Settings.getInstance(this)
                    .setHiddenFolders(this, !hiddenFolders);
            item.setChecked(hiddenFolders);
            refreshPhotos();
            break;
        case R.id.file_explorer:
            startActivity(new Intent(this, FileExplorerActivity.class),
                    ActivityOptionsCompat.makeSceneTransitionAnimation(this).toBundle());
            break;
        case R.id.settings:
            startActivityForResult(new Intent(this, SettingsActivity.class),
                    SETTINGS_REQUEST_CODE);
            break;
        case R.id.about:
            startActivity(new Intent(this, AboutActivity.class),
                    ActivityOptionsCompat.makeSceneTransitionAnimation(this).toBundle());
            break;
        case R.id.sort_by_name:
        case R.id.sort_by_size:
        case R.id.sort_by_most_recent:
            item.setChecked(true);

            int sort_by;
            if (item.getItemId() == R.id.sort_by_name) {
                sort_by = SortUtil.BY_NAME;
            } else if (item.getItemId() == R.id.sort_by_size) {
                sort_by = SortUtil.BY_SIZE;
            } else {
                sort_by = SortUtil.BY_DATE;
            }

            Settings.getInstance(this).sortAlbumsBy(this, sort_by);
            resortAlbums();

            break;
        default:
            break;
    }
    return super.onOptionsItemSelected(item);
}
 
Example 19
Project: pm-home-station   File: MainActivity.java   View Source Code Vote up 4 votes
public static void tintMenuItem(MenuItem item) {
    Drawable icon = item.getIcon();
    icon.mutate();
    icon.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_IN);
}
 
Example 20
Project: MovingGdufe-Android   File: RefreshActionItem.java   View Source Code Vote up 4 votes
public void setMenuItem(MenuItem menuItem) {
    this.mMenuItem = menuItem;
    if (menuItem.getIcon() != null) {
        mRefreshButton.setImageDrawable(mMenuItem.getIcon());
    }
}