TheLettuceMaster
TheLettuceMaster

Reputation: 15734

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.

Upvotes: 1

Views: 1561

Answers (3)

Sebastian
Sebastian

Reputation: 103

The question is old but in case anybody else needs it: You cannot open the context menu directly. However, you can perform a long click on the according view. This will open the context menu. In your case:

holder.ib1.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                // Open Context Menu here
                convertView.performLongClick();
            }
        });

However, be aware that what you want is not according to Android's design guidelines. A long click is supposed to open the contextual menu, not a normal click.

Upvotes: 0

dannyRods
dannyRods

Reputation: 552

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

Upvotes: 1

Monty
Monty

Reputation: 3215

Follow this Link http://developer.android.com/guide/topics/ui/menus.html

and use

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
                                ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.context_menu, menu);
}


@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    switch (item.getItemId()) {
        case R.id.edit:
            editNote(info.id);
            return true;
        case R.id.delete:
            deleteNote(info.id);
            return true;
        default:
            return super.onContextItemSelected(item);
    }
}

Upvotes: 0

Related Questions