EDIT: I needed the list to resize based on the quanity of items. It's rather hard to do it with a ListView. So I actually skipped using the adapter and just went with building rows on a vertical LinearLayout.
I starting to feel lazy again, so I just copy and pasted the CustomDropDown class and the necessary xml files at the bottom. But here are some notes about the code:
1. Create a CustomDropDown that extends Button. After all, the Spinner is just a button that opens a Dialog. You can copy the whole code at the bottom of this post.
- You might see something like isButtonAutoGen or something like that, setTextOnSelectedItems. These are basically related to showing the selected contents on the DropDown Button. If you look at the image below where the dropDown has text that says "Click to open dialog," then imagine that text is replaced by the values in the list. These values are separated by commas and ellipsizes if the content is too long.
- The method, createList, is where it populates the values into the drop down.
2. So now you just need the layouts for these rows and the dialog itself. I will post these at the bottom of the post as well. Animations and styles are included as well.
3. To use, make a new CustomDropDown. Populate data by passing in a text array or text resource into setAdapterFromResources. If you look at the image below, you will see a "Done" button. You may have to manual give it's clickListener the property to close the dialog.
Other things: You can set the multichoice mode and singlechoice mode. Both modes will use the checkbox drawable, since I haven't gotten around to using a radio button for the singlechoice mode.
I hope that the code below works for you. I haven't really tested it out by copy and pasting. Anyways, farewell.
Here is what I was trying to do with these playful things:
Create a spinner that opens a dialog from the bottom and stops about half way. The picture shows a Single-Choice mode, but it should be able to convert to Multi-Choice mode where it shows check-boxes instead of radio buttons. Also, the contents inside the ListView must be capable of modification.
Things that need to be created:
- anim, layout, and style XMLs
- adapter, checkable, and button implementations
I'm pretty tired right now...I'll add more to this section. In the mean time, you can refer to this:
The link above helped me on implementing Checkable. This is what binds the View of a row in a ListView to have a checkable state.
Another thing if you use the Single-Choice mode for the list, using the AdapterView.OnItemSelectedListener does not work here. Instead, use AdapterView.OnItemClickListner. This means if you want another component to be affected by the dropdown at the beginning of the activity, you would have to manually set the state of the component. Then the listener will take care of the rest afterwards.
When access data in the ListView, such as the checkbox value, all ways use the "Item Checked" methods. The Item Selected just does not work at all. So use those methods for both Singe and Multi-modes.
Anyways, time to flee......
Full CustomDropDown code: