Cara Buat Aplikasi Android Spinner Adapter
Ini Adalah Source Code Untuk MainActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements AdapterView.OnItemSelectedListener{
String[] Prov = { "Sulut", "Sulsel", "Sulteng", "Sulbar","Sultra", "Gorontalo"};
@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, Prov);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(aa);
}
@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;
}
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(this, "Anda Memilih: " + Prov [position],Toast.LENGTH_LONG).show();
}
TextView selection;
public void onNothingSelected(AdapterView<?> parent) {
Toast.makeText(this, "Silahkan Pilih Prov", Toast.LENGTH_LONG).show();
}
}
Next Kita Buatkan File activity_main.xml Ini Source Code Nya
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#8b8b8b"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:text="Provinsi Pulau Sulawesi"
android:textStyle="bold"
android:textSize="10pt"
android:textColor="#2b2b2b"
android:id="@+id/selection"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Spinner
android:id="@+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true" />
</LinearLayout>
Dan Ini Adalah Running Hasil Akhir Dari Source Code Di Atas
Komentar
Posting Komentar