Spinner (comboBox) di Android
iseng2 buat aplikasi jadwal pelajaran di android :D
package com.example.jadwal;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.*;
import android.widget.AdapterView.OnItemSelectedListener;
public class MainActivity extends Activity implements
OnItemSelectedListener {
TextView pelajaran1,pelajaran2,pelajaran3;
Spinner hari;
List<String> maper = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pelajaran1=(TextView)findViewById(R.id.pelajaran1);
pelajaran2=(TextView)findViewById(R.id.pelajaran2);
pelajaran3=(TextView)findViewById(R.id.pelajaran3);
hari=(Spinner)findViewById(R.id.hari);
//mengsisi list dengan hari....dimana list ini berfugsi mengisi spiner
maper.add("senen");
maper.add("selasa");
maper.add("rabu");
final ArrayAdapter<String> adp = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_spinner_item,maper);
hari.setAdapter(adp);
hari.setOnItemSelectedListener( this);
//
}
//even ketika spiner di pilih
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id)
{
if(hari.getSelectedItem().equals("senen"))
{
pelajaran1.setText("sijahrah");
pelajaran2.setText("bahasa english");
pelajaran3.setText("statistik");
}
if(hari.getSelectedItem().equals("selasa"))
{
pelajaran1.setText("matematika");
pelajaran2.setText("fisika");
pelajaran3.setText("bahasa");
}
if(hari.getSelectedItem().equals("rabu"))
{
pelajaran1.setText("olahraga");
pelajaran2.setText("-----");
pelajaran3.setText("------");
}
// TODO Auto-generated method stub
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
untuk layout xml
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/AbsoluteLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="@+id/pelajaran1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="9dp"
android:layout_y="130dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FF4444"
android:textSize="20sp" />
<TextView
android:id="@+id/pelajaran2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="8dp"
android:layout_y="168dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#99CC00"
android:textSize="20sp" />
<TextView
android:id="@+id/pelajaran3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="6dp"
android:layout_y="207dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFBB33"
android:textSize="20sp" />
<Spinner
android:id="@+id/hari"
android:layout_width="176dp"
android:layout_height="30dp"
android:layout_x="96dp"
android:layout_y="0dp"
android:background="#33B5E5" />
<Button
android:id="@+id/button1"
android:layout_width="108dp"
android:layout_height="30dp"
android:layout_x="-12dp"
android:layout_y="0dp"
android:background="#33B5E5"
android:text="Hari :"
android:textColor="#ffffff"
android:textSize="20sp" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="3dp"
android:layout_y="35dp"
android:text="Jadwal"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="60sp" />
<Button
android:id="@+id/button2"
android:layout_width="48dp"
android:layout_height="30dp"
android:layout_x="272dp"
android:layout_y="0dp"
android:background="#33B5E5"
android:drawableRight="@drawable/arrow" />
</AbsoluteLayout>
Komentar
Posting Komentar