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

The following examples show how to use android.view.Menu#size() . 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: SettingsActivity.java    From MaterialPreferenceLibrary with Apache License 2.0 6 votes vote down vote up
private static void setOnMenuItemClickListenerRecursive(Menu menu, OnMenuItemClickListener listener) {
    List<Menu> menus = new ArrayList<>(1);
    menus.add(menu);
    while (!menus.isEmpty()) {
        final Menu currentMenu = menus.remove(0);
        for (int i = 0; i < currentMenu.size(); ++i) {
            final MenuItem item = currentMenu.getItem(i);
            item.setOnMenuItemClickListener(listener);
            final SubMenu subMenu = item.getSubMenu();
            if (subMenu != null)
                menus.add(subMenu);
        }
    }
}
 
Example 2
Source File: ViewUserActivity.java    From BotLibre with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
       for (int index = 0; index < menu.size(); index++) {
   	    menu.getItem(index).setEnabled(true);        	
       }
       if (MainActivity.user == null || MainActivity.user == MainActivity.viewUser) {
   	    MenuItem item = menu.findItem(R.id.menuFlag);
   	    if (item != null) {
   	    	item.setEnabled(false);
   	    }
       }
       if (MainActivity.user != MainActivity.viewUser) {
   	    menu.findItem(R.id.menuChangeIcon).setEnabled(false);
   	    menu.findItem(R.id.menuEditUser).setEnabled(false);
       }
    return true;
}
 
Example 3
Source File: PlumbleActivity.java    From Plumble with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    MenuItem disconnectButton = menu.findItem(R.id.action_disconnect);
    disconnectButton.setVisible(mService != null && mService.isConnected());

    // Color the action bar icons to the primary text color of the theme.
    int foregroundColor = getSupportActionBar().getThemedContext()
            .obtainStyledAttributes(new int[] { android.R.attr.textColor })
            .getColor(0, -1);
    for(int x=0;x<menu.size();x++) {
        MenuItem item = menu.getItem(x);
        if(item.getIcon() != null) {
            Drawable icon = item.getIcon().mutate(); // Mutate the icon so that the color filter is exclusive to the action bar
            icon.setColorFilter(foregroundColor, PorterDuff.Mode.MULTIPLY);
        }
    }

    return super.onPrepareOptionsMenu(menu);
}
 
Example 4
Source File: BottomNavigationViewTest.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
@UiThreadTest
@Test
@SmallTest
@SdkSuppress(minSdkVersion = Build.VERSION_CODES.N)
@TargetApi(Build.VERSION_CODES.N)
public void testPointerIcon() throws Throwable {
  final Activity activity = activityTestRule.getActivity();
  final PointerIcon expectedIcon = PointerIcon.getSystemIcon(activity, PointerIcon.TYPE_HAND);
  final MotionEvent event = MotionEvent.obtain(0, 0, MotionEvent.ACTION_HOVER_MOVE, 0, 0, 0);
  final Menu menu = bottomNavigation.getMenu();
  for (int i = 0; i < menu.size(); i++) {
    final MenuItem item = menu.getItem(i);
    assertTrue(item.isEnabled());
    final View itemView = activity.findViewById(item.getItemId());
    assertEquals(expectedIcon, itemView.onResolvePointerIcon(event, 0));
    item.setEnabled(false);
    assertEquals(null, itemView.onResolvePointerIcon(event, 0));
    item.setEnabled(true);
    assertEquals(expectedIcon, itemView.onResolvePointerIcon(event, 0));
  }
}
 
Example 5
Source File: SweetSheet.java    From AndroidSweetSheet with Apache License 2.0 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 6
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 7
Source File: BottomNavigationViewEx.java    From Cashew with Apache License 2.0 5 votes vote down vote up
MyOnNavigationItemSelectedListener(ViewPager viewPager, BottomNavigationViewEx bnve, boolean smoothScroll, OnNavigationItemSelectedListener listener) {
    this.viewPagerRef = new WeakReference<>(viewPager);
    this.listener = listener;
    this.smoothScroll = smoothScroll;

    // create items
    Menu menu = bnve.getMenu();
    int size = menu.size();
    items = new SparseIntArray(size);
    for (int i = 0; i < size; i++) {
        int itemId = menu.getItem(i).getItemId();
        items.put(itemId, i);
    }
}
 
Example 8
Source File: BottomNavigationViewEx.java    From playa with MIT License 5 votes vote down vote up
/**
 * get menu item position in menu
 *
 * @param item
 * @return position if success, -1 otherwise
 */
public int getMenuItemPosition(MenuItem item) {
    // get item id
    int itemId = item.getItemId();
    // get meunu
    Menu menu = getMenu();
    int size = menu.size();
    for (int i = 0; i < size; i++) {
        if (menu.getItem(i).getItemId() == itemId) {
            return i;
        }
    }
    return -1;
}
 
Example 9
Source File: LoansByItemFragment.java    From loaned-android with Apache License 2.0 5 votes vote down vote up
@Override
public void onCreateOptionsMenu(Menu menu,MenuInflater inf){
	super.onCreateOptionsMenu(menu, inf);
       // Check if the menu already has items in it.
       // Otherwise we could cause a duplicate menu item issue.
       if(menu.size()==0)inf.inflate(R.menu.loanslist, menu);
}
 
Example 10
Source File: ContextUtils.java    From Stringlate with MIT License 5 votes vote down vote up
/**
 * Try to tint all {@link Menu}s {@link MenuItem}s with given color
 */
@SuppressWarnings("ConstantConditions")
public void tintMenuItems(Menu menu, boolean recurse, @ColorInt int iconColor) {
    for (int i = 0; i < menu.size(); i++) {
        MenuItem item = menu.getItem(i);
        tintDrawable(item.getIcon(), iconColor);
        if (item.hasSubMenu() && recurse) {
            tintMenuItems(item.getSubMenu(), recurse, iconColor);
        }
    }
}
 
Example 11
Source File: MainActivity.java    From Passbook with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(getResources().getBoolean(R.bool.hide_drawer) ?
            R.menu.menu_home_small : R.menu.menu_home_large, menu);
    for(int i = 0; i < menu.size(); ++i) {
        menu.getItem(i).getIcon().setColorFilter(
                C.ThemedColors[C.colorTextNormal], PorterDuff.Mode.SRC_ATOP);
    }
    SearchView searchView = (SearchView)menu.findItem(R.id.action_search).getActionView();
    searchView.setQueryHint(getString(R.string.search));
    searchView.setOnQueryTextListener(this);
    return true;
}
 
Example 12
Source File: FullCompassFragment.java    From android_gisapp with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onPrepareOptionsMenu(Menu menu)
{
    for (int i = 0; i < menu.size(); i++) {
        MenuItem item = menu.getItem(i);
        if (item.getItemId() == R.id.menu_about || item.getItemId() == R.id.menu_settings) {
            continue;
        }
        item.setVisible(false);
    }
    super.onPrepareOptionsMenu(menu);
}
 
Example 13
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 14
Source File: MdCompat.java    From android-md-core with Apache License 2.0 5 votes vote down vote up
public static void supportDrawableTint(Context context, Menu menu, Palette palette) {
  if (menu != null) {
    int i = 0;
    for (int n = menu.size(); i < n; i++) {
      supportDrawableTint(context, menu.getItem(i), palette);
    }
  }
}
 
Example 15
Source File: AudioBookPlayActivity.java    From HaoReader with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_audio_play_activity, menu);
    for (int i = 0; i < menu.size(); i++) {
        MenuItemImpl item = (MenuItemImpl) menu.getItem(i);
        AppCompat.setTint(item, getResources().getColor(R.color.white));
    }
    return true;
}
 
Example 16
Source File: ActionBarPlugin.java    From cordova-android-actionbar with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public Object onMessage(String id, Object data)
{
	if("onCreateOptionsMenu".equals(id) || "onPrepareOptionsMenu".equals(id))
	{
		menu = (Menu)data;

		if(menu_definition != null && menu.size() != menu_definition.length())
		{
			menu.clear();
			menu_callbacks.clear();
			buildMenu(menu, menu_definition);
		}
	}
	else if("onOptionsItemSelected".equals(id))
	{
		MenuItem item = (MenuItem)data;
		if(item.getItemId() == android.R.id.home)
		{
			webView.sendJavascript("if(window.plugins.actionbar.home_callback) window.plugins.actionbar.home_callback();");
		}
		else if(menu_callbacks.containsKey(item))
		{
			final String callback = menu_callbacks.get(item);
			webView.sendJavascript(callback);
		}
	}
	
	return null;
}
 
Example 17
Source File: LoansByPersonFragment.java    From loaned-android with Apache License 2.0 5 votes vote down vote up
@Override
public void onCreateOptionsMenu(Menu menu,MenuInflater inf){
	super.onCreateOptionsMenu(menu, inf);
       // Check if the menu already has items in it.
       // Otherwise we could cause a duplicate menu item issue.
       if(menu.size()==0)inf.inflate(R.menu.loanslist, menu);
}
 
Example 18
Source File: WebViewActivity.java    From GankMeizhi with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    MenuItem item;
    for (int i = 0; i < menu.size(); i++) {
        item = menu.getItem(i);
        Drawable drawable = item.getIcon();
        drawable.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_IN);
        item.setIcon(drawable);
        if (item.getItemId() == R.id.action_favorite) {
            changeFavoriteIcon(item, favorite.favorites, objectId);
        }
    }

    return super.onPrepareOptionsMenu(menu);
}
 
Example 19
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 20
Source File: BaseActivity.java    From datmusic-android with Apache License 2.0 4 votes vote down vote up
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    for (int i = 0; i < menu.size(); i++)
        menu.getItem(i).setVisible(!shouldGoInvisible);
    return super.onPrepareOptionsMenu(menu);
}