I want to bring up a spinner dialog when the user taps a menu item to allow the user to select an item.
Do I need a separate dialog for this or can I use Spinner directly? I see this link, mentions a MODE_DIALOG option but it doesn't seem to be defined anymore. AlertDialog may be OK but all the options say "clicking on an item in the list will not dismiss the dialog" which is what I want. Any suggestion?
Ideally, the code would be similar to the case where the spinner is shown on the screen:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(activity,
android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
myspinner.setAdapter(adapter);
// myspinner.showAsDialog() <-- what i want
Try this:
See this link for more details.
This is from the Android SDK source code. As you can see you have a special constructor to create a Spinner with the specified mode you wanna use.
Hope it will help you :)
You can use a spinner and set the spinnerMode to dialog, and set the layout_width and layout_height to 0, so that the main view does not show, only the dialog (dropdown view). Call performClick in the button click listener.
Layout:
The advantage of this is you can customize your spinner any way you want.
See my answer here to customize spinner: Overriding dropdown list style for Spinner in Dialog mode
You can create your own custom Dialog. It's fairly easy. If you want to dismiss it with a selection in the spinner, then add an
OnItemClickListener
and addas in the OnClickListener for the OK button. There's one caveat, though, and it's that the onclick listener does not fire if you reselect the default option. You need the OK button also.
Start with the layout:
res/layout/spinner_dialog.xml:
Then, create the class:
src/your/package/SpinnerDialog.java:
Finally, use it as:
Here is an Spinner subclass which overrides performClick() to show a dialog instead of a dropdown. No XML required. Give it a try, let me know if it works for you.
For more information read this article: How To Make Android Spinner Options Popup In A Dialog