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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:47:14+00:00 2026-05-31T18:47:14+00:00

My problem is how to switch from context menu to correct activity. I have

  • 0

My problem is how to switch from context menu to correct activity.

I have such activities:

  • Main
  • AccelerometerOptionsActivity
  • GyroscopeOptionsActivity
  • OrientationOptionsActivity

In main activity I have a list of sensors. When I click the sensor, context menu appears where I can click e.g. Options.

My problem is how to switch from context menu to options activity of chosen sensor.
My code:

@Override
    public boolean onContextItemSelected(MenuItem item) {
      AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
      int menuItemIndex = item.getItemId();
      String[] menuItems = getResources().getStringArray(R.array.sensor_array);
      String menuItemName = menuItems[menuItemIndex];


      if(item.getTitle()=="Start Service"){
          Toast.makeText(this,"Start " + menuItemName+ " selected", Toast.LENGTH_SHORT).show();
      } else if(item.getTitle()=="Stop Service") {
          Toast.makeText(this,"Stop " + menuItemName+ " selected", Toast.LENGTH_SHORT).show();
      } else if(item.getTitle()=="Options") {


              Intent options = new Intent(this, AccelerometerOptionsActivity.class);
                startActivity(options);

      }

      return true;   
    }

UPDATE:

Here is the code:

 @Override 
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { 
         if (v.getId()==R.id.list) {  
            AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;  

            menu.setHeaderTitle(sensorsArray[info.position]);
            menu.add(Menu.NONE, CONTEXTMENU_START, 0,  "Start Service");
            menu.add(Menu.NONE, CONTEXTMENU_STOP, 1,  "Stop Service");
            menu.add(Menu.NONE, CONTEXTMENU_OPTIONS, 2, "Options"); 
            menu.add(Menu.NONE, CONTEXTMENU_GRAPHS, 3, "Graph view");
            menu.add(Menu.NONE, CONTEXTMENU_DATA, 4, "Data view");
         }  
    }
  • 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-31T18:47:16+00:00Added an answer on May 31, 2026 at 6:47 pm

    Try this in your onContextItemSelected():

        @Override
        public boolean onContextItemSelected(MenuItem item) {
            AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item
                        .getMenuInfo();
            int menuItemIndex = item.getItemId(); // the position of the item in the list with the context menu
            String[] menuItems =  getResources().getStringArray(R.array.sensor_array);
            String menuItemName = menuItems[menuItemIndex];
            switch (item.getItemId()) {
            case CONTEXTMENU_START:
                Toast.makeText(this, "Start " + menuItemName + " selected",
                            Toast.LENGTH_SHORT).show();
            break;
            case CONTEXTMENU_STOP:
                Toast.makeText(this, "Stop " + menuItemName + " selected",
                        Toast.LENGTH_SHORT).show();
            break;
                case CONTEXTMENU_OPTIONS:
            // here start the correct options activity either by checking for
            // String equality with menuItemName or by doing a switch on the menuItemIndex(this works
            // if your list of sensors is based on the array R.array.sensor_array)
                switch (menuItemIndex) {
                  case 0:
                  //the user clicked the first sensor in the list so start that option activity           
                  break;
                  case 1:
                    //the user clicked the second sensor in the list so start that option activity
                      break;
                  }
          }
          return super.onContextItemSelected(item);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a problem with switch view between 2 views with nib files !
So I have this problem with strings and switch-case, and I'll try to keep
I have problem in some JavaScript that I am writing where the Switch statement
Problem: I have an address field from an Access database which has been converted
Problem: Given a list of strings, find the substring which, if subtracted from the
I have some trouble sending an Intent from a Service to an Activity. Here's
I experience the following problem while using legacy VC6. I just cann't switch to
Problem: I have two spreadsheets that each serve different purposes but contain one particular
I have six menu options in my webapp, and when the user hovers over
I've got quite a problem with UIScrollView - I have a SwitchViewController (which is

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.