Saturday, December 29, 2012

Android beginner tutorial: Part 35 - Spinner widget

Today we'll take a look at a widget called Spinner.

A Spinner is a widget that, when clicked, displays a list of options for the user to pick. Basically, a variation of a combo box.

Firstly, add a Spinner to the activity layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >
    
<Spinner
    android:id="@+id/spinner"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
    
</LinearLayout>

In the previous tutorials, we've declared the array of contacts in the Java class itself. This time, let's create an array dataprovider in the strings.xml file.

It is done very easily:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Code For Food Test</string>
    <string name="menu_settings">Settings</string>
    <string-array name="choices_array">
        <item>One</item>
        <item>Two</item>
        <item>Three</item>
        <item>Four</item>
        <item>Five</item>
        <item>Seven</item>
        <item>Eight</item>
        <item>Nine</item>
        <item>Ten</item>
    </string-array>

</resources>

Now go to the MainActivity.java class, in the onCreate() function declare 2 variables - one to reference the spinner object, and the second one to hold the array data from strings.xml. It is done like this:

Spinner spinner = (Spinner)findViewById(R.id.spinner);
String[] choices = getResources().getStringArray(R.array.choices_array);

After that, we create an ArrayAdapter instance like usually. Use the android.R.layout.simple_spinner_item layout for the spinner. After declaring the ArrayAdapter, use its setDropDownViewResource() method to set a resource layout for the spinner drop down menu that pops up when the widget is clicked. Use android.R.layout.simple_spinner_dropdown_item for that.

Then set the adapter to the spinner:

ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, choices);
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(arrayAdapter);

Full code:

package com.kircode.codeforfood_test;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class MainActivity extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Spinner spinner = (Spinner)findViewById(R.id.spinner);
String[] choices = getResources().getStringArray(R.array.choices_array);

ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, choices);
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(arrayAdapter);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

}

The results look like this:



Thanks for reading!

No comments:

Post a Comment