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 removing the xml attribute android:clickable="true" from the Spinner widget. It could be that the entire spinner is registering the click event rather than the individual spinner items.
Adding spinner to app bar/ toolbar is very simple, you just need to create a XML file in res/menu/ folder and add a item like your over flow menu and spinner widget as item actionViewClass, rest in your java code. Spinner can be added to android actionbar/toolbar with many ways.
You can use an alert dialog
AlertDialog.Builder b = new Builder(this); b.setTitle("Example"); String[] types = {"By Zip", "By Category"}; b.setItems(types, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); switch(which){ case 0: onZipRequested(); break; case 1: onCategoryRequested(); break; } } }); b.show();
This will close the dialog when one of them is pressed like you are wanting. Hope this helps!
In xml there is option
android:spinnerMode="dialog"
use this for Dialog mode
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With