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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:12:33+00:00 2026-06-07T06:12:33+00:00

I have stared my eyes red on this: I use ABS and everything works

  • 0

I have stared my eyes red on this:

I use ABS and everything works perfectly under Android 2.2, but on ICS item.getItemId() always returns 0. Since item.toString() returns different values. I feel I should be able to solve this, but I alway end up on the Info-page.

public boolean onCreateOptionsMenu(Menu menu) {
    menu.add("Info")
        .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
    menu.add("Preferences")
        .setIcon(R.drawable.ic_preferences)
        .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    if(item.toString() == "Preferences"){
        Intent intent = new Intent();
        intent.setClass(this,Preferences.class);
        startActivity(intent);
        return true;
    }
    else {
        Intent intent = new Intent();
        intent.setClass(this,Info.class);
        startActivity(intent);
        return true;
    }
}

I guess I should switch on item.getItemId() instead, but I can’t figure out where to define the ActionBar Views. For some time I have puzzled with a file actionbar.xml under the menu folder, but with little success:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@+id/preferences" 
        android:showAsAction="always" 
        android:icon="@drawable/ic_preferences"></item>
    <item android:id="@+id/info" 
        android:showAsAction="always"></item>
</menu>

One should believe the the answer is rather simple. Can anyone give me a point in the right direction on an approach that works the same from SDK version 7 throught 15?

Any help is greatly appreciated.

  • 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-07T06:12:34+00:00Added an answer on June 7, 2026 at 6:12 am

    Try this:

    private static final int MENU_INFO = 0;
    private static final int MENU_SETTINGS = 1;
    
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    
        menu.add(0, MENU_INFO, 0, "Info")
             .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
    
        menu.add(0, MENU_SETTINGS, 0, "Preferences")
            .setIcon(R.drawable.ic_preferences)
            .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
    
        return true;
    }
    
    /* Handles item selections */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case MENU_INFO:
            startActivity(new Intent(this, Info.class));
            return true;
        case MENU_SETTINGS:
            startActivity(new Intent(this, Preferences.class));
            return true;
        }
        return false;
    }
    

    The trick is indeed to use item.getItemId(); as you can never be sure what exactly is returned on a .toString(). By assigning a static int-reference to each menu-item when you create the menu options, you know which one has been clicked in the onOptionsItemSelected-method.

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

Sidebar

Related Questions

I know this is a common problem but I have stared, read googled and
I have looked on here and found a few examples but nothing works so
Sorry to be posting this question. :( But I have spent almost the entire
I have stared at this short code too long, and cannot for the life
I really need a second pair of eyes on this... I have a Stored
I have been reading and applying the android design from this page: http://developer.android.com/design/index.html I
I have started to try out to use the new Search API, the demo
I have started implementing Subscriptions into my app but I cannot get the API
I have stored data in hidden <input> tags, but it caused reduction in performance.
I have this while loop in my code. The loop seems to work fine

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.