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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:24:11+00:00 2026-05-31T16:24:11+00:00

I have a button which opens a context menu with a list of various

  • 0

I have a button which opens a context menu with a list of various options. Only one option can be selected at a time so I want to have a radio button next to each of them highlighting which item is currently selected. When I select an item from the context menu the radio button is selected and closes. Once I click the button to open the context menu the previously selected item is not selected.

How do I get the context menu to remember which item was previously selected?

Secondly, when the activity is first created a default option is selected. How do I set an initial default which can then be overwritten when another context menu item is selected? I can set android:checked="true" in the XML but can this be overwritten when a different item is selected?

Java:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.browse);
    Button buttonView = (Button) this.findViewById(R.id.button_view);
    buttonView.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            registerForContextMenu(v);
            openContextMenu(v);
        }
    });
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    menu.setHeaderTitle(R.string.menu_title);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.context_menu, menu);
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    switch(item.getItemId()) {
        case(R.id.item1):
            if(item.isChecked()) {
                item.setChecked(false);
            } else {
                item.setChecked(true);
            }
            break;
        case(R.id.item2):
            if(item.isChecked()) {
                item.setChecked(false);
            } else {
                item.setChecked(true);
            }
            break;
        case(R.id.item3):
            if(item.isChecked()) {
                item.setChecked(false);
            } else {
                item.setChecked(true);
            }
            break;
        default:
            return super.onContextItemSelected(item);
    }
    return true;
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group android:id="@+id/context_menu" android:checkableBehavior="single">
        <item android:id="@+id/item1"
            android:title="@string/context_menu_item1" />
        <item android:id="@+id/item2"
            android:title="@string/context_menu_item2" />
        <item android:id="@+id/item3"
            android:title="@string/context_menu_item3" />
    </group>
</menu>

Any assistance would be 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-05-31T16:24:12+00:00Added an answer on May 31, 2026 at 4:24 pm

    The problem is that your items don’t save the state between displaying of menu.
    So each time you call setChecked, it works only for the currently shown menu and resets for the next.

    You should save the checked state in an external structure like a boolean array for example.

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.setHeaderTitle(R.string.menu_title);
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.context_menu, menu);
    
        // loop for menu items
        for (int i = 0; i < menu.size(); ++i) {
            MenuItem mi = menu.getItem(i);
            // check the Id as you wish
            if (mi.getItemId() == R.id.item2) {
                mi.setChecked(true); 
            }
        }
    }
    

    In order to search a menu item, you can also use the findItem function:

    MenuItem mi = menu.findItem(R.id.item2)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a div which opens when I click a menu button, I am
I have UserControl which contains a TextBox and a Button control. The Button opens
I have inherited a c# class 'Button' (which I can't change) which clashes with
I have one general context menu and for example 4 buttons and 4 textboxes
I have a Toolbarbutton <toolbarbutton context=TabMenue id=esbTb_rss_reader label=News type=menu> with a Context menue which
Is there any way to know the parent/component which initiates context menu? I have
I have a button which I wanted to flash briefly to get the user's
I have a button which is in update panel. When I click on button
Using Flex 3, I have a Button which is skinned using PNGs specified in
I have a Clear button which sets the gridview datasource to null and binds

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.