Android : Spinner

Standard

What to do when you have a list and want the user to select one item amongst it. Spinners make this task simpler. When the user clicks on that element they drop down like a list. The user can select one of the items and continuous. Thus, they avoid blocking your view and using unnecessary screen space. Spinner is a view that displays one child at a time and lets the user pick among them. The items in the Spinner come from the Adapter associated with this view. So here in this Android Series, we learn how to implement these Spinners. So, here I have an example where users have to select his gender and Android version and click the button which launches a new activity to display the user selections. To switch between we use Intents and to pass the user selections of these spinners to the next activity we use SharedPreferences. To learn how this is done you can refer to my tutorial on Intents and Shared Preferences. If you find this hard then for now just display the user’ s selection in the same activity. So, let’s get on with Spinners. In our XML file, we first drag and drop two spinner widgets. Now we need to specify each with the array of items to populate with. This is done by the attribute android:entries=” “. Don’t forget to declare that array in your strings.xml file and specify those elements in that array.


android:entries="@array/Android_Version"

Screenshot_2015-01-02-17-32-41 In the main activity, declare two Spinners, initialise their views. Now we have to do is to set on item select listener which will identify the item selected by the user.


setOnItemSelectedListener(new CustomOnItemSelectedListener());

The setOnItemSelectedListener() registers a callback to be invoked when an item in this AdapterView has been selected. The CustomOnItemSelectedListener() is an interface implementing OnItemSelectedListener. The AdapterView.OnItemSelectedListener requires the onItemSelected() and onNothingSelected() callback methods. The onItemSelected() callback method is invoked when an item in this view has been selected. This callback is invoked only when the newly selected position is different from the previously selected position or if there was no selected item. Implementers can call getItemAtPosition(position) if they need to access the data associated with the selected item. The onNothingSelected() callback method is to be invoked when the selection disappears from this view. The selection can disappear for instance when touch is activated or when the adapter becomes empty. The item selected by the user can be retrieved by getSelectedItem() which returns the data corresponding to the currently selected item, or null if there is nothing selected. This can be displayed in a TextView. Screenshot_2015-01-02-17-32-57


//Main Activity
package com.shirish.Intent;

import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;

public class MainActivity extends ActionBarActivity {
   Button next;
   Spinner gender,android;
   Editor editor;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
     next=(Button) findViewById(R.id.next);
     setTitle("Intent 1");
     gender=(Spinner)findViewById(R.id.gender);
     android=(Spinner)findViewById(R.id.android);
     SharedPreferences pref = getApplicationContext().getSharedPreferences("Options", MODE_PRIVATE);
     editor=pref.edit();
     gender.setOnItemSelectedListener(new CustomOnItemSelectedListener());
     next.setOnClickListener(new OnClickListener(){
         public void onClick(View v){
            String gen=gender.getSelectedItem().toString();
            String and=android.getSelectedItem().toString();
            editor.putString("gender", gen);
            editor.putString("android", and);
            editor.commit();
            Toast.makeText(getApplicationContext(), "Settings saved!",Toast.LENGTH_LONG ).show();
            Intent in=new Intent(MainActivity.this,NextActivity.class);
            startActivity(in);
            //finish();
          }
      });
   }
}

//CustomOnItemSelectedListener Interface
package com.shirish.Intent;

import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;

public class CustomOnItemSelectedListener implements OnItemSelectedListener {

  @Override
  public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {  }

  @Override
  public void onNothingSelected(AdapterView<?> parent) {  }
}

Download The Source code here:

image4233

Advertisements

4 thoughts on “Android : Spinner

Leave a Comment:

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s