Java Code Examples for android.support.v7.widget.PopupMenu#setOnMenuItemClickListener()

The following examples show how to use android.support.v7.widget.PopupMenu#setOnMenuItemClickListener() . 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: ClientItem.java    From snapdroid with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onClick(View v) {
    if (v == ibMute) {
        Volume volume = client.getConfig().getVolume();
        volume.setMuted(!volume.isMuted());
        update();
        listener.onVolumeChanged(this, volume.getPercent(), volume.isMuted());
    } else if (v == ibOverflow) {
        PopupMenu popup = new PopupMenu(v.getContext(), v);
        popup.getMenu().add(Menu.NONE, R.id.menu_details, 0, R.string.menu_details);
        if (!client.isConnected())
            popup.getMenu().add(Menu.NONE, R.id.menu_delete, 1, R.string.menu_delete);
        popup.setOnMenuItemClickListener(this);
        popup.show();
    }
}
 
Example 2
Source File: PopupMenuActivity.java    From MaterialDesignDemo with MIT License 6 votes vote down vote up
public void showPopupMenu(View view) throws NoSuchFieldException, IllegalAccessException {
    PopupMenu popupMenu = new PopupMenu(this, view);
    //设置 PopupMenu 的显示菜单项
    popupMenu.inflate(R.menu.main);
    // popupMenu.getMenuInflater().inflate(R.menu.main, popupMenu.getMenu());//与一行没什么区别
    //默认 PopupMenu 不显示条目icon,可以通过反射来强制使其显示icon
    Field field = popupMenu.getClass().getDeclaredField("mPopup");
    field.setAccessible(true);
    MenuPopupHelper mHelper = (MenuPopupHelper) field.get(popupMenu);
    mHelper.setForceShowIcon(true);
    //设置 PopupMenu 的条目点击事件(点击后会自动dismiss)
    popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            Toast.makeText(getApplicationContext(), item.getTitle(), Toast.LENGTH_SHORT).show();
            return false;
        }
    });
    //显示 PopupMenu
    popupMenu.show();
}
 
Example 3
Source File: MainActivity.java    From letterpress with Apache License 2.0 5 votes vote down vote up
private void showPopupMenu(View anchorView) {
    PopupMenu popup = new PopupMenu(MainActivity.this, anchorView);
    popup.getMenuInflater().inflate(R.menu.menu, popup.getMenu());
    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        public boolean onMenuItemClick(MenuItem item) {
            // ...
            return true;
        }
    });

    // Apply custom font to popup menu
    FontUtil.applyTypeface(popup.getMenu(), mTypeface);
    popup.show();
}
 
Example 4
Source File: NotesFragment.java    From memoir with Apache License 2.0 5 votes vote down vote up
@Override
public void onLongClicked(View view, final int position) {
    final Context context = getContext();
    PopupMenu popupMenu = new PopupMenu(context, view);
    popupMenu.inflate(R.menu.menu_delete);
    popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            switch (item.getItemId()) {

                case R.id.note_delete:
                    new AlertDialog.Builder(context)
                            .setTitle("Delete Note")
                            .setMessage("Are you sure you want to delete the note?")
                            .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    NotesDatabase.getInstance(context).deleteNoteAt(position);
                                    adapter.notifyItemRemoved(position);
                                    refreshLayout();
                                }
                            })
                            .setNegativeButton(android.R.string.no, null)
                            .show();
                    return true;

                default:
                    return false;
            }
        }
    });
    popupMenu.show();
}
 
Example 5
Source File: ReportAdapter.java    From privacy-friendly-pedometer with GNU General Public License v3.0 5 votes vote down vote up
public void showPopup(View v, Context c) {
    PopupMenu popup = new PopupMenu(c, v);
    MenuInflater inflater = popup.getMenuInflater();
    inflater.inflate(R.menu.menu_card_activity_summary, popup.getMenu());
    popup.setOnMenuItemClickListener(this);
    if (mItemClickListener != null) {
        mItemClickListener.setActivityChartDataTypeChecked(popup.getMenu());
    }
    popup.show();
}
 
Example 6
Source File: TicketGroupListAdapter.java    From Ticket-Analysis with MIT License 5 votes vote down vote up
public void setData(String codeGroup, int realPosition) {
    CodeGroupAdapter codeGroupAdapter = new CodeGroupAdapter(mContext, codeGroup);
    rvCodeGroup.setLayoutManager(new GridLayoutManager(mContext, 7));
    rvCodeGroup.setAdapter(codeGroupAdapter);
    PopupMenu popupMenu = new PopupMenu(mContext, tvOperate);
    popupMenu.inflate(R.menu.operate_menu);
    popupMenu.setOnMenuItemClickListener(item -> {
        // TODO Auto-generated method stub
        switch (item.getItemId()) {
            case R.id.copy:
                ClipboardManager clipboardManager = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
                clipboardManager.setPrimaryClip(ClipData.newPlainText(null, codeGroup));
                ToastUtil.showToast("已经复制到剪切板");
                break;
            case R.id.delete:
                if (mCodeGroups.contains(codeGroup)) {
                    int index = mCodeGroups.indexOf(codeGroup);
                    mCodeGroups.remove(index);
                    notifyItemRemoved(index);
                }
                break;
            default:
                break;
        }
        return false;
    });
    tvOperate.setOnClickListener(v -> {
        popupMenu.show();
    });
}
 
Example 7
Source File: InterpretationAdapter.java    From dhis2-android-dashboard with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void onClick(View view) {
    PopupMenu popupMenu = new PopupMenu(mContext, view);

    if (isInterpretationEditable()) {
        popupMenu.getMenu().add(MENU_GROUP_ID, MENU_EDIT_INTERPRETATION_ID,
                MENU_EDIT_INTERPRETATION_ORDER, R.string.edit);
    }

    if (isInterpretationDeletable()) {
        popupMenu.getMenu().add(MENU_GROUP_ID, MENU_DELETE_INTERPRETATION_ID,
                MENU_DELETE_INTERPRETATION_ORDER, R.string.delete);
    }

    popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem menuItem) {
            if (mListener == null) {
                return false;
            }

            if (menuItem.getItemId() == MENU_EDIT_INTERPRETATION_ID) {
                mListener.onInterpretationEditClick(mInterpretation);
            } else if (menuItem.getItemId() == MENU_DELETE_INTERPRETATION_ID) {
                mListener.onInterpretationDeleteClick(mInterpretation);
            }

            return true;
        }
    });

    popupMenu.show();
}
 
Example 8
Source File: DashboardItemAdapter.java    From dhis2-android-dashboard with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void onClick(View view) {
    PopupMenu popupMenu = new PopupMenu(mContext, view);

    if (isItemShareable()) {
        popupMenu.getMenu().add(MENU_GROUP_ID,
                MENU_SHARE_ITEM_ID, MENU_SHARE_ITEM_ORDER,
                R.string.share_interpretation);
    }

    if (isDashboardUpdatable()) {
        popupMenu.getMenu().add(MENU_GROUP_ID,
                MENU_DELETE_ITEM_ID, MENU_DELETE_ITEM_ORDER,
                R.string.delete);
    }

    popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem menuItem) {
            if (mListener == null) {
                return false;
            }

            if (menuItem.getItemId() == MENU_SHARE_ITEM_ID) {
                mListener.onItemShareClick(mDashboardItem);
            } else if (menuItem.getItemId() == MENU_DELETE_ITEM_ID) {
                mListener.onItemDeleteClick(mDashboardItem);
            }

            return true;
        }
    });

    popupMenu.show();
}
 
Example 9
Source File: WhiteListActivity.java    From timecat with Apache License 2.0 5 votes vote down vote up
public void showPopupMenu(View view) {
    //参数View 是设置当前菜单显示的相对于View组件位置,具体位置系统会处理
    PopupMenu popupMenu = new PopupMenu(this, view, Gravity.RIGHT, R.attr.popupMenuStyle, R.style.PopUpMenuStyle);
    //加载menu布局
    Menu menu = popupMenu.getMenu();
    String[] spinnerArray = getResources().getStringArray(SPINNER_ARRAY);
    int i = 0;
    for (i = 0; i < spinnerArray.length; i++) {
        menu.add(0, i, i, spinnerArray[i]);
    }
    menu.add(0, i, i, R.string.select_cancel);
    //设置menu中的item点击事件
    popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            if (mAppAdapter == null) {
                return false;
            }
            int id = item.getItemId();
            if (id < spinnerArray.length) {
                for (AppListAdapter.ApplicationInfoWrap app : mSelectedApplicationInfos) {
                    app.selection = id;
                }
                mAppAdapter.notifyDataSetChanged();
                refreshMenu(true);
            } else {
                unSelectAll();
                refreshMenu(false);
            }
            UrlCountUtil.onEvent(UrlCountUtil.STATUS_WL_SELECTION, id + "");
            return true;
        }
    });
    popupMenu.show();
}
 
Example 10
Source File: AlarmClockAdapter.java    From SuntimesWidget with GNU General Public License v3.0 5 votes vote down vote up
/**
 * showAlarmTypeMenu
 * @param item AlarmClockItem
 * @param buttonView button that triggered menu
 * @param itemView view associated with item
 */
protected void showAlarmTypeMenu(final AlarmClockItem item, final View buttonView, final View itemView)
{
    PopupMenu menu = new PopupMenu(context, buttonView);
    MenuInflater inflater = menu.getMenuInflater();
    inflater.inflate(R.menu.alarmtype, menu.getMenu());

    menu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener()
    {
        @Override
        public boolean onMenuItemClick(MenuItem menuItem)
        {
            switch (menuItem.getItemId())
            {
                case R.id.alarmTypeNotification:
                    return changeAlarmType(item, AlarmClockItem.AlarmType.NOTIFICATION);

                case R.id.alarmTypeAlarm:
                default:
                    return changeAlarmType(item, AlarmClockItem.AlarmType.ALARM);
            }
        }
    });

    SuntimesUtils.forceActionBarIcons(menu.getMenu());
    menu.show();
}
 
Example 11
Source File: LocalConfigActivity.java    From TimetableView with MIT License 5 votes vote down vote up
/**
 * 显示弹出菜单
 */
public void showPopmenu() {
    PopupMenu popup = new PopupMenu(this, moreButton);
    popup.getMenuInflater().inflate(R.menu.popmenu_local_config, popup.getMenu());
    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        public boolean onMenuItemClick(MenuItem item) {
            switch (item.getItemId()) {
                case R.id.top1:
                    loadLocalConfig();
                    break;
                case R.id.top2:
                    clearLocalConfig();
                    break;
                case R.id.top3:
                    exportLocalConfig();
                    break;
                case R.id.top4:
                    loadLocalConfigSet();
                    break;
                default:
                    break;
            }
            return true;
        }
    });

    popup.show();
}
 
Example 12
Source File: FlaglayoutActivity.java    From TimetableView with MIT License 5 votes vote down vote up
/**
 * 显示弹出菜单
 */
public void showPopmenu() {
    PopupMenu popup = new PopupMenu(this, moreButton);
    popup.getMenuInflater().inflate(R.menu.popmenu_flaglayout, popup.getMenu());
    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        public boolean onMenuItemClick(MenuItem item) {
            switch (item.getItemId()) {
                case R.id.top1:
                    modifyFlagBgcolor(Color.RED);
                    break;
                case R.id.top2:
                    resetFlagBgcolor();
                    break;
                case R.id.top3:
                    cancelFlagBgcolor();
                    break;
                case R.id.top4:
                    resetFlaglayout();
                    break;
                default:
                    break;
            }
            return true;
        }
    });

    popup.show();
}
 
Example 13
Source File: PostEditFragment.java    From quill with MIT License 5 votes vote down vote up
@Override
public void onFormatImageClicked(View v) {
    PopupMenu popupMenu = new PopupMenu(mActivity, v);
    popupMenu.inflate(R.menu.insert_image);
    // hide the "Remove This Image" option
    MenuItem removeImageItem = popupMenu.getMenu().findItem(R.id.action_image_remove);
    if (removeImageItem != null) {
        removeImageItem.setVisible(false);
    }

    popupMenu.setOnMenuItemClickListener(item -> {
        mMarkdownEditSelectionState = new EditTextSelectionState(mPostEditView);
        Action1<String> insertMarkdownAction = (imageUrl) -> {
            EditTextUtils.insertMarkdownImageMarkers(imageUrl, mMarkdownEditSelectionState);
        };
        if (item.getItemId() == R.id.action_insert_image_url) {
            onInsertImageUrlClicked(insertMarkdownAction);
        } else if (item.getItemId() == R.id.action_insert_image_upload) {
            // the *WithCheck() method checks for runtime permissions and
            // is generated by the PermissionsDispatcher library
            PostEditFragmentPermissionsDispatcher.onInsertImageUploadClickedWithCheck(this,
                    insertMarkdownAction);
        }
        return true;
    });
    popupMenu.show();
}
 
Example 14
Source File: ReminderPickerDialog.java    From OmniList with GNU Affero General Public License v3.0 5 votes vote down vote up
private void showTimePicker(View v) {
    PopupMenu popupM = new PopupMenu(Objects.requireNonNull(getContext()), v);
    popupM.inflate(R.menu.time_picker_menu);
    ViewUtils.forceShowIcon(popupM);
    popupM.setOnMenuItemClickListener(item -> {
        switch (item.getItemId()){
            case R.id.item_morning:
                alarm.setHour(9);
                alarm.setMinute(0);
                break;
            case R.id.item_noon:
                alarm.setHour(12);
                alarm.setMinute(0);
                break;
            case R.id.item_afternoon:
                alarm.setHour(15);
                alarm.setMinute(0);
                break;
            case R.id.item_evening:
                alarm.setHour(18);
                alarm.setMinute(0);
                break;
            case R.id.item_night:
                alarm.setHour(21);
                alarm.setMinute(0);
                break;
            case R.id.item_custom:
                showCustomTimeDialog();
                return true;
            case R.id.item_clear_time: break; // invisible
        }
        displayAlarmInfo();
        return true;
    });
    popupM.show();
}
 
Example 15
Source File: AlertsFragment.java    From hawkular-android-client with Apache License 2.0 5 votes vote down vote up
private void showAlertMenu(final View alertView, final int alertPosition) {
    PopupMenu alertMenu = new PopupMenu(getActivity(), alertView);

    alertMenu.getMenuInflater().inflate(R.menu.popup_alerts, alertMenu.getMenu());

    alertMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem menuItem) {
            Alert alert = getAlertsAdapter().getItem(alertPosition);

            switch (menuItem.getItemId()) {
                case R.id.menu_resolve:
                    BackendClient.of(AlertsFragment.this).resolveAlert(alert, new AlertActionCallback(AlertsFragment.this));
                    return true;

                case R.id.menu_acknowledge:
                    BackendClient.of(AlertsFragment.this).acknowledgeAlert(alert, new AlertActionCallback(AlertsFragment.this));
                    return true;

                default:
                    return false;
            }
        }
    });

    alertMenu.show();
}
 
Example 16
Source File: StatusRecyclerAdapter.java    From Rumble with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onClick(View v)
{
    PopupMenu popupMenu =  new PopupMenu(activity, v);
    popupMenu.getMenu().add(Menu.NONE, 1, Menu.NONE, R.string.status_more_option_like);
    popupMenu.getMenu().add(Menu.NONE, 2, Menu.NONE, R.string.status_more_option_save);
    popupMenu.getMenu().add(Menu.NONE, 3, Menu.NONE, R.string.status_more_option_delete);
    popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem menuItem) {
            int pos = getAdapterPosition();
            switch (menuItem.getItemId()) {
                case 1:
                    EventBus.getDefault().post(new UserLikedStatus(statuses.get(pos)));
                    return true;
                case 2:
                    EventBus.getDefault().post(new UserSavedStatus(statuses.get(pos)));
                    return true;
                case 3:
                    EventBus.getDefault().post(new UserDeleteStatus(statuses.get(pos)));
                    return true;
                default:
                    return false;
            }
        }
    });
    popupMenu.show();
}
 
Example 17
Source File: TrainingOverviewAdapter.java    From privacy-friendly-pedometer with GNU General Public License v3.0 5 votes vote down vote up
public void showPopup(View v, Context c) {
    PopupMenu popup = new PopupMenu(c, v);
    MenuInflater inflater = popup.getMenuInflater();
    inflater.inflate(R.menu.menu_card_training_session, popup.getMenu());
    popup.setOnMenuItemClickListener(this);
    popup.show();
}
 
Example 18
Source File: PostItemHeaderView.java    From redgram-for-reddit with GNU General Public License v3.0 5 votes vote down vote up
private void setupMoreView() {
    popupMenu = new PopupMenu(getContext(), headerMoreView);
    popupMenu.getMenuInflater().inflate(R.menu.header_more_menu, popupMenu.getMenu());
    popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            if (item.getItemId() == R.id.view_subreddit) {
                listener.visitSubreddit(postItem.getSubreddit());
                return true;
            }
            if (item.getItemId() == R.id.open_browser) {
                listener.openInBrowser(position);
                return true;
            }

            if (item.getItemId() == R.id.copy_link) {
                listener.copyItemLink(position);
                return true;
            }

            if (item.getItemId() == R.id.report) {
                listener.reportPost(position);
                return true;
            }

            if (item.getItemId() == R.id.view_profile) {
                listener.visitProfile(postItem.getAuthor());
                return true;
            }

            return false;
        }
    });
}
 
Example 19
Source File: BaseFuncActivity.java    From TimetableView with MIT License 4 votes vote down vote up
/**
 * 显示弹出菜单
 */
public void showPopmenu() {
    PopupMenu popup = new PopupMenu(this, moreButton);
    popup.getMenuInflater().inflate(R.menu.popmenu_base_func, popup.getMenu());
    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        public boolean onMenuItemClick(MenuItem item) {
            switch (item.getItemId()) {
                case R.id.top1:
                    addSubject();
                    break;
                case R.id.top2:
                    deleteSubject();
                    break;

                case R.id.top4:
                    hideNonThisWeek();
                    break;
                case R.id.top5:
                    showNonThisWeek();
                    break;
                case R.id.top6:
                    setMaxItem(8);
                    break;
                case R.id.top7:
                    setMaxItem(10);
                    break;
                case R.id.top8:
                    setMaxItem(12);
                    break;
                case R.id.top9:
                    showTime();
                    break;
                case R.id.top10:
                    hideTime();
                    break;
                case R.id.top11:
                    showWeekView();
                    break;
                case R.id.top12:
                    hideWeekView();
                    break;
                case R.id.top13:
                    setMonthWidth();
                    break;
                case R.id.top16:
                    resetMonthWidth();
                    break;
                case R.id.top14:
                    hideWeekends();
                    break;
                case R.id.top15:
                    showWeekends();
                    break;
                default:
                    break;
            }
            return true;
        }
    });

    popup.show();
}
 
Example 20
Source File: ArtistListData.java    From Pasta-for-Spotify with Apache License 2.0 4 votes vote down vote up
@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.menu:
            PopupMenu popup = new PopupMenu(v.getContext(), v);
            MenuInflater inflater = popup.getMenuInflater();
            inflater.inflate(R.menu.menu_basic, popup.getMenu());

            final MenuItem fav = popup.getMenu().findItem(R.id.action_fav);
            new Action<Boolean>() {
                @NonNull
                @Override
                public String id() {
                    return "isTrackFav";
                }

                @Nullable
                @Override
                protected Boolean run() throws InterruptedException {
                    return pasta.isFavorite(listData);
                }

                @Override
                protected void done(@Nullable Boolean result) {
                    if (result == null) return;
                    if (result) {
                        fav.setTitle(R.string.unfav);
                    } else {
                        fav.setTitle(R.string.fav);
                    }
                }

            }.execute();

            popup.setOnMenuItemClickListener(this);
            popup.show();
            break;
        default:
            Bundle args = new Bundle();
            args.putParcelable("artist", listData);

            Fragment f = new ArtistFragment();
            f.setArguments(args);

            activity.getSupportFragmentManager().beginTransaction().replace(R.id.fragment, f).addToBackStack(null).commit();
            break;
    }
}