So I have a submenu that I have for an options menu item. I want a list of checkable entries that the user can select/deselect as many as they want. The only problem I can't solve is how to prevent the option menu from closing when one of the checkboxes is clicked. I saw the performShortcut has a FLAG_PERFORM_NO_CLOSE flag, but I'm not sure how to use that method. I've tried many things, but I'm confused on where the keyevent is supposed to come from or if this is even the right method I should be looking at.
So tl;dr: How do I prevent options menus/submenus from closing when an option is selected?
The way I would handle this is using the standard alert dialog class. In your menu handler, create an AlertDialog and pass an array of your options to the Builder.
The method you should pay attention to is AlertDialog.Builder.setMultiChoiceItems(CharSequence[] items, boolean[] checkedItems, DialogInterface.OnMultiChoiceClickListener listener)
Pass an array to this method and put your submenu selection code in the ClickListener.
Related
I want to know if there is a specific reason why you cannot use OnItemClickListener with a spinner in Android? Looking through older posts ( setOnItemClickListener Not Works with Spinner and I have an error: setOnItemClickListener cannot be used with a spinner, what is wrong?) it seems as though people don't even bother with OnItemClickListener and rather go straight for OnItemSelectedListener. Why does Eclipse give OnItemClickListener as an option for a spinner if it can never work?
Some context- I'm not really too bothered by which method I use. As soon as the user selects an item from the spinner, I want to make a second spinner visible and populate it from the database based on the option selected in the first spinner. Now when I use OnItemSelectedListener, the second spinner is immediately set to visible. Is there a workaround for this?
Why does Eclipse give OnItemClickListener as an option for a spinner if it can never work?
Because everything extending AdapterView has setOnItemClickListener(). Just because some subclasses ignore it does not mean that the setter magically disappears.
Is there a workaround for this?
There is no workaround, because there is no problem, because it is doing exactly what you say you want to have happen ("I want to make a second spinner visible and populate it from the database based on the option selected in the first spinner"). A Spinner always has a selection if there is data to select from. Hence, your second Spinner should always be visible, since it is "based on the option selected in the first spinner", and there will always be an "option selected in the first spinner".
It sounds like what you really want is to only have the second Spinner visible for specific selections in the first Spinner, where you set up the first Spinner where there is some selection that, in business terms if not technical terms, means "no selection". You are welcome to do this, but then you will have to implement the logic to handle this, only showing the second Spinner when an appropriate value is selected in the first Spinner.
I want to have a dropdownlist like this:
But i want to pop it up when you click a menu item. So spinner is not a necessary. Is there a standalone dropdownlist, or is it possible i trigger the spinner automatically?
You can do this a few ways:
Give your item a submenu by putting another <menu> element inside the <item> tag.
Handle the item click yourself and show a ListPopupWindow
Create a DialogFragment. Override onCreateDialog() and use an AlertDialog.Builder to create the dialog. Call one of the variations of setSingleChoiceItems() on the builder to set a list of single choice items to show in the content.
The first method is maybe the easiest since the selection will call back to your onOptionsItemSelected() method and you can easily handle it there, but the other options may be of interest to you as well.
If you want to show a dropdown list on an menu item click, you need to use the Spinner object.
Alternatively you can also use the Action bar with drop-down navigation : http://developer.android.com/guide/topics/ui/actionbar.html#Dropdown
I wish to select multiple ListView items and then share all of them. How can I go about it?
See the Awesome example here android-multiple-selection-listview
Well,
First you need to create OnItemClickListener(View v) in order to get clicks.
Then you can make a HashMap in order to preserve the items which are clicked.
You can also change the Background of the clicked item so the user will know which items are clicked.
Final thing you need is the action (button click - Share) which will pass through the HashMap and collect all clicked items. After that you can do with them what ever you want.
It looks like a huge work, but it's can easily divided into steps and you close them one by one... It actually quite simply.
I want that on the click of the option menu item in action bar, the listview's choice mode becomes multiple, which is easy to achieve. Simultaneously i want to inflate two options menu items namely "ok" and "cancel", so that once the user is done selecting some or all listview items, he can click on "ok" option menu to proceed.
I have a 2d array of strings. I would like to use a spinner to allow users to select values from the 2nd lvl of the array but I would like to display the selection options in a expandablelistview using the values of the 1st lvl of the array as category headers.
Is this possible, can someone point me in the right direction of how this should be implemented ?
You cannot directly use an ExpandableListView for the Spinner popup, any more than you can directly use a ListView for the Spinner popup. It is what it is, and that is defined by the Spinner class.
Options include:
Subclassing Spinner and overriding whatever drives the popup dialog
Copying Spinner into your project and replacing whatever drives the popup dialog (if overriding will not work due to method visibility, etc.)
Don't use a Spinner, and instead use a Button plus an AlertDialog containing an ExpandableListView, or something like that
In principle, you could override getDropDownView() in your SpinnerAdapter (e.g., ArrayAdapter). However, there is no concept of the drop-down view being disabled, and I assume you would want your category headers to be non-selectable. If, however, selecting a category is acceptable, this approach would be simplest.
I'm new to Android. I have a list of items and trying to associate single context menus to each list item. I have set setListAdapter and onListItemClick but when I click on any list item I always get the same context menu.
Ideally, list item A should trigger menu A when cliccked and list item B should get menu B. Can't figure out how to do it. Could anybody help me find an example code I could use to learn how to do it?
I don't have any example code that shows the technique -- my best example is something I did for a consulting client.
However, let me point you to this sample project that uses context menus and use it as a basis for this explanation.
You need to return the customized menu in onCreateContextMenu(). If you always return the same menu here, you will always see the same menu. To determine which menu to display, you will need to know which list item was long-tapped. In the case of a context menu for a ListView, you can cast the ContextMenu.ContextMenuInfo supplied to onCreateContextMenu() to be an AdapterView.AdapterContextMenuInfo. That object can tell you the position and _ID of the item in the list that was long-tapped, so you can choose the proper menu.
In the sample code linked to above, I do that cast in onContextItemSelected(), so I can know which item the user is deleting. However, the same cast works in onCreateContextMenu().
The Android team recently released a number of new samples. I believe what you're trying to do has an excellent example here.