Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create an Android Spinner as a popup?

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              
like image 921
Edwin Evans Avatar asked Jun 09 '11 00:06

Edwin Evans


People also ask

How do you make a clickable spinner?

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.

How do I add a spinner to my toolbar?

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.


2 Answers

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!

like image 175
Nathan Schwermann Avatar answered Oct 06 '22 01:10

Nathan Schwermann


In xml there is option

android:spinnerMode="dialog" 

use this for Dialog mode

like image 29
user2582324 Avatar answered Oct 06 '22 01:10

user2582324