Problem: Cannot display a context menu showing a "delete" option" when longclicking on an item within a recyclerview
Result expected: see image below
I'm almost there, but I'm missing something to make the contextMenu displayed on a longClick.
Here is what I put in the viewHolder. I don't know what I should add and where to display the context menu in the onLongClick event.
I skipped some lines of code and kept the ones relevant to my question.
Thanks a lot for your assistance,
My interface to handle both types of clicks
public interface OnItemClickListener{
    void onItemClick(int position);
}
public interface OnItemLongClickListener{
    void onItemLongClick(int position);
}
Viewholder code
public void bindLongClick(final int position, final OnItemLongClickListener onItemLongClickListener) {
        itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                onItemLongClickListener.onItemLongClick(position);
                return true;
            }
        });
    }
    @Override
    public void onCreateContextMenu(ContextMenu contextMenu, View view, ContextMenu.ContextMenuInfo contextMenuInfo){
            //menuInfo is null
        Log.v(LOG_TAG, "grrr");
        contextMenu.setHeaderTitle("Select The Action");
        contextMenu.add(0, view.getId(), 0, "Supprimer");//groupId, itemId, order, title
    }
Adapter code
@Override
    public void onBindViewHolder(CityListViewholder holder, int position) {
        holder.cityName.setText(cityArrayList.get(position).getCityName());
        holder.bindClick(position, onItemClickListener);
        holder.bindLongClick(position, onItemLongClickListener);
    }
Then, in the activity - I skipped what is not relevant for my question
mCityListAdapter = new CityListAdapter(mContext, cityArrayList, new CityListAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                mPager.setCurrentItem(position);
                mDrawerLayout.closeDrawers();
            }
        }, new CityListAdapter.OnItemLongClickListener() {
            @Override
            public void onItemLongClick(int position) {
                Log.v(LOG_TAG, "Position "+position);
            }
        });
        registerForContextMenu(mRecyclerView);

What you need there is to show Dialog with list inside. Like that:
    itemView.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View view) {
            final CharSequence[] items = {"Supprimer", "etc", "etc1"};
            AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
            builder.setTitle("Select The Action");
            builder.setItems(items, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int item) {
                }
            });
            builder.show();
            return true;
        }
    });
                        Accepted answer isn't technically "the answer", it is rather a clever workaround since it creates an AlertDialog instead of a ContextMenu as requested by the OP, it does "solve the requirement" but it does not create a ContextMenu.
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    menu.add("do this");
    menu.add("do that");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
    if (item.getTitle().equals("do this") {
        // do whatever this...
    }
    else if (item.getTitle().equals("do that") {
        // do whatever that...
    }
    return super.onContextItemSelected(item);
}
private class ItemViewHolder extends RecyclerView.ViewHolder {
    ...
    private ItemViewHolder(@NonNull View pItemView) {
        super(pItemView);
        pItemView.setLongClickable(true); // <-- make long clickable
        ...
    }
}
registerForContextMenu(myRecyclerView);
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With