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 904997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:08:33+00:00 2026-05-15T16:08:33+00:00

Alright, this might be simple but I dunno how to do it! I have

  • 0

Alright, this might be simple but I dunno how to do it! I have my menu defined through XML, as shown below. It loads and everything.

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/home"
          android:title="Home" />
    <item android:id="@+id/about"
          android:title="About" />
    <item android:id="@+id/quit"
          android:title="Quit" />
</menu>

Now, going through onOptionsItemSelected(), how do I tell which menu item is selected?

This is from an example… What would the case’s be?

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case 1:
        Toast.makeText(this, "Home", Toast.LENGTH_LONG).show();
        return true;
    case 2:
        Toast.makeText(this, "About", Toast.LENGTH_LONG).show();
        return true;
    case 3:
        Toast.makeText(this, "Quit", Toast.LENGTH_LONG).show();
        return true;
    }
    return false;
}
  • 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-15T16:08:34+00:00Added an answer on May 15, 2026 at 4:08 pm

    Your case statements should use the ids defined in your xml:

    case R.id.home:
        ....
    case R.id.about:
        ....
    case R.id.quit:
        ....
    default:
        throw new IllegalStateException("oops, forgot to code something");
    

    the default case is just good practice imho. 🙂

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

Sidebar

Related Questions

Alright, I might make this confusing, but hopefully not. So I have a UITableView
Alright, this is melting my brain. It might have something to do with the
Alright I know this is more than likely a amateur question but I have
Alright, this one's interesting. I have a solution, but I don't like it. The
Alright, some of you might have noticed I've been working on this problem off
Alright this is my first day with JQuery so have fun with these functions
Alright I don't see why this isnt working. It seems pretty simple. Here is
Alright, this is driving me nuts because my regex is working on Rubular, but
Alright, this problem is a little complicated, so bear with me. I have a
Alright, this is a very simple question. I just installed Tomcat 6 on my

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.