Sunday, August 2, 2009

HelloAndroid with Spinner

In this exercise, I add a Spinner for user to select country where he come from. I will show how to add a Spinner in main.xml, add a array.xml to embed items into Spinner, and how to handle it in HelloAndroid.java.

A view that displays one child at a time and lets the user pick among them. The items in the Spinner come from the Adapter associated with this view.

Environment:
Ubuntu 9.04
Eclipse 3.5 Galileo
Android 1.5 SDK r3

The exercise continuous from the previous article, Change UI elements by changing main.xml. The project can be downloaded here.

Modify main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Who you are?"
/>
<EditText
android:id = "@+id/message_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Where you come from?"
/>
<Spinner
android:id = "@+id/spinner_countries"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right|bottom"
>
<Button
android:id = "@+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK" />
<Button
android:id = "@+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel" />
</LinearLayout>
</LinearLayout>

Download main.xml

Create a file, named array.xml in /HelloAndroid2/res/values/
array.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Used in Spinner/spinner_2.java -->
<string-array name="countries">
<item>Canada</item>
<item>China</item>
<item>Germany</item>
<item>Japan</item>
<item>Korea</item>
<item>Russia</item>
<item>UK</item>
<item>USA</item>
</string-array>
</resources>

Download array.xml

Finally, modify JavaAndroid.java

package com.example.helloandroid;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class HelloAndroid extends Activity {

private Spinner spinner_countries;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button okButton = (Button) findViewById(R.id.ok);
okButton.setOnClickListener(okOnClickListener);
Button cancelButton = (Button) findViewById(R.id.cancel);
cancelButton.setOnClickListener(cancelOnClickListener);

spinner_countries = (Spinner) findViewById(R.id.spinner_countries);
ArrayAdapter<CharSequence> adapter
= ArrayAdapter.createFromResource(this,
R.array.countries, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner_countries.setAdapter(adapter);
}
private Button.OnClickListener okOnClickListener = new Button.OnClickListener(){
@Override
public void onClick(View v) {
EditText edit_text = (EditText) findViewById(R.id.message_text);
CharSequence edit_text_value = edit_text.getText();
setTitle("Hello: "+edit_text_value+" from "
+spinner_countries.getItemAtPosition((int) spinner_countries.getSelectedItemId()));
}
};

private Button.OnClickListener cancelOnClickListener = new Button.OnClickListener(){
@Override
public void onClick(View v) {
finish();
}
};
}

Download HelloAndroid.java

Now, you can try to run it on Emulator.

The project can be downloaded here.

Related article:
- Custom Spinner with icon



No comments: