0% found this document useful (0 votes)
17 views3 pages

Spinner: 1. Buat Project Baru Dengan Nama Spinner 2. Di Activity - Main - XML Coding

The document describes creating a Spinner project in Android. It involves: 1. Creating a new project called "Spinner" 2. Designing an XML layout with two Spinners and TextViews 3. Implementing code in the MainActivity class to populate the Spinners from string arrays, set OnItemSelectedListeners, and display selections. 4. Defining the string arrays for country and fruit options in strings.xml.

Uploaded by

helfy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views3 pages

Spinner: 1. Buat Project Baru Dengan Nama Spinner 2. Di Activity - Main - XML Coding

The document describes creating a Spinner project in Android. It involves: 1. Creating a new project called "Spinner" 2. Designing an XML layout with two Spinners and TextViews 3. Implementing code in the MainActivity class to populate the Spinners from string arrays, set OnItemSelectedListeners, and display selections. 4. Defining the string arrays for country and fruit options in strings.xml.

Uploaded by

helfy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

SPINNER

1. Buat project baru dengan nama Spinner


2. Di activity_main.xml coding

<LinearLayout 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"
android:orientation = "vertical"
tools:context="com.example.project_spinner.MainActivity" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pilih negara" />

<Spinner
android:id="@+id/negara"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<TextView
android:id="@+id/tampil"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pilih Buah" />

<Spinner
android:id="@+id/buah"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</LinearLayout>

3. Pada MainActivity.java
public class MainActivity extends ActionBarActivity {

Spinner negara, buah;


TextView tampil;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

negara = (Spinner)findViewById(R.id.negara);
buah = (Spinner)findViewById(R.id.buah);
tampil = (TextView)findViewById(R.id.tampil);
ArrayAdapter<CharSequence>adapter =
ArrayAdapter.createFromResource(this, R.array.list_negara,
android.R.layout.simple_spinner_item);

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item
);
negara.setAdapter(adapter);
negara.setOnItemSelectedListener(new function());
//negara.setOnItemSelectedListener(new function());

ArrayAdapter<CharSequence>adapter2 =
ArrayAdapter.createFromResource(this, R.array.list_buah,
android.R.layout.simple_spinner_item);

adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_ite
m);
buah.setAdapter(adapter2);
buah.setOnItemSelectedListener(new function2());
}

public class function implements OnItemSelectedListener {


boolean isFirst = true;
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
if(isFirst){
isFirst = false;
}else{
String str =
parent.getItemAtPosition(position).toString();
tampil.setText(str);
}

@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub

public class function2 implements OnItemSelectedListener {


boolean isFirst = true;
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
if(isFirst){
isFirst = false;
}else{
Toast.makeText(parent.getContext(), "kamu sudah
memilih" + parent.getItemAtPosition(position).toString(),
Toast.LENGTH_LONG).show();
}

4. Pada values, string.xml

<string-array name = "list_negara">


<item>Indonesia</item>
<item>Malaysia</item>
<item>Singapura</item>
</string-array>
<string-array name = "list_buah">
<item>Durian</item>
<item>Semangka</item>
<item>Apel</item>
</string-array>

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy