Friday, January 14, 2011

Android Simple Spinner Example


SIMPLE SPINNER

SOURCE CODE [main.xml] is

<?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">
<Spinner android:id="@+id/spin"
android:layout_width="150px"
android:layout_height="wrap_content"
android:layout_gravity="center">
</Spinner>
</LinearLayout>

SOURCE CODE [SpinnerExample.java] is

package com.SpinnerExample;

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

public class SpinnerExample extends Activity
{
Spinner sp;
                ArrayAdapter<String> adapter;
                String numbers[] = { "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN",
                                                "EIGHT", "NINE", "TEN" };

public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
                                setContentView(R.layout.main);

                                sp = (Spinner) findViewById(R.id.spin);
                               
                                adapter = new ArrayAdapter<String>(this,
                                                                android.R.layout.simple_spinner_item, numbers);
                                sp.setAdapter(adapter);
                }
}

The OUTPUT will be

https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgIXwi0qMGdjunle2uXFSdkzyRTJ-exfa8YKY3xs_T_F6iCTT_VBik6t6-YXgNqtQM5H9EpnjQbLbK_qRRYboYwvb8cZY8Q7MEutqQ4s-OvVsn89mJjiDez-IrHLskgxUkUBtAfOAqOuQ0/ https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjM6aJzfeCSvKE6vAsjZL2gIVjdACOQ1TPI6YXA7TZ7dJ2O1yKTWOXM_GVhc8cQkrXRfDKhATbruR_37LN7D-OMdmKepnMpeY-VEPxLLdgbPUUq0CDgcKbJc15cfp2HKEY-S6tJM7YpuUc/