1. Buat project baru di eclipse (File –> New –> Project –> Android Application Project)
2. Ketikkan kode xml di main.xml seperti di bawah ini:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Spinner android:id="@+id/spinner1" android:layout_width="match_parent" android:layout_height="wrap_content" android:prompt="@string/hello_world" android:entries="@array/jus_buah" android:layout_alignParentLeft="true" android:layout_below="@+id/textView1" /> </RelativeLayout>3. Tambahkan isi dari string,xml yang berada di folder res –> values seperti di bawah ini:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Spinner</string> <string name="action_settings">Settings</string> <string name="hello_world">Pilih Buah yang Anda Sukai</string> <string-array name="jus_buah"> <item>Anggur</item> <item>Apel</item> <item>Alpukat</item> <item>Belimbing</item> <item>Durian</item> <item>Jeruk</item> <item>Jambu</item> <item>Kelengkeng</item> </string-array> <string name="spinner_message_template"> You selected \'%s\'.</string> </resources>4. Ketikkan kode program di MainActivity.java seperti di bawah ini:
package com.spinner; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.Spinner; import android.widget.Toast; public class MainActivity extends Activity { String[] Buah; Spinner s1; ArrayAdapter<String> adapter; private String mItemSelectedMessageTemplate; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mItemSelectedMessageTemplate = getString(R.string.spinner_message_template); s1 = (Spinner) findViewById(R.id.spinner1); s1.setOnItemSelectedListener(new OnItemSelectedListener() { private boolean isFirst = true; @Override public void onItemSelected(AdapterView<?> spinner, View selectedView, int selectedIndex, long id) { if(isFirst){ isFirst = false; }else{ String selection = spinner.getItemAtPosition(selectedIndex).toString(); String message = String.format(mItemSelectedMessageTemplate, selection); showToast(message); } } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }); } protected void showToast(String message) { Toast.makeText(this, message, Toast.LENGTH_LONG).show(); //Intent i = new Intent(this, Next.class); //startActivity(i); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }5. Run dan lihat hasilnya, selamat mencoba, semoga bermanfaat
Screenshoot tampilannya seperti gambar di bawah ini:
No comments:
Post a Comment