0

I am creating an android app by using TabActivity, in my TabActivity I have a button on click on the button I want to popup an alertDialog box, I need to add a spinner to the alert dialog box.

The code for button click is given below

addMedicalRCD.setOnClickListener(new OnClickListener() 
{

    public void onClick(View arg0) 
    {
        View layout = inflater.inflate(R.layout.patient_add_medical_record, null);
        spinnerMedicalType=(Spinner)layout.findViewById(R.id.spinner_medicalRCD_type);
        addItemsT0SpinnerMedicalRCDType();

        alertAddMedicalRCD = new AlertDialog.Builder(getParent());
        alertAddMedicalRCD.setTitle("Add new medical record");
        alertAddMedicalRCD.setView(layout);


    }
});

And here is the body of the method addItemsT0SpinnerMedicalRCDType();

private void addItemsT0SpinnerMedicalRCDType() 
{
    String[] s =new String[] {"Prescription","Email","Doctor    rounds","Phone","Other"};
    ArrayAdapter<String>adapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item, s);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerMedicalType.setAdapter(adapter);
}

And in the onCreate method I defined the context=this;

but when I click on the spinner it doesn't show the options. how can solve this problem. please help me

My logcat

 03-07 13:25:29.501: E/AndroidRuntime(1339): FATAL EXCEPTION: main
 03-07 13:25:29.501: E/AndroidRuntime(1339): android.view.WindowManager$BadTokenException:    Unable to add window -- token null is not for an application
 03-07 13:25:29.501: E/AndroidRuntime(1339):    at   android.view.ViewRoot.setView(ViewRoot.java:531)
 03-07 13:25:29.501: E/AndroidRuntime(1339):    at  android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
 03-07 13:25:29.501: E/AndroidRuntime(1339):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
03-07 13:25:29.501: E/AndroidRuntime(1339):     at android.app.Dialog.show(Dialog.java:241)
03-07 13:25:29.501: E/AndroidRuntime(1339):     at android.app.AlertDialog$Builder.show(AlertDialog.java:802)
03-07 13:25:29.501: E/AndroidRuntime(1339):     at android.widget.Spinner.performClick(Spinner.java:260)
03-07 13:25:29.501: E/AndroidRuntime(1339):     at android.view.View$PerformClick.run(View.java:9080)
03-07 13:25:29.501: E/AndroidRuntime(1339):     at android.os.Handler.handleCallback(Handler.java:587)
03-07 13:25:29.501: E/AndroidRuntime(1339):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-07 13:25:29.501: E/AndroidRuntime(1339):     at android.os.Looper.loop(Looper.java:123)
03-07 13:25:29.501: E/AndroidRuntime(1339):     at android.app.ActivityThread.main(ActivityThread.java:3683)
03-07 13:25:29.501: E/AndroidRuntime(1339):     at java.lang.reflect.Method.invokeNative(Native Method)
03-07 13:25:29.501: E/AndroidRuntime(1339):     at java.lang.reflect.Method.invoke(Method.java:507)
03-07 13:25:29.501: E/AndroidRuntime(1339):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-07 13:25:29.501: E/AndroidRuntime(1339):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-07 13:25:29.501: E/AndroidRuntime(1339):     at dalvik.system.NativeStart.main(Native Method)
Ahmad Dwaik 'Warlock'
  • 5,953
  • 5
  • 34
  • 56
Vikky
  • 933
  • 2
  • 15
  • 29

2 Answers2

0

just change the position of this alertAddMedicalRCD.setView(layout); after you inflatted the view

View layout = inflater.inflate(R.layout.patient_add_medical_record, null);
Pragnani
  • 20,075
  • 6
  • 49
  • 74
0

You can show Single choice list like Spinner with the help of AlertDilog while clicking on Button.

enter image description here

enter image description here

@Override
    public void onClick(View v) {
        AlertDialog.Builder builder = new AlertDialog.Builder(
                v.getContext());
        builder.setTitle(R.string.select_lanuage_txt);
        builder.setIcon(R.drawable.dialog_arrow);
        builder.setSingleChoiceItems(items, -1,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        Toast.makeText(MainActivity.this, "Selected Index"+" "+which, Toast.LENGTH_LONG).show();
                    }
                }).setPositiveButton(
                getResources().getString(R.string.Cancel),
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,
                            int whichButton) {
                        dialog.dismiss();
                    }
                });
        alert = builder.create();
        alert.show();

    }

For Full details and Source code go through my Android Blog.

http://amitandroid.blogspot.in/2013/03/android-alert-dialog-with-single-choice.html

Hope this blog will help you in order to achieve your requirement.

Thanks,

enter image description here

Now i added a EditText to the Dialog. is it you are looking for?

Amit Gupta
  • 8,914
  • 1
  • 25
  • 33