Below is the simple example of Spinner element in Android:
There are three steps to generate Spinner control in Android:
Strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Practicework</string>
<string name="action_settings">Settings</string>
<string name="btn1">Check Status</string>
<string-array name="countryname">
<item>Australia</item>
<item>New Zealand</item>
<item>England</item>
<item>Germany</item>
<item>France</item>
<item>India</item>
<item>Sri Lanka</item>
</string-array>
</resources>
There are three steps to generate Spinner control in Android:
- Define list of items in strings.xml file using "string-array".
- Assign string array to "entries" property of Spinner control.
- "getSelectedItem()" at code base will give selected item from the list.
Strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Practicework</string>
<string name="action_settings">Settings</string>
<string name="btn1">Check Status</string>
<string-array name="countryname">
<item>Australia</item>
<item>New Zealand</item>
<item>England</item>
<item>Germany</item>
<item>France</item>
<item>India</item>
<item>Sri Lanka</item>
</string-array>
</resources>
activity_practice.xml
<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"
android:background="#d0d0d0"
tools:context=".PracticeActivity" >
<Spinner
android:id="@+id/spnr1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/countryname"
android:layout_margin="20dp"
/>
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn1"
android:layout_margin="20dp"
/>
</LinearLayout>
PracticeActivity.java
package com.agilissystems.practicework;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
public class PracticeActivity extends Activity implements OnClickListener {
Button oBtn1;
Spinner oSpnr1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_practice);
oBtn1 = (Button) findViewById(R.id.btn1);
oSpnr1 = (Spinner) findViewById(R.id.spnr1);
oBtn1.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.practice, menu);
return true;
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (arg0.getId() == R.id.btn1){
StringBuffer oStr1 = new StringBuffer();
oStr1.append(oSpnr1.getSelectedItem().toString());
Toast.makeText(this, "Country : " + oStr1.toString(), Toast.LENGTH_LONG).show();
}
}
}
First, select the country name from the spinner control and then click on Button "Check Status" will toast name of selected country.