Android Spinner and its item selection

In Android, Spinner is like a dropdown list. It displays a list of items. One of its items can be selected at a time. You can use an array or an ArrayList object that contains items as its data source. Typically the array or ArrayList object is not directly set to the Spinner. You need to create an object of ArrayAdapter and pass the array or ArrayList object to its constructor. Then use the setAdapter method to set the ArrayAdapter object to the Spinner.





Now to be clear, you need to create a new Android project. Then edit the activity_main.xml file to add a Spinner as shown below.

<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:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context=".MainActivity" >
  
   <Spinner
  android:id="@+id/country_spin"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"

/>
  
</LinearLayout>


The Spinner needs its own layout file that defines its item template. In the layout file, you need to have one TextView. When you attach a data source to the Spinner, Android looks for the TextView in the layout file to display its text items. Here is the layout file of the Spinner (spinner_layout.xml).

<?xml version="1.0" encoding="utf-8"?>

<!-- Single Item Design -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="50dip"
   android:orientation="horizontal"
   android:padding="8dip"
   android:background="#ffffff"
   >
<TextView
   android:id="@+id/textview"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"    
     android:paddingLeft="5sp"    
    
>
</TextView>

</RelativeLayout>



Double-click the MainAcitivity.java on the left side to open the MainAcitivity class. Then in the onCreate() method to the MainActivity class, you need to write code to make a reference to the Spinner, create an array of strings to use as data source of the Spinner, create an object of ArrayAdapter, and use the setAdapter method to attach the data source to the Spinner. When you create the ArrayAdapter object, you need to pass four values: current activity, a resource layout file that contains a TextView, the TextView resource id to display items of the spinner, and the data source.
To respond to user selections, you need to register the Spinner to receive the item selection event by using the setOnItemSelectedListener() method. You pass to this method an object of a class that implements the OnItemSelectedListener interface. The onItemSelected() method to perform action when an item is selected.


import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   Spinner sp=(Spinner)findViewById(R.id.spinner);
   String[] items={"Cambodia","Japan","USA"};
   ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, R.layout.spinner_layout,R.id.txtview, items);
   sp.setAdapter(adapter);
   sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
  public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    ViewGroup vg=(ViewGroup)view;
   TextView tv=(TextView)vg.findViewById(R.id.txtview);
   Toast.makeText(MainActivity.this, tv.getText().toString(), Toast.LENGTH_LONG).show();
  }
@Override
   public void onNothingSelected(AdapterView<?> parent) {

      }
  });

 }
}

When you run the project again and select an item of the Spinner, you will see a Toast message showing the selected item.


Android-Spinner-and-its-item-selection

Posted by: Dara | post date: 05-25-2014 | Subject: Android Apps Development

write programming tip



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.