CRTC Electronics

Click here to edit subtitle

Drop Down Box / Spinners

create a list

in the string.xml file add the code that makes the list for your spinner

    <string name="country_prompt">Choose a country</string>


    <string-array name="country_arrays">

   <item>Malaysia</item>

   <item>United States</item>

   <item>Indonesia</item>

   <item>France</item>

   <item>Italy</item>

   <item>Singapore</item>

   <item>New Zealand</item>

   <item>India</item>

    </string-array>

link the list to the drop down box

in the main.xml in the <Spinner   add this code

android:entries="@array/country_arrays" android:prompt="@string/country_prompt"

read selected item

create the spinner variable in MainActivity


in onCreate set the variable to the xml spinner


when you need to get the selected item from the spinner use getSelectedItem().toString(); to read selected item;

private Spinner country_ ;


country_ = (Spinner)findViewById(R.id.spinner1);


country_ .getSelectedItem().toString();

add items to the list in code

in the .java file you can add items to the list by adding this code

// add items into spinner dynamically 
public void addItemsOnSpinner1() {
 spinner1 = (Spinner) findViewById(R.id.spinner2); 
 List<String> 
list = new ArrayList<String>(); 
list.add("list 1"); 
list.add("list 2"); 
list.add("list 3"); 
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list); dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
 spinner1.setAdapter(dataAdapter); }