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
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Who you are?"
/>
android:id = "@+id/message_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Where you come from?"
/>
android:id = "@+id/spinner_countries"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right|bottom"
>
android:id = "@+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK" />
android:id = "@+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel" />
Create a file, named array.xml in /HelloAndroid2/res/values/
array.xml
- Canada
- China
- Germany
- Japan
- Korea
- Russia
- UK
- USA
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 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();
}
};
}
Now, you can try to run it on Emulator.
Related article:
- Custom Spinner with icon