Caranya buat Project Android di Eclipse. Kemudian buat desain tampilan main.xml sebagai berikut :
<?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"
/>
<Spinner android:id="@+id/Spinner01" android:layout_width="wrap_content" android:layout_height="wrap_content"></Spinner>
</LinearLayout>
package com.amijaya.dynamicspinner;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
// http://cariprogram.blogspot.com
// nuramijaya@gmail.com
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spinner1 = (Spinner) findViewById(R.id.Spinner01);
String[] menuArray = new String[] { "Uki", "Ardhi", "Ito", "Enas", "Radit", "Dian" };
//String[] menuArray;
//menuArray = new String[5];
//menuArray[0] = "Uki";
//menuArray[1] = "Dian";
ArrayAdapter<String> adapter;
adapter = adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, menuArray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter);
}
}
Sedangkan untuk AndroidManifest.xml tidak perlu diubah cukup default saja :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.amijaya.dynamicspinner"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="8" />
</manifest>
Hasilnya :
Kode Program selengkapnya dapat didownload pada link berikut. Setelah tampil Google Docs, klik File-Download.
Semoga berguna.
feedback: nuramijaya@gmail.com