Java Code Examples for android.view.Menu#getItem()

The following examples show how to use android.view.Menu#getItem() . 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: ShopListActivity.java    From FaceT with Mozilla Public License 2.0 6 votes vote down vote up
private void applyCustomFontToWholeMenu() {
        Menu m = view.getMenu();
        for (int i = 0; i < m.size(); i++) {
            MenuItem mi = m.getItem(i);
//            //for applying a font to subMenu ...
//            SubMenu subMenu = mi.getSubMenu();
//            if (subMenu!=null && subMenu.size() >0 ) {
//                for (int j=0; j <subMenu.size();j++) {
//                    MenuItem subMenuItem = subMenu.getItem(j);
//                    applyFontToMenuItem(subMenuItem);
//                }
//            }
            //the method we have create in activity
            applyFontToMenuItem(mi);
        }
    }
 
Example 2
Source File: CustomActionWebView.java    From CustomActionWebView with MIT License 6 votes vote down vote up
/**
 * 处理item,处理点击
 * @param actionMode
 */
private ActionMode resolveActionMode(ActionMode actionMode) {
    if (actionMode != null) {
        final Menu menu = actionMode.getMenu();
        mActionMode = actionMode;
        menu.clear();
        for (int i = 0; i < mActionList.size(); i++) {
            menu.add(mActionList.get(i));
        }
        for (int i = 0; i < menu.size(); i++) {
            MenuItem menuItem = menu.getItem(i);
            menuItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    getSelectedData((String) item.getTitle());
                    releaseAction();
                    return true;
                }
            });
        }
    }
    mActionMode = actionMode;
    return actionMode;
}
 
Example 3
Source File: MainActivity.java    From FaceT with Mozilla Public License 2.0 6 votes vote down vote up
private void applyCustomFontToWholeMenu() {
        Menu m = view.getMenu();
        for (int i = 0; i < m.size(); i++) {
            MenuItem mi = m.getItem(i);
//            //for applying a font to subMenu ...
//            SubMenu subMenu = mi.getSubMenu();
//            if (subMenu!=null && subMenu.size() >0 ) {
//                for (int j=0; j <subMenu.size();j++) {
//                    MenuItem subMenuItem = subMenu.getItem(j);
//                    applyFontToMenuItem(subMenuItem);
//                }
//            }
            //the method we have create in activity
            applyFontToMenuItem(mi);
        }
    }
 
Example 4
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 5
Source File: AppUtil.java    From udacity-p1-p2-popular-movies with MIT License 5 votes vote down vote up
private static void tintMenuItems(@Nullable Menu menu, @ColorInt int color) {
    if (menu == null) {
        return;
    }
    for (int i = 0; i < menu.size(); ++i) {
        MenuItem menuItem = menu.getItem(i);
        if (menuItem != null && menuItem.getIcon() != null) {
            tintDrawable(menuItem.getIcon(), color);
        }
    }
}
 
Example 6
Source File: MainActivity.java    From external-nfc-api with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
	
	for(int i = 0; i < menu.size(); i++) {
		MenuItem item = menu.getItem(i);
		if(item.getItemId() == R.id.action_ndef_format) {
			item.setVisible(ndefFormatable != null);
		} else if(item.getItemId() == R.id.action_ndef_write) {
			item.setVisible(ndef != null);
		}
	}
	
	return super.onPrepareOptionsMenu(menu);
}
 
Example 7
Source File: FloatingSearchView.java    From FloatingSearchView with Apache License 2.0 5 votes vote down vote up
private void showMenu(final boolean visible) {
    Menu menu = getMenu();
    for(int i = 0; i < menu.size(); i++) {
        MenuItem item = menu.getItem(i);
        if(mAlwaysShowingMenu.contains(item.getItemId())) continue;
        item.setVisible(visible);
    }
}
 
Example 8
Source File: AppMenuPropertiesDelegate.java    From 365browser with Apache License 2.0 5 votes vote down vote up
private void disableEnableMenuItem(
        Menu menu, int id, boolean visible, boolean enabled, boolean managed) {
    for (int i = 0; i < menu.size(); ++i) {
        MenuItem item = menu.getItem(i);
        if (item.getItemId() == id && item.isVisible()) {
            item.setVisible(visible);
            item.setEnabled(enabled);
            if (managed) {
                item.setIcon(ManagedPreferencesUtils.getManagedByEnterpriseIconId());
            } else {
                item.setIcon(null);
            }
        }
    }
}
 
Example 9
Source File: Preferences.java    From 365browser 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 10
Source File: BottomNavigationViewTest.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
@Test
@SmallTest
public void testBasics() {
  // Check the contents of the Menu object
  final Menu menu = bottomNavigation.getMenu();
  assertNotNull("Menu should not be null", menu);
  assertEquals("Should have matching number of items", MENU_CONTENT_ITEM_IDS.length, menu.size());
  for (int i = 0; i < MENU_CONTENT_ITEM_IDS.length; i++) {
    final MenuItem currItem = menu.getItem(i);
    assertEquals("ID for Item #" + i, MENU_CONTENT_ITEM_IDS[i], currItem.getItemId());
  }
}
 
Example 11
Source File: HistoryManagerToolbar.java    From 365browser with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
MenuItem getItemById(int menuItemId) {
    Menu menu = getMenu();
    for (int i = 0; i < menu.size(); i++) {
        MenuItem item = menu.getItem(i);
        if (item.getItemId() == menuItemId) return item;
    }
    return null;
}
 
Example 12
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 13
Source File: BubbleActions.java    From BubbleActions with Apache License 2.0 5 votes vote down vote up
/**
 * Set the actions using a menu xml resource. There are 3 requirements of the menu xml:
 * 1. The menu cannot have more than 5 items,
 * 2. Each menu item cannot have a submenu, and
 * 3. Each menu item must have an icon, title, and an id
 *
 * @param menuRes  The resource id of the menu
 * @param callback A callback to run on the main thread when an action is selected
 * @return the BubbleActions instance that called this method
 */
public BubbleActions fromMenu(int menuRes, final MenuCallback callback) {
    Menu menu = new PopupMenu(root.getContext(), null).getMenu();
    MenuInflater inflater = new MenuInflater(root.getContext());
    inflater.inflate(menuRes, menu);

    if (menu.size() > BubbleActionOverlay.MAX_ACTIONS) {
        throw new IllegalArgumentException(TAG + ": menu resource cannot have more than "
                + BubbleActionOverlay.MAX_ACTIONS + "actions.");
    }

    for (int i = 0; i < menu.size(); i++) {
        final MenuItem item = menu.getItem(i);

        if (item.hasSubMenu() || item.getIcon() == null || item.getTitle() == null || item.getItemId() == 0) {
            throw new IllegalArgumentException(TAG + ": menu resource cannot have a submenu and " +
                    "must have an icon, title, and id.");
        }

        final int id = item.getItemId();
        addAction(item.getTitle(), item.getIcon(), new Callback() {
            @Override
            public void doAction() {
                callback.doAction(id);
            }
        });
    }

    return this;
}
 
Example 14
Source File: Preferences.java    From AndroidChromium 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 15
Source File: MenuTinter.java    From xpra-client with GNU General Public License v3.0 5 votes vote down vote up
public static void tintMenuIcons(Menu menu, @ColorInt int color) {
    for(int i = 0; i < menu.size(); ++i) {
        final MenuItem item = menu.getItem(i);
        Drawable drawable = item.getIcon();
        if(null != drawable) {
            drawable = DrawableCompat.wrap(drawable);
            DrawableCompat.setTint(drawable, color);
            item.setIcon(drawable);
        }
    }
}
 
Example 16
Source File: SimpleCategoriesMenuAdapter.java    From LoopBar with MIT License 5 votes vote down vote up
@NonNull
private static List<ICategoryItem> convertMenuToCategoriesList(@NonNull Menu menu) {
    List<ICategoryItem> result = new ArrayList<>(menu.size());

    for (int i = 0, size = menu.size(); i < size; i++) {
        MenuItem menuItem = menu.getItem(i);
        result.add(new CategoryItem(menuItem.getIcon(), String.valueOf(menuItem.getTitle())));
    }

    return result;
}
 
Example 17
Source File: GenericActionBarActivity.java    From MonsterHunter4UDatabase with MIT License 5 votes vote down vote up
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    for (int i = 0; i < menu.size(); i++) {
        MenuItem mi = menu.getItem(i);
        String title = mi.getTitle().toString();
        Spannable newTitle = new SpannableString(title);
        newTitle.setSpan(new ForegroundColorSpan(Color.WHITE), 0,
                newTitle.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        mi.setTitle(newTitle);
    }
    return true;
}
 
Example 18
Source File: AdaptiveActionsToolbar.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
public static void adjustMenuActions(@NonNull Menu menu, int maxToShow, int toolbarWidthPx) {
  int menuSize = 0;

  for (int i = 0; i < menu.size(); i++) {
    if (menu.getItem(i).isVisible()) {
      menuSize++;
    }
  }

  int widthAllowed = toolbarWidthPx - ViewUtil.dpToPx(NAVIGATION_DP);
  int nItemsToShow = Math.min(maxToShow, widthAllowed / ViewUtil.dpToPx(ACTION_VIEW_WIDTH_DP));

  if (nItemsToShow < menuSize) {
    widthAllowed -= ViewUtil.dpToPx(OVERFLOW_VIEW_WIDTH_DP);
  }

  nItemsToShow = Math.min(maxToShow, widthAllowed / ViewUtil.dpToPx(ACTION_VIEW_WIDTH_DP));

  for (int i = 0; i < menu.size(); i++) {
    MenuItem item = menu.getItem(i);
    if (item.isVisible() && nItemsToShow > 0) {
      item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
      nItemsToShow--;
    } else {
      item.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
    }
  }
}
 
Example 19
Source File: Main.java    From Cook-It-Android-XML-Template with MIT License 4 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    setupToolbar(R.id.toolbar, "COOK IT", R.color.colorPink, R.color.colorWhiteTrans, R.drawable.ic_burger);

    FragmentTransaction ft;
    FragmentHome fragmentHome = new FragmentHome();
    ft = getSupportFragmentManager().beginTransaction();
    ft.replace(R.id.frameLayout, fragmentHome).commit();

    drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    Menu m = navigationView.getMenu();
    for (int i=0;i<m.size();i++) {
        MenuItem mi = m.getItem(i);
        SubMenu subMenu = mi.getSubMenu();
        if (subMenu!=null && subMenu.size() >0 ) {
            for (int j=0; j <subMenu.size();j++) {
                MenuItem subMenuItem = subMenu.getItem(j);
                applyFontToMenuItem(subMenuItem);
            }
        }
        applyFontToMenuItem(mi);
    }

    View header = navigationView.getHeaderView(0);
    ImageView imageView = (ImageView) header.findViewById(R.id.imageView);
    Glide.with(this)
            .load(Uri.parse("https://s3.amazonaws.com/uifaces/faces/twitter/jsa/128.jpg"))
            .transform(new CircleGlide(this))
            .into(imageView);
}
 
Example 20
Source File: GreenDaoSampleActivity.java    From android-opensource-library-56 with Apache License 2.0 4 votes vote down vote up
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.green_dao_sample, menu);
    mCleanup = menu.getItem(0);
    return true;
}