I have an options menu with an “Add/Remove” option that, when clicked, shows a checkable list. The problem with the code that I currently have is that you can only select one item at a time, and the menu disappears. I want to be able to check multiple items in the list at once, and for it not to disappear until the user touches a spot elsewhere on the screen. How can I do this? Here’s the general idea of what I have:
<?xml version="1.0" encoding="UTF-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/select_options"
android:title="Add/Remove">
<menu>
<group android:checkableBehavior="all">
<item android:id="@+id/A"
android:checked="true"
android:title="Option One" />
<item android:id="@+id/B"
android:checked="true"
android:title="Option Two" />
</group>
</menu>
</item>
</menu>
and
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.selection_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.A:
item.setChecked(!item.isChecked());
return true;
case R.id.B:
item.setChecked(!item.isChecked());
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Hello TheBeatlemaniac !
I honestly don’t know if what you are seeking for is doable or not ( EDIT : in the way you are implementing it, as a sub menu ), but I would have done it this way:
Create an activity that looks like the sub menu you want to display.
It might seem a little bit more complicated but it is straight forward and in this manner, it will not disappear if you select / deselect an item, and you can implement much more functionality.
Here’s how I would have personally done it:
As you can see, the class implements Serializable so that objects of that class can be passed from an activity to another using intents/bundles.
The list (ArrayList) is used to host all your setting sub menu items with the check boxes.
As you can see, each SettingCheckBox object has a description and a state (checked or unchecked). By default, once create, the object state is unchecked.
You should initialize the list inside the onCreate method.
The static and final variable SETTING_CHECK_BOX is used as key to save/restore the content of that list before/after activity recreations (like a screen rotation), and also to pass the settings list from an activity to another. (explained later on)
No need for the sub menu anymore since you will be implementing an activity that acts like one.
Now, to link the menu item to the activity that will display the settings, you should use the onOptionsItemSelected method inside your current activity like this :
The settings activity is started for a result. It means like it behaves as a child activity, and can return a result to its parent activity.
The settings list is passed to the settings activity via the intent.
If the child activity ends and returns data to the parent activity, the following method is called :
You should make the child / settings activity return the (new/modified) list of settings, and as demonstrated above, the new list is set.
This is the layout of the activity that will act as your sub menu. It is actually a list activity, and can hold as many options as you want (you just add them in the array list declared in your activity above).
This is the layout of each row in the list, there is a text view and check box ( just like the sub menu you were already using).
This class represents the activity that will act as your sub menu. As I previously said, it is a List Activity (and hence should extend ListActivity). In order to display the various options inside the list, you need an adapter (array adapter is sufficient for this case), that’s the role of the MyActivity_Settings_Adapter class (that extends ArrayAdapter ).
If the list activity finishes (the user clicks on the back button, or anywhere outside the activity which is displayed as a dialog), it (the activity) returns to the parent activity the new list of options with the new checked values.
The adapter will build each row for the list to display.
In addition, the adapter will assign a click listener for every check box, so that if checked (or unchecked) the option will be modified accordingly.
And if you click anywhere outside the sub menu (or simply press on the back button), the sub menu will disappear, and the user selections are preserved in the boolean array in your main activity.
If you are not familiar with ListActivity and ArrayAdapter, this tutorial would help a lot !
The theme applied (@android:style/Theme.Dialog) will make the activity look like a sub menu.
Hope it helps !
I tried it and it works perfectly ! Try it and let me know what happens.