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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:53:18+00:00 2026-06-08T06:53:18+00:00

I have a submenu and am trying to get the items to show the

  • 0

I have a submenu and am trying to get the items to show the currently selected state. As it’s a submenu I can’t call the menu methods. It shows as checked once it has been selected the first time, but I need to make it show when the menu is first inflated. Any ideas please?

public boolean onCreateOptionsMenu(Menu menu){
    MenuInflater minf= getMenuInflater();
    minf.inflate(R.menu.menu,menu);
    return true;
}



public boolean onOptionsItemSelected(MenuItem item){

    switch (item.getItemId()){
    //-------Options menu----------
    case R.id.about:
        Intent intentA = new Intent(FuelMoney.this, About.class);
        startActivity(intentA);
        return true;
    case R.id.locale:
        return true;

    //-----Sub menu---------- UK item not showing as clicked (rest of the code not complete yet)

        case R.id.uk_item:
            if(this.countryCode.equals("uk"))
            {
                item.setChecked(true);
            }
        Toast.makeText(this, "UK selected", Toast.LENGTH_SHORT).show();
        this.countryCode="uk";
        this.country = new Country(countryCode);
        this.regionAttributes();
        item.setChecked(true);
        return true;
    case R.id.us_item:
        Toast.makeText(this, "US selected", Toast.LENGTH_SHORT).show();
        this.countryCode="us";
        this.country = new Country(countryCode);
        this.regionAttributes();
        return true;
    case R.id.eu_item:
        Toast.makeText(this, "EU selected", Toast.LENGTH_SHORT).show();
        this.countryCode="eu";
        this.country = new Country(countryCode);
        this.regionAttributes();
        return true;
    case R.id.jpn_item:
        Toast.makeText(this, "Japan selected", Toast.LENGTH_SHORT).show();
        this.countryCode="jpn";
        this.country = new Country(countryCode);
        this.regionAttributes();
        return true;
    case R.id.india_item:
        Toast.makeText(this, "India selected", Toast.LENGTH_SHORT).show();
        this.countryCode="ind";
        this.country = new Country(countryCode);
        this.regionAttributes();
        return true;
    default :
        return super.onOptionsItemSelected(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-06-08T06:53:19+00:00Added an answer on June 8, 2026 at 6:53 am

    Got it. I needed to get the SubMenu and call methods on that.

    Here is the complete code for creating an options menu that displays a submenu of choices (in this case countries), which also displays the current setting:

    public boolean onCreateOptionsMenu(Menu menu){
            MenuInflater minf= getMenuInflater();
            minf.inflate(R.menu.menu,menu);
            return true;
        }
    
    
        public boolean onOptionsItemSelected(MenuItem item){
    
            switch (item.getItemId()){
            //-------Options menu----------
            case R.id.about:
            Intent intentA = new Intent(myClass.this, About.class);
            startActivity(intentA);
            return true;
        case R.id.locale:
    
                //-----Sub menu----------
            SubMenu sub = item.getSubMenu();
            sub.setGroupCheckable(R.id.locale, true, true);
            if(this.countryCode.equals("uk"))
            {
                sub.getItem(0).setChecked(true);
            }else if(this.countryCode.equals("us"))
            {
                sub.getItem(1).setChecked(true);
            }else if(this.countryCode.equals("eu"))
            {
                sub.getItem(2).setChecked(true);
            }else if(this.countryCode.equals("jpn"))
            {
                sub.getItem(3).setChecked(true);
            }else if(this.countryCode.equals("ind"))
            {
                sub.getItem(4).setChecked(true);
            }
    
    
            return true;
    
            case R.id.uk_item:
            this.subMenuHelper("uk");// see below for subMenuHelper
            item.setChecked(true);
            return true;
        case R.id.us_item:
            this.subMenuHelper("us");
            item.setChecked(true);
            return true;
        case R.id.eu_item:
            this.subMenuHelper("eu");
            item.setChecked(true);
            return true;
        case R.id.jpn_item:
            this.subMenuHelper("jpn");
            item.setChecked(true);
            return true;
        case R.id.india_item:
            this.subMenuHelper("ind");
            item.setChecked(true);
            return true;
        default :
            return super.onOptionsItemSelected(item);
    
            }
    
        }
    
    
    
        public void subMenuHelper(String cCode)
          {
                this.countryCode=cCode;
                this.country = new Country(this.countryCode);
                this.regionAttributes(this.country);
            }
    

    And the xml:

        <?xml version="1.0" encoding="utf-8"?>
    <menu
      xmlns:android="http://schemas.android.com/apk/res/android">
      <item android:id="@+id/locale"
            android:title="@string/menu_set_region"
            android:icon="@android:drawable/ic_menu_mapmode">
    
            <!--region submenu -->
        <menu>
        <group android:checkableBehavior="single">
            <item android:id="@+id/uk_item"
                android:title="@string/uk"
                />  
            <item android:id="@+id/us_item"
                android:title="@string/us"
                />
            <item android:id="@+id/eu_item"
                android:title="@string/eu"
                />
            <item android:id="@+id/jpn_item"
                android:title="@string/jpn"
                />
            <item android:id="@+id/india_item"
                android:title="@string/india"
                />
            </group>
            </menu>
        </item>
    
        <item android:id="@+id/about"
            android:icon="@android:drawable/ic_menu_info_details"
            android:title="@string/menu_about"
            />
     </menu>
    

    I hope theres enough here so you can fill in the blanks 🙂

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

Sidebar

Related Questions

I have a menu and i am trying to show and hide the submenu
I have this JQ drop down menu and i am trying to get it
I have a reference to the outermost menu, but I'm trying to get a
I'm trying to get a submenu so that I can make changes to it
I am currently trying to add a sub menu and get it to do
I have been trying to make the sub-menu horizontal. In my HTML it looks
I have a submenu that has all items linked to the same node, but
I am trying to show the last li as selected as well as initially
I am trying to design a menu and submenu like this . I tried
I am trying to get an image to appear next to a menu item

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.