Android - SPLessons

Android Spinner

Chapter 20

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Android Spinner

Android Spinner

Android Spinner allows you to select an item from the drop down menu. It is a widget that offers a fast means to select a value from a set. A spinner shows the initial selected value by default. When a user touches the spinner, a drop down list with various other available values/options can be seen, from which the user can select a desired value. Android Spinner is added in API Level 1.

The values you provide for the spinner can come from any source (static or dynamic data), but must be provided through an SpinnerAdapter, such as ArrayAdapter, if the choices are available in an array, or a CursorAdapter, if the choices are available from a database query.

An Adapter object acts as a bridge between an AdapterView and the underlying data for that view. The Adapter provides access to the data items and is in-charge of making a View for each item in the data set. At this point, when a user selects an item from the drop-down list, the Spinner object receives an OnItemSelected() event.

To define the selection event handler for a spinner, implement the AdapterView.OnItemSelectedistener Interface and the corresponding onItemSelected() callback method.

Ex:

 // Create an ArrayAdapter using the string array and a default spinner layout
 ArrayAdapter spAdapter=new ArrayAdapter(context, android.R.layout.select_dialog_item,list);
 // Apply the adapter to the spinner
 sp.setAdapter(spAdapter);
 sp.setOnItemSelectedListener(new OnItemSelectedListener() {

       @Override
      public void onItemSelected(AdapterView arg0, View arg1,
      int position, long arg3) {
      // TODO Auto-generated method stub
      String pos=sp.getItemAtPosition(position).toString();
      Toast.makeText(context, "You selected "+pos, Toast.LENGTH_LONG).show();
        }

        @Override
         public void onNothingSelected(AdapterView arg0) {
         // TODO Auto-generated method stub

        }
        });

Create MainActivity.java under src/<your packagename>.

MainActivity.java:

public class MainActivity extends Activity {

    Context context;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //set activity content to external view
        setContentView(R.layout.activity_main);
        context=this;

       //find Views by Id
        TextView tv1=(TextView)findViewById(R.id.textView1);
        final Spinner sp=(Spinner)findViewById(R.id.spinner1);

        //create an array
        ArrayList<String> list=new ArrayList<String>();
        list.add("Android");
        list.add("IOS");
        list.add("Swift");
        list.add("Ruby");
        list.add("Python");

        // Create an ArrayAdapter using the string array and a default spinner layout
        ArrayAdapter<String> spAdapter=new ArrayAdapter<String>(context, android.R.layout.select_dialog_item,list);
        // Apply the adapter to the spinner
        sp.setAdapter(spAdapter);
        sp.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                                       int position, long arg3) {
           // TODO Auto-generated method stub
                String pos=sp.getItemAtPosition(position).toString();
                Toast.makeText(context, "You selected "+pos, Toast.LENGTH_LONG).show();
            }

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

            }
        });

    }
}

Create activity_main.xml under res/layout folder.

activity_main.xml:




    

    

AndroidManifest.xml:




    

    
        
            
                

                
            
        
    


Output: