Buatlah project baru dengan
ketentuan berikut:
Project Name :
Spinner View
Build Target : Android 4.2.2
Aplication name : spinner
Package name :
com.immobulus.spinner
Activity :
spinner
Buatlah satu file baru di res/layout dengan
nama file spinner.xml
spinner.xml
<?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"
>
<Spinner
android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
/>
</LinearLayout>
spinner.java
package com.immobulus.spinner;
import
android.app.Activity;
import
android.os.Bundle;
import
android.view.View;
import
android.widget.AdapterView;
import
android.widget.ArrayAdapter;
import
android.widget.Spinner;
import
android.widget.Toast;
import
android.widget.AdapterView.OnItemSelectedListener;
public class spinner extends Activity
{
String[] club = {
"Barcelona",
"Real Madrid",
"Menchester United",
"Chealse FC",
"AC Milan",
"Arsenal",
"Inter Milan",
"Tothem Hotspur",
"Valencia",
"Juventus",
"Menchester City"
};
Spinner s1;
@Override
public void onCreate(Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.spinner);
s1
= (Spinner) findViewById(R.id.spinner1)
ArrayAdapter<String> adapter = new
ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, club);
s1.setAdapter(adapter);
s1.setOnItemSelectedListener(new
OnItemSelectedListener()
{
public void
onItemSelected(AdapterView<?> arg0,
View arg1, int arg2, long arg3)
{
int index = s1.getSelectedItemPosition();
Toast.makeText(getBaseContext(),
"You have
selected item : " + club[index],
Toast.LENGTH_SHORT).show();
}
public void
onNothingSelected(AdapterView<?> arg0) {}
});
}
}
AndroidManifext.xml
<?xml version="1.0"
encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.immobulus.spinner"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon"
android:label="@string/app_name">
<activity android:name=".spinner"
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="9"
/>
</manifest>
Kemudian buat satu file dengan nama ViewActivity.java dan
isikan coding seperti berikut:
ViewActivity.java
package com.immobulus.spinner;
import
android.app.Activity;
import
android.content.Intent;
import
android.os.Bundle;
public class ViewsActivity extends Activity
{
/** Called when the activity is first
created. */
@Override
public void onCreate(Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startActivity(new Intent(this, spinner.class));
}
}
Running
aplikasi sehingga akan menghasilkan gambar seperti berikut:
Click list yang muncul dan pilih Manchester City, sehingga akan selected dan memunculkan pesan dalam
bentuk Toast apa yang kita pilih
seperti berikut:
Sumber: PEMROGRAMAN APLIKASI MOBILE SMARTPHONE DAN TABLET PC BERBASIS ANDROID. oleh : Nazruddin Safaat H
Tidak ada komentar:
Posting Komentar