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

  • Home
  • SEARCH
  • 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 8564737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:15:57+00:00 2026-06-11T17:15:57+00:00

I have made a context menu but I cant get the itemListener to work

  • 0

I have made a context menu but I cant get the itemListener to work because I cant use switch-case for the selected title(since it works only for Integer,Not string).

I want to do something upon clicking the item. Although I can do it with (IF statement).

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_overview);
        View view=findViewById(R.id.myView);
        registerForContextMenu(view);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);

    menu.setHeaderTitle("Context Menu");  
        menu.add(0, v.getId(), 0, "Item 1");  
        menu.add(0, v.getId(), 0, "Item 2");  
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {

        switch(item.getItemId()) {
        case 0: Toast.makeText(this, "Item selected 1", Toast.LENGTH_SHORT).show();

            break;
        case 1: Toast.makeText(this, "Item selected 2", Toast.LENGTH_SHORT).show();

            break;
        default:

    }

        return true;
    }
  • 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-06-11T17:15:58+00:00Added an answer on June 11, 2026 at 5:15 pm

    You’re setting both the IDs for your options to the same thing, so this won’t work. The second argument in the add() method is supposed to be a unique ID to allow you to determine the option clicked later on. Try something like:

    //Class level variables
    static int OPTION_ONE = 1;
    static int OPTION_TWO = 2;
    
    //Adding the options
    menu.add(0, OPTION_ONE, 0, "Item 1");  
    menu.add(0, OPTION_TWO, 0, "Item 2"); 
    
    //Determining which one was clicked
    switch(item.getItemId()) {
        case OPTION_ONE: 
            Toast.makeText(this, "Item selected 1", Toast.LENGTH_SHORT).show();
    
            break;
        case OPTION_TWO: 
            Toast.makeText(this, "Item selected 2", Toast.LENGTH_SHORT).show();
    
            break;
        default:
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I made my own right click context menu, and I have expandable options
i have made a few multi activity database applications using sqlite, but i get
I have a Menu made in MVVM, but my Menuitem are big as shown
I have made a jQuery toggle for a menu that I had in mind.
I have made a cart and the cart has remove item button, but the
I have a menu on the left of my page that is made of
I have a menu made with HTML, using a simple UL with lots of
I have made one custom ListView but it is not calling any onclick listener
Hello all i have made a tab menu in my sidebar (follow a tutorial),
I have made a website using( Asp.net, c# ) and its content in English

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.