views:

23

answers:

1

I would like to add radio buttons to my context menu, but I'm not sure how. This is how it is created:

@Override  
    public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {  

        super.onCreateContextMenu(menu, v, menuInfo);  
        menu.setHeaderTitle("Selection Options");  
        menu.add(0, v.getId(), 0, "Remove");  
    }  
A: 

from your code:

menu.add(0, v.getId(), 0, "Remove"); 

v is a View that can be a RadioButton or any other type of Views.

if you are not using xml to define RadioButton. you should set its ID in your application.

v.setId();

Or you can define ids.xml in res/values.

samples/ApiDemos/src/com/example/android/apis/RadioGroup1.java samples/ApiDemp/res/values/ids.xml

Menu & Context Menu developers guide:

http://developer.android.com/guide/topics/ui/menus.html

if you scroll the above page you can find RadioButton sample in context menu.

Quote form the above page:

Checkable menu items

A menu can be useful as an interface for turning options on and off, using a checkbox for stand-alone options, or radio buttons for groups of mutually exclusive options. Figure 2 shows a submenu with items that are checkable with radio buttons.

Note: Menu items in the Icon Menu (from the Options Menu) cannot display a checkbox or radio button. If you choose to make items in the Icon Menu checkable, you must manually indicate the checked state by swapping the icon and/or text each time the state changes.

You can define the checkable behavior for individual menu items using the android:checkable attribute in the element, or for an entire group with the android:checkableBehavior attribute in the element. For example, all items in this menu group are checkable with a radio button:

> <?xml version="1.0" encoding="utf-8"?>
> <menu
> xmlns:android="http://schemas.android.com/apk/res/android"&gt;
>     <group android:checkableBehavior="single">
>         <item android:id="@+id/red"
>               android:title="@string/red" />
>         <item android:id="@+id/blue"
>               android:title="@string/blue" />
>     </group> </menu> The android:checkableBehavior attribute

accepts either:

single Only one item from the group can be checked (radio buttons) all All items can be checked (checkboxes) none No items are checkable You can apply a default checked state to an item using the android:checked attribute in the element and change it in code with the setChecked() method.

When a checkable item is selected, the system calls your respective item-selected callback method (such as onOptionsItemSelected()). It is here that you must set the state of the checkbox, because a checkbox or radio button does not change its state automatically. You can query the current state of the item (as it was before the user selected it) with isChecked() and then set the checked state with setChecked(). For example:

> @Override public boolean
> onOptionsItemSelected(MenuItem item) {
> switch (item.getItemId()) {   case
> R.id.vibrate:   case
> R.id.dont_vibrate:
>     if (item.isChecked()) item.setChecked(false);
>     else item.setChecked(true);
>     return true;   default:
>     return super.onOptionsItemSelected(item);   }
> } 

If you don't set the checked state

this way, then the visible state of the item (the checkbox or radio button) will not change when the user selects it. When you do set the state, the Activity preserves the checked state of the item so that when the user opens the menu later, the checked state that you set is visible.

Note: Checkable menu items are intended to be used only on a per-session basis and not saved after the application is destroyed. If you have application settings that you would like to save for the user, you should store the data using Shared Preferences.

mohammad shamsi
I am not inflating my context menu from XML. how can I do it in code?
Sheehan Alam
@Sheehan: answer edited. look at first part.
mohammad shamsi
not sure if i get what you mean. what would I set for v.setId()?
Sheehan Alam
in order to add a View (RadioButton here) to a Menu you have to add method in menu. (manu.add(...)) the second parameter in this method is your view id (RadioButton id). if you created your RadioButton by code, you need to set its id by code too. but if you are loading it from xml then its id already assigned.
mohammad shamsi