Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3301718
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:46:46+00:00 2026-05-17T20:46:46+00:00

I am having a lot of trouble understanding how the Android context menu works.

  • 0

I am having a lot of trouble understanding how the Android context menu works. I am writing a simple program which will display a list of different types of ice cream in a list view. When the user to “taps and holds over each type, a context menu should appear with 4 options asking what information they want to be displayed about that ice cream (picture, ingredients, order, other).

I don’t understand how to make it so that each list item’s context menu gives the appropriate info (the info associated with whichever list item the user tapped and held).

I am working from an example given by my teacher, which has some of the functionality I am after, but does not deal with the issue of displaying different info for each list item. I see that in the given code, my prof even gave a hint in the comments within the parameters of onCreateContextMenu():

public void onCreateContextMenu( //called each time the context menu is requested
        ContextMenu menu,   //the ContextMenu itself
        View v, //The view for which the context menu is being built
        ContextMenu.ContextMenuInfo menuInfo) { //tells you which item in the list the user did the tap-and-hold over 

But I am still unsure of what I am able to do with this parameter, and I’ve been spinning my wheels for a long time now. There is at least one other thread on stackoverflow that addresses this question, but it did not help me get any closer to understanding what’s going on here.

I really appreciate the help, as I am new to Android (and OOP generally) and a little hesitant to ask questions for fear of public techie ridicule or being seen as taking advantage of the community. I just hope that at some point I’ll be able to pay it forward to someone else.

Thanks!

Here’s my code:

package CS285.Assignment2;

import android.app.Activity;
import android.os.Bundle;
import android.app.ListActivity;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;


public class IceCreamMenu extends ListActivity {
    TextView selection;
    ImageView imgView;

    String[] items={"Butter Pecan", "Chocolate", 
            "Coffee", "Mango", 
            "Rocky Road","Strawberry", 
            "Vanilla"};

    int[] images={R.drawable.butterpecan,
            R.drawable.choclate,
            R.drawable.coffee,
            R.drawable.mango,
            R.drawable.rockyroad,
            R.drawable.strawberry,
            R.drawable.vanilla};

    public static final int IMG_ID = 0;
    public static final int INGR_ID = 1;
    public static final int ORDER_ID = 2;
    public static final int OTHER_ID = 3;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        selection=(TextView)findViewById(R.id.selection);
        imgView = (ImageView)findViewById(R.id.Image);

        setListAdapter(new ArrayAdapter<String>(this,
                                                android.R.layout.simple_list_item_1, 
                                                items));
        registerForContextMenu(getListView()); //indicate that the ListView has a context menu.

    }

    //event handling for ListItemClick events
    public void onListItemClick(ListView parent, View v,
                        int position, long id) {

        selection.setText(items[position]);

    }

    @Override
    public void onCreateContextMenu( //called each time the context menu is requested
            ContextMenu menu,   //the ContextMenu itself
            View v, //The view for which the context menu is being built
            ContextMenu.ContextMenuInfo menuInfo) { //tells you which item in the list the user did the tap-and-hold over 

            populateMenu(menu);

    }

    private void populateMenu(Menu menu) {
        menu.add(Menu.NONE, IMG_ID, Menu.NONE, "Picture");
        menu.add(Menu.NONE, INGR_ID, Menu.NONE, "Ingredients");
        menu.add(Menu.NONE, ORDER_ID, Menu.NONE, "Order");
        menu.add(Menu.NONE, OTHER_ID, Menu.NONE, "Other");

    }

    private boolean applyMenuChoice(MenuItem item) {
        switch (item.getItemId()) {
        case IMG_ID:

            imgView.setImageResource(images[0]);

            return(true);

        case INGR_ID:


            return(true);

        case ORDER_ID:


            return(true);

        case OTHER_ID:


            return(true);
        }

        return(false);
    }

    //called whenever an item in a ContextMenu is selected.
    @Override
    public boolean onContextItemSelected(MenuItem item) { //item is the menu item chosen
        return(applyMenuChoice(item) ||  
        super.onContextItemSelected(item));
    }


}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T20:46:46+00:00Added an answer on May 17, 2026 at 8:46 pm

    The short answer is cast menuInfo to an AdapterContextMenuInfo object and then access its public position member variable

    selectedPostion = ((AdapterView.AdapterContextMenuInfo)menuInfo).position;
    

    The value of selectedPostion is the position in the ListView that was long-clicked.

    This article in the Android Developer Guide has an excellent description of how to work with the ContextMenuInfo object, a must read:

    http://developer.android.com/guide/topics/ui/menus.html#context-menu

    As you can see in your onCreate() method your ListView is registering itself to respond to long-clicks and generate a ContextMenu. So when you long-click on a View item in the ListView, it is the ListView that is providing your onCreateContextMenu method with the menuInfo parameter. This parameter contains information about how the ContextMenu was generated.

    http://developer.android.com/reference/android/view/ContextMenu.ContextMenuInfo.html

    The ListView extends the AdapterView class. Which has AdpterView.AdapterContextMenuInfo subclass. Whenever the ListView object is long clicked, it sets up this parameter with information about what View object was long clicked. This object then gets passed to the onCreateContextMenu(…) method.

    http://developer.android.com/reference/android/widget/AdapterView.AdapterContextMenuInfo.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I’m having a lot of trouble understanding how to create a module that will
I'm having a lot of trouble writing this regular expression: (?<=\s+|^\s*|\(\s*|\.)(?:item|item1|item2)(?=\s+|\s*$|\s*\)|\.) It works very
I'm having a lot of trouble with a script i'm writing that will upload
having a lot of trouble setting up polymorphic. I have Comments which works fine,
I'm having a lot of trouble getting my priority queue to recognize which parameter
http://ftp.crashboxcreative.com/ftp/EastsideBaptist/EBC-Final/ I'm having a lot of trouble with a simple jquery show/hide effect. When
I'm having a lot of trouble with what seems like a very simple thing.
I'm having a lot of trouble doing something very simple. I have a viewController
I'm having a lot of trouble understanding the purpose of templates in ATL/WTL code.
Why I am asking I have been having a lot of trouble understanding the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.