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?

  • Fake Incoming Call on an Android device
  • How to clear the Android Stack of activities?
  • Why are there extra pixels around my Android GridView?
  • read and write data with GSON
  • Supported @SuppressWarnings Values in Android Studio
  • How can an app detect that it's going to be uninstalled?
  • 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             
    

    Related posts:

    Transfer real-time video stream to server using Android
    Always fade in from placeholder with Picasso
    Jacinto6 board with Android: how to build a portrait firmware?
    CollapsingToolbarLayout: Change home button color when expanded
    How to send a high-priority GCM?
    Android, pausing and resuming handler callbacks
  • Toolbar subtitle not appearing
  • How to use the Radaee Pdf reader sdk
  • Android: WebView's method goBack() shows a blank page
  • How to get Ringtone name in Android?
  • How is Android permission enforced?
  • Send post request using Volley and receive in PHP
  • 9 Solutions collect form web for “How do I create an Android Spinner as a popup?”

    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

    Try this:

    Spinner popupSpinner = new Spinner(context, Spinner.MODE_DIALOG);
    

    See this link for more details.

    MODE_DIALOG and MODE_DROPDOWN are defined in API 11 (Honeycomb). MODE_DIALOG describes the usual behavior in previous platform versions.

    Adding a small attribute as android:spinnerMode="dialog" would show the spinner contents in a pop-up.

    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 add

    int n = mSpinner.getSelectedItemPosition();
    mReadyListener.ready(n);
    SpinnerDialog.this.dismiss();
    

    as 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:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content">
    <TextView 
        android:id="@+id/dialog_label" 
        android:layout_height="wrap_content" 
        android:layout_width="fill_parent"
        android:hint="Please select an option" 
        />
    <Spinner
        android:id="@+id/dialog_spinner" 
        android:layout_height="wrap_content" 
        android:layout_width="fill_parent"
        />
    <Button
        android:id="@+id/dialogOK" 
        android:layout_width="120dp"
        android:layout_height="wrap_content" 
        android:text="OK"
        android:layout_below="@id/dialog_spinner"
        />
    <Button
        android:id="@+id/dialogCancel" 
        android:layout_width="120dp"
        android:layout_height="wrap_content" 
        android:text="Cancel"
        android:layout_below="@id/dialog_spinner"
        android:layout_toRightOf="@id/dialogOK"
        />
    </RelativeLayout>
    

    Then, create the class:

    src/your/package/SpinnerDialog.java:

    public class SpinnerDialog extends Dialog {
        private ArrayList<String> mList;
        private Context mContext;
        private Spinner mSpinner;
    
       public interface DialogListener {
            public void ready(int n);
            public void cancelled();
        }
    
        private DialogListener mReadyListener;
    
        public SpinnerDialog(Context context, ArrayList<String> list, DialogListener readyListener) {
            super(context);
            mReadyListener = readyListener;
            mContext = context;
            mList = new ArrayList<String>();
            mList = list;
        }
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            setContentView(R.layout.spinner_dialog);
            mSpinner = (Spinner) findViewById (R.id.dialog_spinner);
            ArrayAdapter<String> adapter = new ArrayAdapter<String> (mContext, android.R.layout.simple_spinner_dropdown_item, mList);
            mSpinner.setAdapter(adapter);
    
            Button buttonOK = (Button) findViewById(R.id.dialogOK);
            Button buttonCancel = (Button) findViewById(R.id.dialogCancel);
            buttonOK.setOnClickListener(new android.view.View.OnClickListener(){
                public void onClick(View v) {
                    int n = mSpinner.getSelectedItemPosition();
                    mReadyListener.ready(n);
                    SpinnerDialog.this.dismiss();
                }
            });
            buttonCancel.setOnClickListener(new android.view.View.OnClickListener(){
                public void onClick(View v) {
                    mReadyListener.cancelled();
                    SpinnerDialog.this.dismiss();
                }
            });
        }
    }
    

    Finally, use it as:

    mSpinnerDialog = new SpinnerDialog(this, mTimers, new SpinnerDialog.SpinnerListener() {
      public void cancelled() {
        // do your code here
      }
      public void ready(int n) {
        // do your code here
      }
    });
    

    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.

    public class DialogSpinner extends Spinner {
        public DialogSpinner(Context context) {
            super(context);
        }
    
        @Override 
        public boolean performClick() {
            new AlertDialog.Builder(getContext()).setAdapter((ListAdapter) getAdapter(), 
                new DialogInterface.OnClickListener() {
                    @Override public void onClick(DialogInterface dialog, int which) {
                        setSelection(which);
                        dialog.dismiss();
                    }
                }).create().show();
            return true;
        }
    }
    

    For more information read this article: How To Make Android Spinner Options Popup In A Dialog

    android:spinnerMode="dialog"
    
    // Creating adapter for spinner
    
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, categories);
    
    // Drop down layout style - list view with radio button
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    
    // attaching data adapter to spinner
    spinner.setAdapter(dataAdapter);
    

    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 🙂

     /**
         * Construct a new spinner with the given context's theme, the supplied attribute set,
         * and default style. <code>mode</code> may be one of {@link #MODE_DIALOG} or
         * {@link #MODE_DROPDOWN} and determines how the user will select choices from the spinner.
         *
         * @param context The Context the view is running in, through which it can
         *        access the current theme, resources, etc.
         * @param attrs The attributes of the XML tag that is inflating the view.
         * @param defStyle The default style to apply to this view. If 0, no style
         *        will be applied (beyond what is included in the theme). This may
         *        either be an attribute resource, whose value will be retrieved
         *        from the current theme, or an explicit style resource.
         * @param mode Constant describing how the user will select choices from the spinner.
         * 
         * @see #MODE_DIALOG
         * @see #MODE_DROPDOWN
         */
        public Spinner(Context context, AttributeSet attrs, int defStyle, int mode) {
            super(context, attrs, defStyle);
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.