spinner code example

Example 1: spinner android

// Spinner element
      Spinner spinner = (Spinner) findViewById(R.id.spinner);
      
      // Spinner click listener
      spinner.setOnItemSelectedListener(this);
      
      // Spinner Drop down elements
      List<String> categories = new ArrayList<String>();
      categories.add("Automobile");
      categories.add("Business Services");
      categories.add("Computers");
      categories.add("Education");
      categories.add("Personal");
      categories.add("Travel");
      
      // Creating adapter for spinner
      ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
      
      // Drop down layout style - list view with radio button
      dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
      
      // attaching data adapter to spinner
      spinner.setAdapter(dataAdapter);

Example 2: spinner

mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) 
    { 
        // An item was selected. You can retrieve the selected item using
        // parent.getItemAtPosition(pos)
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) 
    { 
        // If an option is removed then what to do
        // or anything else
    }

});

Tags:

Misc Example