Sunday, 10 February 2013

How To Add Spinner dynamically on AlertDialog-Box

Sometimes you face the problem regarding adding spinner on AlertdialogBox.So dont worry I give
the solution here:

Step1)Create a method name ViewDemo() and call it in your onCreate() method.

Setp2)Then Add this following

    AlertDialog.Builder Dialog = new AlertDialog.Builder(this);
    Dialog.setTitle("Select Option");

     LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     View dialogView = li.inflate(R.layout.option, null);
     Dialog.setView(dialogView);

      Dialog.setPositiveButton("Ok",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                    }
                });

      Dialog.setNegativeButton("Cancel",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                    }
                });
        Dialog.show();
        Spinner spinnercategory = (Spinner) dialogView
                .findViewById(R.id.viewSpin);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                this, R.array.category, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnercategory.setAdapter(adapter);

        spinnercategory.setOnItemSelectedListener(new OnItemSelectedListener() {

            public void onItemSelected(AdapterView<?> parent, View arg1,
                    int arg2, long arg3) {
                //String selItem = parent.getSelectedItem().toString();
            }

            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub
            }
        });

    }

Step3)Then Write the xml file option.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:paddingLeft="10dip" >

    <Spinner
        android:id="@+id/viewSpin"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:prompt="@array/category" />

</LinearLayout>

Step4)At last You create an array of option in val/array.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="category">
        <item>A +VE</item>
        <item>A -VE</item>
        <item>B +VE</item>
        <item>B -VE</item>
        <item>O +VE</item>
        <item>O -VE</item>
         <item>AB +VE</item>
        <item>AB -VE</item>
       
        </string-array>
</resources>

For Download Source Click Here

    Thanks..

0 comments:

Post a Comment

 

Copyright © Unique Android Tutorial.. Design by Free CSS Templates | Blogger Theme by BTDesigner | Powered by Blogger