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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:07:59+00:00 2026-05-24T13:07:59+00:00

As I am creating sub menu items dynamically , so its obvious that the

  • 0

As I am creating sub menu items dynamically , so its obvious that the MenuItems’s index will be dynamic only. so here what i facing the problem.

so far i have successfully create the menu items dynamically inside onCreateOptionsMenu function

SubMenu switchMenu = menu.addSubMenu("My Menu");

for(int i=0;i<myList.getListSize();i++){
 switchMenu.add(FILE, NEW_MENU_ITEM+i, 0, myList.get(i).data);  
}

Now the problem comes when Menu Items are selected inside onOptionsItemSelected

//get the the selected index 
int selectedMenuIndex = item.getItemId();    

// Pass it to a function in another activity 
myList.myActivity.switch(selectedMenuIndex);

//Finishing the current activity and loads the previously selected
finish();

Whenever the parent menu loads and i click on it to get the submenu , android automatically selects first submenu in the list i.e. index =0 and immediately fires the above code and it doesn’t wait for rest of the submenu to load and allow the user to select from the submens item that i have created dynamically.

  • 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-24T13:08:00+00:00Added an answer on May 24, 2026 at 1:08 pm

    Do this in your OnOptionsItemSelected

        public boolean onOptionsItemSelected(MenuItem item)
        {
    
         if( (item.getItemId() & NEW_MENU_ITEM ) == NEW_MENU_ITEM)  // check if its a sub menu ID
         {
    
           switch(item.getItemId() - NEW_MENU_ITEM)
           {
              case 0: // first sub menu option
              {
                 DoSubMenu1();
                 break;
              }
    
              case 1:: // second sub menu option
              {
                 DoSubMenu2();
                 break;
              }
    
              // and so on ..................
            }
    
            return;
         }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating a website that has 5 different sub-sites each with the same
I'm having trouble creating a sub that can create objects of a variable type
I'm creating a horizontal menu with sub-menus under it (fig c) but all I
Consider a dynamic dropdown-menu. Because of creating this code server side I don't know
I'm creating a sub dialog another vendor is calling. I'm having trouble reading the
Creating liquid layouts is an immense pain. Now, I totally understand that tables should
Creating a server-side socket will fail if I'm trying to use the same port
I'm attempting to setup a MenuItem that will have a submenu of page numbers
I am creating an object structure and I want all sub classes of the
imagine a (container-) application is creating multiple classloaders to host a couple of other/sub-

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.