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

The following examples show how to use android.view.MenuItem#getIcon() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EBookDetailActivity.java    From MaterialHome with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            startActivity(new Intent(this, MainActivity.class));
            return true;
        case R.id.action_share:
            StringBuilder sb = new StringBuilder();
            sb.append(getString(R.string.your_friend));
            sb.append(getString(R.string.share_book_1));
            sb.append(bookInfo.getTitle());
            sb.append(getString(R.string.share_book_2));
            UIUtils.share(this, sb.toString(), null);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                if (item.getIcon() instanceof Animatable) {
                    ((Animatable) item.getIcon()).start();
                }
            }
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}
 
Example 2
Source File: ReadingActivity.java    From Xndroid with GNU General Public License v3.0 6 votes vote down vote up
@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 3
Source File: AppMenuAdapter.java    From delion with Apache License 2.0 6 votes vote down vote up
private void setupStandardMenuItemViewHolder(StandardMenuItemViewHolder holder,
        View convertView, final MenuItem item) {
    // Set up the icon.
    Drawable icon = item.getIcon();
    holder.image.setImageDrawable(icon);
    holder.image.setVisibility(icon == null ? View.GONE : View.VISIBLE);
    holder.image.setChecked(item.isChecked());
    holder.text.setText(item.getTitle());
    holder.text.setContentDescription(item.getTitleCondensed());

    boolean isEnabled = item.isEnabled();
    // Set the text color (using a color state list).
    holder.text.setEnabled(isEnabled);
    // This will ensure that the item is not highlighted when selected.
    convertView.setEnabled(isEnabled);

    convertView.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            mAppMenu.onItemClick(item);
        }
    });
}
 
Example 4
Source File: LeftNavigationController.java    From Musicoco with Apache License 2.0 6 votes vote down vote up
@Override
public void themeChange(ThemeEnum themeEnum, int[] colors) {
    ThemeEnum th = appPreference.getTheme();
    int[] cs = ColorUtils.get10ThemeColors(activity, th);
    int mainBC = cs[3];
    int mainTC = cs[5];
    int vicTC = cs[6];
    int accentC = cs[2];

    navigationView.setItemTextColor(ColorStateList.valueOf(mainTC));
    navigationView.setBackgroundColor(mainBC);

    updateSwitchMenuIconAndText();

    Menu menu = navigationView.getMenu();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        for (int i = 0; i < menu.size(); i++) {
            MenuItem item = menu.getItem(0);
            Drawable icon = item.getIcon();
            if (icon != null) {
                icon.setTint(accentC);
            }
        }
    }
}
 
Example 5
Source File: BaseActivity.java    From Infinity-For-Reddit with GNU Affero General Public License v3.0 6 votes vote down vote up
@SuppressLint("RestrictedApi")
protected boolean applyMenuItemTheme(Menu menu) {
    if (customThemeWrapper != null) {
        int size = Math.min(menu.size(), 2);
        for (int i = 0; i < size; i++) {
            MenuItem item = menu.getItem(i);
            if (((MenuItemImpl) item).requestsActionButton()) {
                Drawable drawable = item.getIcon();
                if (drawable != null) {
                    DrawableCompat.setTint(drawable, customThemeWrapper.getToolbarPrimaryTextAndIconColor());
                    item.setIcon(drawable);
                }
            }
        }
    }
    return true;
}
 
Example 6
Source File: SweetSheet.java    From MousePaint with MIT License 6 votes vote down vote up
/**
 * 当前只处理一级的菜单
 *
 * @param menu
 * @return
 */
private List<MenuEntity> getMenuEntityFormMenuRes(Menu menu) {

    List<MenuEntity> list = new ArrayList<>();
    for (int i = 0; i < menu.size(); i++) {
        MenuItem item = menu.getItem(i);

        if (item.isVisible()) {
            MenuEntity itemEntity = new MenuEntity();
            itemEntity.title = item.getTitle().toString();
            itemEntity.icon = item.getIcon();
            list.add(itemEntity);
        }
    }
    return list;
}
 
Example 7
Source File: MBaseSimpleActivity.java    From a with GNU General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
//@Override
public boolean onMenuOpened111(int featureId, Menu menu) {
    if (menu != null) {
        //展开菜单显示图标
        if (menu.getClass().getSimpleName().equalsIgnoreCase("MenuBuilder")) {
            try {
                Method method = menu.getClass().getDeclaredMethod("setOptionalIconsVisible", Boolean.TYPE);
                method.setAccessible(true);
                method.invoke(menu, true);
                method = menu.getClass().getDeclaredMethod("getNonActionItems");
                ArrayList<MenuItem> menuItems = (ArrayList<MenuItem>) method.invoke(menu);
                if (!menuItems.isEmpty()) {
                    for (MenuItem menuItem : menuItems) {
                        Drawable drawable = menuItem.getIcon();
                        if (drawable != null) {
                            drawable.mutate();
                            drawable.setColorFilter(getResources().getColor(R.color.tv_text_default), PorterDuff.Mode.SRC_ATOP);
                        }
                    }
                }
            } catch (Exception ignored) {
            }
        }

    }
    return super.onMenuOpened(featureId, menu);
}
 
Example 8
Source File: Preferences.java    From delion with Apache License 2.0 5 votes vote down vote up
@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 9
Source File: BottomNavigationView.java    From Carbon with Apache License 2.0 5 votes vote down vote up
public Item(MenuItem menuItem) {
    id = menuItem.getItemId();
    try {   // breaks preview
        this.icon = menuItem.getIcon();
    } catch (Exception e) {
    }
    this.text = menuItem.getTitle();
    iconTint = MenuItemCompat.getIconTintList(menuItem);
}
 
Example 10
Source File: ExifEditorActivity.java    From Camera-Roll-Android-App with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.exif_editor, menu);
    this.menu = menu;

    MenuItem save = menu.findItem(R.id.save);
    save.setVisible(editedItems.size() > 0);
    Drawable d = save.getIcon();
    DrawableCompat.wrap(d);
    DrawableCompat.setTint(d, textColorSecondary);
    DrawableCompat.unwrap(d);
    save.setIcon(d);
    return super.onCreateOptionsMenu(menu);
}
 
Example 11
Source File: UiUtils.java    From PopularMovies with MIT License 5 votes vote down vote up
public static void tintMenuIcon(Context context, MenuItem item, @ColorRes int color) {
    Drawable itemIcon = item.getIcon();
    Drawable iconWrapper = DrawableCompat.wrap(itemIcon);
    DrawableCompat.setTint(iconWrapper, context.getResources().getColor(color));

    item.setIcon(iconWrapper);
}
 
Example 12
Source File: ToolbarContentTintHelper.java    From APlayer with GNU General Public License v3.0 5 votes vote down vote up
@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();
  }

  if (menu != null && menu.size() > 0) {
    for (int i = 0; i < menu.size(); i++) {
      final MenuItem item = menu.getItem(i);
      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 13
Source File: NormalActivity.java    From MultiTypeRecyclerViewAdapter with Apache License 2.0 5 votes vote down vote up
@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 14
Source File: MenuUtils.java    From Forage with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Tints the color of the icon of the specified MenuItem.
 * See http://stackoverflow.com/questions/28219178/toolbar-icon-tinting-on-android
 *
 * @param color Color to tint
 * @param item  MenuItem to tint.
 */
public static void tintMenuItemIcon(@ColorInt int color, MenuItem item) {
    final Drawable drawable = item.getIcon();
    if (drawable != null) {
        final Drawable wrapped = DrawableCompat.wrap(drawable);
        drawable.mutate();

        DrawableCompat.setTint(wrapped, color);
        item.setIcon(drawable);
    }
}
 
Example 15
Source File: FloatingActionMenu.java    From Carbon with Apache License 2.0 4 votes vote down vote up
public Item(MenuItem item) {
    icon = item.getIcon();
    tint = MenuItemCompat.getIconTintList(item);
    enabled = item.isEnabled();
    title = item.getTitle();
}
 
Example 16
Source File: MainActivity.java    From Camera-Roll-Android-App with Apache License 2.0 4 votes vote down vote up
@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 17
Source File: MenuMainDemoFragment.java    From material-components-android with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("RestrictTo")
private void showMenu(View v, @MenuRes int menuRes) {
  PopupMenu popup = new PopupMenu(getContext(), v);
  // Inflating the Popup using xml file
  popup.getMenuInflater().inflate(menuRes, popup.getMenu());
  // There is no public API to make icons show on menus.
  // IF you need the icons to show this works however it's discouraged to rely on library only
  // APIs since they might disappear in future versions.
  if (popup.getMenu() instanceof MenuBuilder) {
    MenuBuilder menuBuilder = (MenuBuilder) popup.getMenu();
    //noinspection RestrictedApi
    menuBuilder.setOptionalIconsVisible(true);
    for (MenuItem item : menuBuilder.getVisibleItems()) {
      int iconMarginPx =
          (int)
              TypedValue.applyDimension(
                  TypedValue.COMPLEX_UNIT_DIP, ICON_MARGIN, getResources().getDisplayMetrics());

      if (item.getIcon() != null) {
        if (VERSION.SDK_INT > VERSION_CODES.LOLLIPOP) {
          item.setIcon(new InsetDrawable(item.getIcon(), iconMarginPx, 0, iconMarginPx, 0));
        } else {
          item.setIcon(
              new InsetDrawable(item.getIcon(), iconMarginPx, 0, iconMarginPx, 0) {
                @Override
                public int getIntrinsicWidth() {
                  return getIntrinsicHeight() + iconMarginPx + iconMarginPx;
                }
              });
        }
      }
    }
  }
  popup.setOnMenuItemClickListener(
      menuItem -> {
        Snackbar.make(
                getActivity().findViewById(android.R.id.content),
                menuItem.getTitle(),
                Snackbar.LENGTH_LONG)
            .show();
        return true;
      });
  popup.show();
}
 
Example 18
Source File: EditImageActivity.java    From Camera-Roll-Android-App with Apache License 2.0 4 votes vote down vote up
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    final CropImageView imageView = findViewById(R.id.cropImageView);
    switch (item.getItemId()) {
        case android.R.id.home:
            onBackPressed();
            break;
        case R.id.rotate:
            boolean showAnimations = Settings.getInstance(this).showAnimations();
            Drawable d = item.getIcon();
            if (showAnimations && d instanceof Animatable && !((Animatable) d).isRunning()) {
                ((Animatable) d).start();
            }
            rotate90Degrees();
            break;
        case R.id.done:
            done(item.getActionView());
            break;
        case R.id.aspect_ratio_free:
            item.setChecked(true);
            imageView.setFreeAspectRatio();
            break;
        case R.id.aspect_ratio_original:
            item.setChecked(true);
            imageView.setOriginalAspectRatioFixed();
            break;
        case R.id.aspect_ratio_square:
            item.setChecked(true);
            imageView.setAspectRatio(1.0);
            break;
        case R.id.aspect_ratio_3_2:
            item.setChecked(true);
            imageView.setAspectRatio(3.0 / 2.0);
            break;
        case R.id.aspect_ratio_4_3:
            item.setChecked(true);
            imageView.setAspectRatio(4.0 / 3.0);
            break;
        case R.id.aspect_ratio_16_9:
            item.setChecked(true);
            imageView.setAspectRatio(16.0 / 9.0);
            break;
        case R.id.restore:
            imageView.restore();
            break;
        default:
            break;
    }
    return super.onOptionsItemSelected(item);
}
 
Example 19
Source File: MeasurementEntryFragment.java    From openScale with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    menu.clear();
    inflater.inflate(R.menu.dataentry_menu, menu);

    // Apply a tint to all icons in the toolbar
    for (int i = 0; i < menu.size(); ++i) {
        MenuItem item = menu.getItem(i);
        final Drawable drawable = item.getIcon();
        if (drawable == null) {
            continue;
        }

        final Drawable wrapped = DrawableCompat.wrap(drawable.mutate());

        if (item.getItemId() == R.id.saveButton) {
            DrawableCompat.setTint(wrapped, Color.parseColor("#FFFFFF"));
        } else if (item.getItemId() == R.id.editButton) {
            DrawableCompat.setTint(wrapped, Color.parseColor("#99CC00"));
        } else if (item.getItemId() == R.id.expandButton) {
            DrawableCompat.setTint(wrapped, Color.parseColor("#FFBB33"));
        } else if (item.getItemId() == R.id.deleteButton) {
            DrawableCompat.setTint(wrapped, Color.parseColor("#FF4444"));
        }

        item.setIcon(wrapped);
    }

    saveButton = menu.findItem(R.id.saveButton);
    editButton = menu.findItem(R.id.editButton);
    expandButton = menu.findItem(R.id.expandButton);
    deleteButton = menu.findItem(R.id.deleteButton);

    // Hide/show icons as appropriate for the view mode
    switch (mode) {
        case ADD:
            setViewMode(MeasurementView.MeasurementViewMode.ADD);
            break;
        case EDIT:
            setViewMode(MeasurementView.MeasurementViewMode.EDIT);
            break;
        case VIEW:
            setViewMode(MeasurementView.MeasurementViewMode.VIEW);
            break;
    }

    super.onCreateOptionsMenu(menu, inflater);
}
 
Example 20
Source File: TrackIconUtils.java    From mytracks with Apache License 2.0 2 votes vote down vote up
/**
 * Reverts the menu icon color.
 * 
 * @param menuitem the menu item
 */
private static void revertMenuIconColor(MenuItem menuitem) {
  Drawable drawable = menuitem.getIcon();
  drawable.setColorFilter(new ColorMatrixColorFilter(REVERT_COLOR_MATRIX));
}