Android Spinner and Toast displays the selected item in a Spinner.
Main Layout Xml
spinner_page.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Spinner
android:id="@+id/spversions"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" />
</RelativeLayout>
MainActivity File
SpinnerPage.java
package com.example.spinner;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
public class SpinnerPage extends Activity implements OnItemSelectedListener {
private Spinner spversions;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spinner_page);
spversions = (Spinner) findViewById(R.id.spversions);
String[] android_versions = { "Cupcake", "Donut", "Eclair", "Froyo",
"Gingerbread", "Honeycomb", "Ice Cream Sandwich", "Jelly Bean",
"KitKat", "Lolipop" };
ArrayAdapter<String> adapter_versions = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, android_versions);
adapter_versions
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spversions.setAdapter(adapter_versions);
spversions.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
spversions.setSelection(position);
String selected_version = (String) spversions.getSelectedItem();
Toast.makeText(getApplicationContext(),
"Selected item:" + selected_version, Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
}
Screen Shots
Selected item displays in a Toast
Main Layout Xml
spinner_page.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Spinner
android:id="@+id/spversions"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" />
</RelativeLayout>
MainActivity File
SpinnerPage.java
package com.example.spinner;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
public class SpinnerPage extends Activity implements OnItemSelectedListener {
private Spinner spversions;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spinner_page);
spversions = (Spinner) findViewById(R.id.spversions);
String[] android_versions = { "Cupcake", "Donut", "Eclair", "Froyo",
"Gingerbread", "Honeycomb", "Ice Cream Sandwich", "Jelly Bean",
"KitKat", "Lolipop" };
ArrayAdapter<String> adapter_versions = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, android_versions);
adapter_versions
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spversions.setAdapter(adapter_versions);
spversions.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
spversions.setSelection(position);
String selected_version = (String) spversions.getSelectedItem();
Toast.makeText(getApplicationContext(),
"Selected item:" + selected_version, Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
}
Screen Shots
Selected item displays in a Toast