Java Code Examples for android.view.MenuItem#OnMenuItemClickListener

The following examples show how to use android.view.MenuItem#OnMenuItemClickListener . 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: WidgetsDialogFragment.java    From HgLauncher with GNU General Public License v3.0 5 votes vote down vote up
@Override public void onCreateContextMenu(@NonNull ContextMenu menu, @NonNull View v, @Nullable ContextMenu.ContextMenuInfo menuInfo) {
    // Set the calling view.
    callingView = (AppWidgetHostView) v;

    int index = appWidgetContainer.indexOfChild(v);

    // Workaround for DialogFragment issue with context menu.
    // Taken from: https://stackoverflow.com/a/18853634
    MenuItem.OnMenuItemClickListener listener = new MenuItem.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            onContextItemSelected(item);
            return true;
        }
    };

    // Generate menu.
    // TODO: Maybe a more robust and automated way can be done for this.
    menu.clear();
    menu.add(1, 0, 100, getString(R.string.action_remove_widget));
    menu.add(1, 1, 100, getString(R.string.action_up_widget));
    menu.add(1, 2, 100, getString(R.string.action_down_widget));
    menu.getItem(0).setOnMenuItemClickListener(listener);

    // Move actions should only be added when there is more than one widget.
    menu.getItem(1).setVisible(appWidgetContainer.getChildCount() > 1 && index > 0);
    menu.getItem(2).setVisible(appWidgetContainer.getChildCount() != index + 1);

    if (appWidgetContainer.getChildCount() > 1) {
        if (index > 0) {
            menu.getItem(1).setOnMenuItemClickListener(listener);
        }

        if (index + 1 != appWidgetContainer.getChildCount()) {
            menu.getItem(2).setOnMenuItemClickListener(listener);
        }
    }
}
 
Example 2
Source File: FloatingToolbar.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
public void layoutMenuItems(List<MenuItem> menuItems, MenuItem.OnMenuItemClickListener menuItemClickListener, int suggestedWidth) {
    mOnMenuItemClickListener = menuItemClickListener;
    cancelOverflowAnimations();
    clearPanels();
    menuItems = layoutMainPanelItems(menuItems, getAdjustedToolbarWidth(suggestedWidth));
    if (!menuItems.isEmpty()) {
        layoutOverflowPanelItems(menuItems);
    }
    updatePopupSize();
}
 
Example 3
Source File: HeroListFragment.java    From tup.dota2recipe with Apache License 2.0 5 votes vote down vote up
/**
 * 创建英雄属性筛选子菜单
 * 
 * @param cMenu
 *            当前菜单项
 * @param cMenuQueryKeyId
 * @param cSubMenuKeysId
 *            当前菜单关联的子菜单项资源ID
 * @param cSubMenuValues
 *            当前菜单关联的子菜单项 Values
 * @param menuItemClickListener
 *            创建后的子菜单单击监听
 */
private void createHeroOptionsSubMenu(MenuItem cMenu, int cMenuQueryKeyId,
        int cSubMenuKeysId, String[] cSubMenuValues,
        MenuItem.OnMenuItemClickListener menuItemClickListener) {
    if (cMenu == null || menuItemClickListener == null) {
        return;
    }

    final SubMenu cSubMenu = cMenu.getSubMenu();
    final String[] cSubMenuKeys = this.getResources().getStringArray(cSubMenuKeysId);
    MenuItem cMenuItem = null;
    int cIndex = 0;
    for (String cMenuKey : cSubMenuKeys) {
        cMenuItem = cSubMenu.add(0, cIndex++, 0, cMenuKey);
        cMenuItem.setOnMenuItemClickListener(menuItemClickListener);
    }

    // 设置菜单默认值
    final String queryKey = menu_hero_query_keys[cMenuQueryKeyId];
    if (!queryKey.equals(KEY_MENU_HERO_QUERY_ALL)
            && !queryKey.equals(KEY_MENU_HERO_QUERY_DEFAULT)
            && cSubMenuKeys != null) {
        final int queryValueIndex = Utils.indexOf(cSubMenuValues, queryKey);
        if (queryValueIndex >= 0 && cSubMenuKeys.length > queryValueIndex) {
            cMenu.setTitle(cSubMenuKeys[queryValueIndex]);
        }
    }
}
 
Example 4
Source File: ShareTarget.java    From Android-Next with Apache License 2.0 5 votes vote down vote up
public ShareTarget(CharSequence title, Drawable icon, int id,
                   final MenuItem.OnMenuItemClickListener listener) {
    this.title = title;
    this.icon = icon;
    this.id = id;
    this.listener = listener;
}
 
Example 5
Source File: ItemsListFragment.java    From tup.dota2recipe with Apache License 2.0 5 votes vote down vote up
/**
 * 创建英雄属性筛选子菜单
 * 
 * @param cMenu
 *            当前菜单项
 * @param cSubMenuKeysId
 *            当前菜单关联的子菜单项资源ID
 * @param menuItemClickListener
 *            创建后的子菜单单击监听
 */
private void createHeroOptionsSubMenu(MenuItem cMenu, int cMenuQueryKeyId,
        int cSubMenuKeysId,
        String[] cSubMenuValues,
        MenuItem.OnMenuItemClickListener menuItemClickListener) {
    if (cMenu == null || menuItemClickListener == null) {
        return;
    }

    final SubMenu cSubMenu = cMenu.getSubMenu();
    final String[] cSubMenuKeys = this.getResources().getStringArray(
            cSubMenuKeysId);
    MenuItem cMenuItem = null;
    int cIndex = 0;
    for (String cMenuKey : cSubMenuKeys) {
        cMenuItem = cSubMenu.add(0, cIndex++, 0, cMenuKey);
        cMenuItem.setOnMenuItemClickListener(menuItemClickListener);
    }

    // 设置菜单默认值
    final String queryKey = menu_items_query_keys[cMenuQueryKeyId];
    if (!queryKey.equals(KEY_MENU_ITEMS_QUERY_ALL) && cSubMenuKeys != null) {
        final int queryValueIndex = Utils.indexOf(cSubMenuValues, queryKey);
        if (queryValueIndex >= 0 && cSubMenuKeys.length > queryValueIndex) {
            cMenu.setTitle(cSubMenuKeys[queryValueIndex]);
        }
    }
}
 
Example 6
Source File: TGMenuBase.java    From tuxguitar with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void initializeItem(Menu menu, int id, MenuItem.OnMenuItemClickListener listener, boolean enabled, boolean checked) {
	MenuItem menuItem = menu.findItem(id);
	menuItem.setOnMenuItemClickListener(listener);
	menuItem.setEnabled(enabled);
	menuItem.setChecked(checked);
	menuItem.setVisible(true);
}
 
Example 7
Source File: TGTrackTuningActionHandler.java    From tuxguitar with GNU Lesser General Public License v2.1 5 votes vote down vote up
public MenuItem.OnMenuItemClickListener createRemoveTuningModelAction(final TGTrackTuningModel model) {
	return new MenuItem.OnMenuItemClickListener() {
		@Override
		public boolean onMenuItemClick(MenuItem menuItem) {
			TGTrackTuningActionHandler.this.dialog.postRemoveTuningModel(model);
			TGEditorManager.getInstance(TGTrackTuningActionHandler.this.dialog.findContext()).updateSelection();
			return true;
		}
	};
}
 
Example 8
Source File: AppListFragment.java    From island with Apache License 2.0 5 votes vote down vote up
@Override public void onPrepareOptionsMenu(final Menu menu) {
		final MenuItem.OnMenuItemClickListener tip = mUserGuide == null ? null : mUserGuide.getAvailableTip();
		menu.findItem(R.id.menu_tip).setVisible(tip != null).setOnMenuItemClickListener(tip);
		menu.findItem(R.id.menu_search).setVisible(mViewModel.mSelection.getValue() == null).setOnActionExpandListener(mOnActionExpandListener);
//		menu.findItem(R.id.menu_files).setVisible(context != null && Users.hasProfile() &&
//				(! Permissions.has(context, WRITE_EXTERNAL_STORAGE) || findFileBrowser(context) != null));
		menu.findItem(R.id.menu_show_system).setChecked(mViewModel.areSystemAppsIncluded());
		if (BuildConfig.DEBUG) menu.findItem(R.id.menu_test).setVisible(true);
	}
 
Example 9
Source File: FloatingToolbar.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
public FloatingToolbar setOnMenuItemClickListener(MenuItem.OnMenuItemClickListener menuItemClickListener) {
    if (menuItemClickListener != null) {
        mMenuItemClickListener = menuItemClickListener;
    } else {
        mMenuItemClickListener = NO_OP_MENUITEM_CLICK_LISTENER;
    }
    return this;
}
 
Example 10
Source File: FloatingToolbar.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
public void layoutMenuItems(List<MenuItem> menuItems, MenuItem.OnMenuItemClickListener menuItemClickListener, int suggestedWidth) {
    mOnMenuItemClickListener = menuItemClickListener;
    cancelOverflowAnimations();
    clearPanels();
    menuItems = layoutMainPanelItems(menuItems, getAdjustedToolbarWidth(suggestedWidth));
    if (!menuItems.isEmpty()) {
        layoutOverflowPanelItems(menuItems);
    }
    updatePopupSize();
}
 
Example 11
Source File: PermissionGroupActivity.java    From AppOpsX with MIT License 5 votes vote down vote up
@Override
public boolean onCreateOptionsMenu(Menu menu) {
  if(mPresenter != null && mPresenter.isLoadSuccess()){
    getMenuInflater().inflate(R.menu.group_menu,menu);

    MenuItem menuShowNet = menu.findItem(R.id.action_with_net);
    MenuItem mendShowHided = menu.findItem(R.id.action_show_ignored);

    final SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);

    MenuItem.OnMenuItemClickListener itemClickListener = new MenuItem.OnMenuItemClickListener() {
      @Override
      public boolean onMenuItemClick(MenuItem item) {
        item.setChecked(!item.isChecked());
        switch (item.getItemId()){
          case R.id.action_with_net:
            sp.edit().putBoolean("key_g_show_net", item.isChecked()).apply();
            break;
          case R.id.action_show_ignored:
            sp.edit().putBoolean("key_g_show_ignored", item.isChecked()).apply();
            break;
        }
        invalidateOptionsMenu();
        return true;
      }
    };

    menuShowNet.setChecked(sp.getBoolean("key_g_show_net", false));
    menuShowNet.setOnMenuItemClickListener(itemClickListener);

    mendShowHided.setChecked(sp.getBoolean("key_g_show_ignored",false));
    mendShowHided.setOnMenuItemClickListener(itemClickListener);
  }
  return super.onCreateOptionsMenu(menu);
}
 
Example 12
Source File: AttachmentsAdapter.java    From OmniList with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
protected void convert(BaseViewHolder helper, Attachment attachment) {
    Uri thumbnailUri = FileHelper.getThumbnailUri(PalmApp.getContext(), attachment.getUri());
    Glide.with(PalmApp.getContext())
            .load(thumbnailUri)
            .centerCrop()
            .crossFade()
            .into((ImageView) helper.getView(R.id.iv_thumbnail));

    helper.setText(R.id.tv_title, FileHelper.getNameFromUri(PalmApp.getContext(), thumbnailUri));

    if (Constants.MIME_TYPE_AUDIO.equals(attachment.getMineType())){
        helper.setImageResource(R.id.iv_thumbnail, attachment.isAudioPlaying() ? R.drawable.stop : R.drawable.play);
    }

    MenuItem.OnMenuItemClickListener listener = item -> {
        LogUtils.d(item.getItemId());
        switch (OnContextMenuClickedListener.AttachmentMenuItem.getItemById(item.getItemId())){
            case SHARE:
                if (onContextMenuClickedListener != null) {
                    onContextMenuClickedListener.onItemClicked(OnContextMenuClickedListener.AttachmentMenuItem.SHARE, attachment);
                }
                break;
            case DELETE:
                if (onContextMenuClickedListener != null) {
                    onContextMenuClickedListener.onItemClicked(OnContextMenuClickedListener.AttachmentMenuItem.DELETE, attachment);
                }
                break;
        }
        return true;
    };

    helper.itemView.setOnCreateContextMenuListener((menu, v, menuInfo) -> {
        menu.add(0, OnContextMenuClickedListener.AttachmentMenuItem.SHARE.itemId, 0, R.string.text_share).setOnMenuItemClickListener(listener);
        menu.add(0, OnContextMenuClickedListener.AttachmentMenuItem.DELETE.itemId, 1, R.string.text_delete).setOnMenuItemClickListener(listener);
    });
}
 
Example 13
Source File: TGMenuBase.java    From tuxguitar with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void initializeItem(Menu menu, int id, MenuItem.OnMenuItemClickListener listener, boolean enabled) {
	this.initializeItem(menu, id, listener, enabled, false);
}
 
Example 14
Source File: ContextMenu.java    From talkback with Apache License 2.0 4 votes vote down vote up
public MenuItem.OnMenuItemClickListener getDefaultListener() {
  return listener;
}
 
Example 15
Source File: MainMenuAdapter.java    From 920-text-editor-v2 with Apache License 2.0 4 votes vote down vote up
public void setMenuItemClickListener(MenuItem.OnMenuItemClickListener menuItemClickListener) {
    this.menuItemClickListener = menuItemClickListener;
}
 
Example 16
Source File: ShareTarget.java    From Android-Next with Apache License 2.0 4 votes vote down vote up
public ShareTarget(CharSequence title, Drawable icon,
                   final MenuItem.OnMenuItemClickListener listener) {
    this(title, icon, 0, listener);
}
 
Example 17
Source File: ShareTarget.java    From Android-Next with Apache License 2.0 4 votes vote down vote up
public ShareTarget(CharSequence title, Drawable icon,
                   final MenuItem.OnMenuItemClickListener listener) {
    this(title, icon, 0, listener);
}
 
Example 18
Source File: AppPermissionActivity.java    From AppOpsX with MIT License 4 votes vote down vote up
@Override
public boolean onCreateOptionsMenu(final Menu menu) {
  if (!mPresenter.isLoadSuccess()) {
    return false;
  }

  getMenuInflater().inflate(R.menu.app_menu, menu);

  MenuItem menuShowAllPerm = menu.findItem(R.id.action_hide_perm);
  MenuItem menuShowOpDesc = menu.findItem(R.id.action_show_op_perm);
  MenuItem menuShowOpName = menu.findItem(R.id.action_show_op_name);
  MenuItem menuShowPremTime = menu.findItem(R.id.action_show_perm_time);

  final SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);

  final Map<MenuItem, String> menus = new HashMap<>();
  menus.put(menuShowAllPerm, "key_show_no_prems");
  menus.put(menuShowOpDesc, "key_show_op_desc");
  menus.put(menuShowOpName, "key_show_op_name");
  menus.put(menuShowPremTime, "key_show_perm_time");

  MenuItem.OnMenuItemClickListener itemClickListener = new MenuItem.OnMenuItemClickListener() {
    @Override
    public boolean onMenuItemClick(MenuItem item) {
      String s = menus.get(item);
      if (s != null) {
        item.setChecked(!item.isChecked());
        sp.edit().putBoolean(s, item.isChecked()).apply();
        ActivityCompat.invalidateOptionsMenu(AppPermissionActivity.this);
        mPresenter.load();
      }
      return true;
    }
  };

  Set<Map.Entry<MenuItem, String>> entries = menus.entrySet();
  for (Map.Entry<MenuItem, String> entry : entries) {
    entry.getKey().setChecked(sp.getBoolean(entry.getValue(), false));
    entry.getKey().setOnMenuItemClickListener(itemClickListener);
  }

  return true;
}
 
Example 19
Source File: ContextMenu.java    From talkback with Apache License 2.0 2 votes vote down vote up
/**
 * Sets the default click listener for menu items. If the default click listener returns false for
 * an event, it will be passed to the menu's parent menu (if any).
 *
 * @param listener The default click listener for menu items.
 */
public void setDefaultListener(MenuItem.OnMenuItemClickListener listener) {
  this.listener = listener;
}
 
Example 20
Source File: AdvancedShareActionProvider.java    From Android-Next with Apache License 2.0 2 votes vote down vote up
/**
 * 设置MenuItem的点击事件
 *
 * @param listener
 */
public void setOnMenuItemClickListener(MenuItem.OnMenuItemClickListener listener) {
    mOnMenuItemClickListener = listener;
}