Search code examples
androidlistviewcontextmenuandroid-arrayadapter

Opening ContextMenu from inside ArrayAdapter


I have a setOnClickLisnter Method in my getView method in my Custom ArrayAdapter.

What I want to do is this, on a single, short click of an imageButton inside each row of the ListView, I want it to open up a ContextMenu.

I currently have the ContextMenu working when you long click on the ListView. Now I want to move that same functionality over to the short click mentioned above.

I have this inside my onCreate:

registerForContextMenu(getListView());

I also have a onCreateContextMenu inside the Activity. My main question is, how do you access this from inside the ArrayAdapter?

EDIT: I don't have to do this inside a setOncLickListner method, just somewhere in the getView

Code of getView:

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (convertView == null) {

            convertView = inflater.inflate(R.layout.commentlayout, parent,
                    false);
            holder = new ViewHolder();
            holder.ib1 = (ImageButton) convertView
                    .findViewById(R.id.labelChatIcon);


            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
            convertView.setOnCreateContextMenuListener(null);
        }

        holder.ib1.setBackgroundColor(Color.TRANSPARENT);

        holder.ib1.setBackgroundColor(Color.TRANSPARENT);
        holder.ib1.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                // Open Context Menu here

            }
        });

        return convertView;
    }

UPDATE: I think I am changing my plans and will use an Alert Dialog with Radio buttons. A Context menu is probably no appropriate in this situation.


Solution

  • You could use an Alert.Builder instead of the context menu, you can add a custom view by using builder.setView(View v);

    You would have a layout file with a bunch of different buttons of width: match_parent. Check out this link for all the different options available