Spinners

Intro

Spinners are what Android calls their widget that displays drop-down lists .  Why "spinner"?  I have no idea...you'll have to ask Google.  You can think of a Spinner as a close relative to a ListView; in fact, they are both sub-classed from the AdapterView class and function in very similar ways.  Fortunately for you, since you already know how to work with ListViews, Spinners should be a breeze!

Demonstration

In order to introduce you to Spinners, we'll take what you've already done with ListViews in the Presidents List app and modify our code to use a drop-down list. Here are the steps:

  1. Make a copy of the PresidentsList app and call it PresidentsSpinner 
  2. Change the package name to edu.usna.cs.president2
  3. Open the manifest file and make the following changes:
    1. Update the package name 
    2. Move the MAIN intent-filter to the WebviewActivity 
    3. Remove Parent attributes and meta-data block from WebviewActivity
    4. Delete the MainActivity block
  4. Open the activity_webview.xml file and add a Spinner widget:
    <Spinner
        android:id="@+id/president_spinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"/>
     
    1. Modify the WebView and ProgressBar appropriately
  5. Open WebviewActivity.java:
      1. Implement OnItemSelectedListener
      2. Add the methods required by the new interface (let Eclipse do it for you!)
      3. Remove the lines of code in onCreate that are intended to get Intent data.
      4. Open MainActivity.java and copy the code from the onCreate method that creates out lisr of presidents and place that code in the onCreate of the WebviewActivity.
      5. Instantiate your new Spinner, create an ArrayAdapter and associate the preisdents list with the adapter.
      6. Set the Spinner's ArrayAdapter with the Spinner.
      7. At this point, your onCreate should look like this: 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	setContentView(R.layout.activity_webview);
    	// Show the Up button in the action bar.
    	setupActionBar();
    			
    	String fullURL = baseURL + "List_of_Presidents_of_the_United_States";
    	
    	Spinner spinner = (Spinner) findViewById(R.id.president_spinner);
    	List presidentsList = 
    	    Arrays.asList(getResources().getStringArray(R.array.presidents));
    	spinner.setAdapter(
    	    new ArrayAdapter(this, 
                    android.R.layout.simple_list_item_1, presidentsList));
    	spinner.setOnItemSelectedListener(this);
    	
    	webView = (WebView) findViewById(R.id.wiki_webview);
    	webView.getSettings().setJavaScriptEnabled(true);
    	
    	progressBar = (ProgressBar) findViewById(R.id.progressbar);
    	initializeWebView();
    	
    	webView.loadUrl(fullURL);
    }
    
  6. Finally, put the proper code in the onItemSelected() method so that when an item is selected from the drop-down list, the webview loads the appropriate page. [I'll let you figure this one out]
  7. Delete MainActivity.java and activity_main.xml
  8. Launch the Application and bask in the glory of your own genius!

Sample Eclipse Project: Presidents Spinner