In this article, we will create an Android application with a spinner widget containing a list of countries. On selecting a country from the list, the selected country will be displayed in a TextView widget.
This application is developed in Eclipse (4.2.1) with ADT plugin (20.0.3) and Android SDK (R20.0.3).
1. Create a new Android application project namely “SpinnerWidgetItemSelectedListener”
2. Design application launcher icon
3. Create a blank activity for define the class MainActivity
4. Enter MainActivity details
5. Delete Android’s support library from this project, if exists
By default Eclipse ( 4.2.1) adds Android Support Library to Android application project. For this application, we don’t need to use this support library. So the library file libs/android-support-v4.jar may be removed manually via ProjectExplorer by simply right click on the file and then clicking the menu item “delete”
6. Update the file res/values/strings.xml
<resources> <string name="app_name">SpinnerWidgetItemSelectedListener</string> <string name="hello_world">Hello world!</string> <string name="menu_settings">Settings</string> <string name="title_activity_main">ItemSelectedListener for Spinner</string> <string name="str_spr_countries">Countries</string> </resources>
7. Update the file res/values/styles.xml
<resources> <style name="AppTheme" parent="android:Theme" /> </resources>
8. Update the file res/values-v11/styles.xml
<resources> <style name="AppTheme" parent="android:Theme" /> </resources>
9. Update the file res/values-v14/styles.xml
<resources> <style name="AppTheme" parent="android:Theme.Holo" /> </resources>
10. Update the layout of the MainActivity in the file res/layout/activity_main.xml
<RelativeLayout 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" > <Spinner android:id="@+id/spr_country_list" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_centerHorizontal="true" tools:context=".MainActivity" android:prompt="@string/str_spr_countries" android:padding="0dp" /> <TextView android:id="@+id/tv_country" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/spr_country_list" android:layout_centerHorizontal="true" tools:context=".MainActivity" /> </RelativeLayout>
11. Update the MainActivity in the file src/in/wptrafficanalyzer/spinnerwidgetitemselectedlistener/MainActivity.java
package in.wptrafficanalyzer.spinnerwidgetitemselectedlistener; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.ArrayAdapter; import android.widget.Spinner; import android.widget.TextView; public class MainActivity extends Activity { TextView mTvCountry; Spinner mSprCountries; // An array containing list of Country Names String[] countries = new String[] { "India", "Pakistan", "Afghanistan", "Sri Lanka", "Burma", "Bangladesh", "China", "Nepal", "Bhutan", "North Korea", "South Korea", "Japan" }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Getting a reference to Spinner Widget containing list of countries mSprCountries = (Spinner) findViewById(R.id.spr_country_list); // Getting a referece to TextView Widget, which displays the selected // country from the spinner widget mTvCountry = (TextView) findViewById(R.id.tv_country); // Array adapter to set data in Spinner Widget ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, countries); // Setting the array adapter containing country list to the spinner widget mSprCountries.setAdapter(adapter); OnItemSelectedListener countrySelectedListener = new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> spinner, View container, int position, long id) { mTvCountry.setText(countries[position]); } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }; // Setting ItemClick Handler for Spinner Widget mSprCountries.setOnItemSelectedListener(countrySelectedListener); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }
12. Screenshots of the application in execution
13. Download

14. Testing with Android Testing Framework
Only the 50% of the application development is completed now. The remaining 50% is with the testing of the application and it is discussed in the article titled “Testing ItemSelectedListener for Spinner using Android Testing Framework” .
15. Reference
http://developer.android.com/guide/index.html

I am George Mathew, working as software architect and Android app developer at wptrafficanalyzer.in
You can hire me on hourly basis or on project basis for Android applications development.
For hiring me, please mail your requirements to info@wptrafficanalyzer.in.
My other blogs
store4js.blogspot.com
This is not a widget!