Android Spinner Demonstration

Screenshot

Screenshot of the app running in the Gingerbread emulator

As with the ListView on the preceding page, we use an ArrayAdapter. This time we set the properties of the upper part with the "simplerow" layout passed to the constructor and the lower part with the "dropdownrow layout" passed to the setDropDownViewResource method. We would have liked to be able to reduce the width of the drop down rows, but this is difficult to achieve.

The code of MainActivity.pas

namespace org.me.android_spinner;

interface

uses
  java.util,
  android.app,
  android.content,
  android.os,
  android.util,
  android.view,
  android.widget,
  android.graphics;
type
  MainActivity = public class(Activity)
  private
    Font_sizes : array[0 .. 4] of String := ["14", "16", "18", "20", "24"];
    lv : ListView;
    tv : TextView;
    sp : Spinner;
    lstAdapter : ArrayAdapter<String>;     
  public
    method onCreate(savedInstanceState: Bundle); override;
    method ItemSelected(parent : AdapterView; view : View; position : Integer; id : Int64);
  end;

implementation

method MainActivity.ItemSelected(parent : AdapterView; view : View ; position : Integer; id : Int64);
var
  Selected_size : Integer;
begin
  // Assign the text of the TextView.
  tv.Text := String[R.string.font] + ' ' + Font_sizes[position];
  // Obtain the text size from the selected position
  Selected_size := Integer.valueOf(Font_sizes[position]);
  // Set the text size of the TextView.
  tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, Selected_size);
end;

method MainActivity.onCreate(savedInstanceState: Bundle);
begin
  inherited;
  // Set view from the "main" layout resource.
  ContentView := R.layout.main;
  //Assign the Spinner and TextView.
  sp :=Spinner(findViewById(R.id.sp1));
  tv  := TextView(findViewById(R.id.tv1));
  // Assign the ArrayAdapter.
  lstAdapter := new ArrayAdapter<String>(self, R.layout.simplerow, Font_sizes);
  // Set the style from the layout file.
  lstAdapter.setDropDownViewResource(R.layout.dropdownrow);
  sp.setAdapter(lstAdapter);
  // Link the code to handle the item selection event. 
  sp.OnItemSelectedListener := new interface
                               AdapterView.OnItemSelectedListener(onItemSelected := @ItemSelected);
end;

end.

Layout Files


main.layout-xml

<?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 
    android:id="@+id/myLinearLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <TextView
      android:id="@+id/tv1"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:background="#00FF00"
      android:textColor="#FFFFFF"
      android:textSize="14sp"
      android:textStyle="bold"></TextView>
    <Spinner
      android:id="@+id/sp1"
      android:layout_width="wrap_content"      
      android:layout_height="wrap_content"
      android:layout_gravity="center_horizontal"></Spinner>
  </LinearLayout>

simplerow.layout-xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="50dp"
  android:layout_height="wrap_content"
  android:padding="5dp"
  android:textColor="#FFFFFF"
  android:background="#FF0000"
  android:textSize="16sp" ></TextView>

dropdownlist.layout-xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:layout_gravity="center_horizontal"
  android:gravity="center_horizontal"
  android:textSize="16sp"
  android:textColor="#FFFFFF"
  android:background="#FF0000"></TextView>

Strings File

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <string name="app_name">Spinner Demo</string>
  <string name="font">Selected font size in scaled pixels: </string>
</resources>
 
Programming - a skill for life!

How to use EditText, Toast, DatePicker, ListView, RadioGroup, ScrollView and ImageView widgets, sound and files in Oxygene for Java Android apps