Home » Android » android – How to make an alert dialog with a spinner

android – How to make an alert dialog with a spinner

Posted by: admin June 15, 2020 Leave a comment


I know how to make an alert dialog but I need to make one with a spinner so that when it pops up the person will have an option as to what happens. Does anyone have the code for an alert dialog with a spinner or know any good tutorials?

Thanks in advance

How to&Answers:
LayoutInflater li = LayoutInflater.from(context);

View promptsView = li.inflate(R.layout.my_dialog_layout, null);

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);


// set dialog message

alertDialogBuilder.setTitle("My Dialog..");
// create alert dialog
final AlertDialog alertDialog = alertDialogBuilder.create();

final Spinner mSpinner= (Spinner) promptsView
final Button mButton = (Button) promptsView

// reference UI elements from my_dialog_layout in similar fashion

mSpinner.setOnItemSelectedListener(new OnSpinnerItemClicked());

// show it


my_dialog_layout is the popup layout which you contains the Spinner mySpinner


public class OnSpinnerItemClicked implements OnItemSelectedListener {

        public void onItemSelected(AdapterView<?> parent,
                View view, int pos, long id) {
            Toast.makeText(parent.getContext(), "Clicked : " +
                    parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();


        public void onNothingSelected(AdapterView parent) {
            // Do nothing.