Monday, April 30, 2018

Android Spinner Example


Spinner

Spinner is a widget which provides a quick way to select a value from a set. A spinner presents multiple options/items to the user from which user can select one option.
For ex.  In game application when we want user to select a particular level he/she wants to play, we can use spinner to multiples levels in which can select one level.






Using Spinner

Steps to use spinner in your application
1: Defining the spinner in your .xml/layout file by using “Spinner” tag
<!—Declaring Spinner in xml -->
    <Spinner
        android:id="@+id/spinner"
        android:layout_marginTop="20dp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:prompt="@string/spinner_title"
    />


2: Create an array/arraylist of strings that will be set as the items of spinner.
String spinnerItems[]={"Level 1","Level 2","Level 3","Level 4","Level 5",};


3: Create adapter and set it with spinner
// Create adapter for spinner
ArrayAdapter  dataAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, spinnerItems);

// attaching data adapter to spinner
mySpinnerObject.setAdapter(dataAdapter);


4: Set  clicklistener on Spinner
// set clicklistener on Spinner
 mySpinnerObject.setOnItemSelectedListener(this);



5: override  onItemSelected() method, this method gets called by the System when an item is selected. This method receives the position parameter which signifies the index of the array/arraylist which is selected by the user.



@Override
 public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
                         
// get the selected Spinner's Item
            String itemSelected = parent.getItemAtPosition(position).toString();

}

Let put all this together and develop an android application to learn how to use spinner.





Demo Application: “Working with Spinner”

What we will do: We will present a list of levels “Level 1,Level 2,Level 3,Level 4,Level 5" to the user and show the level selected by the user using Toast.


activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:padding="10dip"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">


    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Select the Level"
        android:layout_marginTop="20dp"
        android:textColor="#0000FF"
        android:textSize="20dp"
        android:textStyle="bold"
    />

    <!-- Spinner Element -->
    <Spinner
        android:id="@+id/spinner"
        android:layout_marginTop="20dp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
    />

  </LinearLayout>



SpinnerMainActivity.java

public class SpinnerMainActivity extends Activity implements OnItemSelectedListener
{
     Spinner mySpinner;
      /* create array of string of levels
     * to be set as Spinner Item
     */
    String spinnerItems[]={"Level 1","Level 2","Level 3","Level 4","Level 5",};
   
     @Override
     protected void onCreate(Bundle savedInstanceState)
     {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
         
          // get the Spinner reference
               mySpinner = (Spinner) findViewById(R.id.spinner);
       
               // set clicklistener on Spinner
               mySpinner.setOnItemSelectedListener(this);
       
              // Create adapter for spinner
          ArrayAdapter  dataAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, spinnerItems);

          // Drop down layout style - list view with radio button
                    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

          // attaching data adapter to spinner
          mySpinner.setAdapter(dataAdapter);
     }

     /* onItemSelected() method gets called by the System
      * when an item is selected
      * This method receives the position parameter
      * which signifies the index of the array
      * which is selected
      */
     @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
        {
           
              // get the selected Spinner's Item
              String itemSelected = parent.getItemAtPosition(position).toString();

              // Showing selected spinner item
              Toast.makeText(getApplicationContext(),  itemSelected+"  Selected", Toast.LENGTH_LONG).show();
          }
      
      
       @Override
          public void onNothingSelected(AdapterView view)
          {
              // TODO Auto-generated method stub
          }

}




No comments:

Post a Comment