1

I have button in my application and when I click on this button, it opens a spinner, but spinner is in dropdown mode and I need to make it in dialog mode. For API 11 and higher, there is a simple code which does the trick:

Spinner s1 = new Spinner(this, Spinner.MODE_DIALOG);

But I need to use some code which is also for API for 7 and higher. Could anybody help me, please?

Adam
  • 2,152
  • 4
  • 35
  • 45

2 Answers2

0

This is how i did:

Here, questions is String[];

DialogInterface.OnClickListener questionDialogListener = new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface arg0, int arg1) {

// implement the coding for getting the selected item.



                arg0.dismiss();
            }
        };

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Select Question:");
        builder.setItems(questions, questionDialogListener);

        AlertDialog dialog = builder.create();
        dialog.show();
    }
Chintan Soni
  • 24,761
  • 25
  • 106
  • 174
  • Yes, but this is dialog and I need spinner, not dialog, but spinner in dialog mode or something similar to this. – Adam May 08 '13 at 17:30
  • @Adam Is this what you're lokking for...? http://stackoverflow.com/questions/3673717/set-spinner-within-custom-dialog ---or--- http://stackoverflow.com/questions/6286847/how-do-i-create-an-android-spinner-as-a-popup – Chintan Soni May 09 '13 at 01:36
  • No, I need to show my spinner as dialog and not in dropdown mode. – Adam May 09 '13 at 09:04
0

I DID IT THIS WAY:

    LayoutInflater inflater = context.getLayoutInflater();
    final View dlg =inflater.inflate(R.layout.dialog,null);             

    final AlertDialog d = new AlertDialog.Builder(context)
        .setView(dlg)
        .setPositiveButton("SAVE",
                new Dialog.OnClickListener() {
                    public void onClick(DialogInterface d, int which) {
                        //Do nothing here. We override the onclick
                    }
                })
        .setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                  //This will close dialog
               }
           })
        .create();

        d.setOnShowListener(new DialogInterface.OnShowListener() {              
            @Override
            public void onShow(DialogInterface dialog) {    
                final Spinner mySpinner = (Spinner)dlg.findViewById(R.id.spinner);
                ArrayAdapter<CharSequence> adapter =  new ArrayAdapter<CharSequence>(context,android.R.layout.simple_spinner_item );
            //ADD VALUES TO adapter
                adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);                 
                mySpinner.setAdapter(adapter);
                Button b = d.getButton(AlertDialog.BUTTON_POSITIVE);
                b.setOnClickListener(new View.OnClickListener() {               
                    @Override
                    public void onClick(View view) {
                        d.dismiss();
                        //DO SOMETHING
                    }
                });
            }
        });             
        d.show();
JannGabriel
  • 302
  • 5
  • 14