Pada MainActivity.java ketikkan script berikut :
****
package wulan.novi.spinner; // package sesuai nama package yang anda buat
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
public class MainActivity extends Activity implements AdapterView.OnItemSelectedListener{
TextView selection;
String[] pilihan={ "Wulan","Wulan2","Wulan3","Wulan4" };
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_main);
selection = (TextView) findViewById(R.id.selection);
Spinner spin=(Spinner) findViewById(R.id.spinner);
spin.setOnItemSelectedListener(this);
ArrayAdapter<String> aa = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item,pilihan);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(aa);
}
public void onItemSelected(
AdapterView<?> parent, View v, int position, long id){
selection.setText(pilihan[position]);
}
public void onNothingSelected(AdapterView<?> parent){
selection.setText("");
}
}
****
Sedangkan pada file XML nya (activity_main.xml), ketik script berikut :
****
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/myLinearLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<TextView
android:id="@+id/selection"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ff0033cc"
android:textSize="14pt"
android:textStyle="bold"
>
</TextView>
<Spinner
android:id="@+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
</Spinner>
</LinearLayout>
****
Berikut hasil Running nya :
No comments:
Post a Comment