[Tut] About using a widget Spinner

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

[Tut] About using a widget Spinner

Postby tum0rc0re » Sat Jan 12, 2008 9:09 pm

Spinner – is a widget, which is used for storing some set of strings and displaying one of them on the screen via switching between them.

Image

The Spinner object brings only the GUI-functionality, but the SpinnerAdapter provides the common functionality. It’s a bridge between the Spinner and strings with whom this widget works.

The adapter of spinner offers two different ways of interacting with the data. The first method is to use spinner, is using the Left-Right-keys to switch between strings. The second method uses a drop down list for choosing the required string that appears when you click on right edge of the spinner ("<>").

Image

ArrayAdapter is used for filling data. We will use a List to store the strings we want to display. An code-example using the widget (see below).

main.xml

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.  android:orientation="vertical"
  4.  android:layout_width="fill_parent"
  5.  android:layout_height="fill_parent"
  6.  android:background="#ffc5d1d4"
  7. >
  8.  
  9. <TextView
  10.  id="@+id/lblAllCountries"
  11.  android:layout_width="wrap_content"
  12.  android:layout_height="wrap_content"
  13.  android:text="List of countries"
  14.  android:textSize="13dip"
  15.  android:layout_centerHorizontal="true"
  16.  android:layout_marginTop="20dip"
  17. />
  18.  
  19. <Spinner
  20.  id="@+id/spnCountries"
  21.  android:layout_width="150dip"
  22.  android:layout_height="wrap_content"
  23.  android:layout_centerHorizontal="true"
  24.  android:layout_below="@+id/lblAllCountries"
  25. />
  26.  
  27. <TextView
  28.  id="@+id/lblNewCountry"
  29.  android:layout_width="wrap_content"
  30.  android:layout_height="wrap_content"
  31.  android:textSize="13dip"
  32.  android:text="Input new country"
  33.  android:layout_below="@+id/spnCountries"
  34.  android:layout_centerHorizontal="true"
  35. />
  36.  
  37. <EditText
  38.  id="@+id/txtNewCountry"
  39.  android:layout_width="150dip"
  40.  android:layout_height="wrap_content"
  41.  android:layout_below="@+id/lblNewCountry"
  42.  android:layout_centerHorizontal="true"
  43.  android:textSize="13dip"
  44. />
  45.  
  46. <Button
  47.  id="@+id/btnAddItem"
  48.  android:layout_width="150dip"
  49.  android:layout_height="wrap_content"
  50.  android:text="Add country"
  51.  android:gravity="center"
  52.  android:layout_centerInParent="true"
  53.  android:layout_below="@+id/txtNewCountry"
  54.  android:textSize="13dip"
  55. />
  56.  
  57. <Button
  58.  id="@+id/btnRemoveItem"
  59.  android:layout_width="150dip"
  60.  android:layout_height="wrap_content"
  61.  android:text="Remove current country"
  62.  android:layout_below="@+id/btnAddItem"
  63.  android:gravity="center"
  64.  android:layout_centerHorizontal="true"
  65.  android:textSize="13dip"
  66. />
  67. </RelativeLayout>
  68.  
Parsed in 0.006 seconds, using GeSHi 1.0.8.4


SpinnerSample.java

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package maximyudin.spinner;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import android.app.Activity;
  5. import android.os.Bundle;
  6. import android.widget.Spinner;
  7. import android.widget.ArrayAdapter;
  8. import android.widget.Button;
  9. import android.view.View;
  10. import android.widget.EditText;
  11.  
  12. public class SpinnerSample extends Activity {
  13.   private static final String[] mCountries = {
  14.     “Russia“, “Germany“, “Ukraine“, “Belarus“, “USA“
  15.   };
  16.  
  17.   private List<String> allcountries;
  18.   private ArrayAdapter<String> aspnCountries;
  19.   private EditText txtNewCountry;
  20.   private Spinner spnCountries;
  21.  
  22.   @Override
  23.   public void onCreate(Bundle icicle) {
  24.     super.onCreate(icicle);
  25.     setContentView(R.layout.main);
  26.  
  27.     txtNewCountry = (EditText) findViewById(R.id.txtNewCountry);
  28.     spnCountries = (Spinner) findViewById(R.id.spnCountries);
  29.     allcountries = new ArrayList<String>();
  30.  
  31.     for (int i = 0; i < mCountries.length; i++) {
  32.       allcountries.add(mCountries[i]);
  33.     }
  34.  
  35.     aspnCountries = new ArrayAdapter<String>(this,
  36.       android.R.layout.simple_spinner_item, allcountries);
  37.     aspnCountries.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  38.     spnCountries.setAdapter(aspnCountries);
  39.  
  40.     final Button btnAddItem = (Button) findViewById(R.id.btnAddItem);
  41.     btnAddItem.setOnClickListener(btnAddItemListener);
  42.     final Button btnRemoveItem = (Button) findViewById(R.id.btnRemoveItem);
  43.     btnRemoveItem.setOnClickListener(btnRemoveItemListener);
  44. }
  45.  
  46. private Button.OnClickListener btnAddItemListener =
  47.   new Button.OnClickListener() {
  48.     public void onClick(View v) {
  49.       allcountries.add(txtNewCountry.getText().toString());
  50.       aspnCountries = new ArrayAdapter<String>(SpinnerSample.this,
  51.         android.R.layout.simple_spinner_item, allcountries);
  52.       spnCountries.setAdapter(aspnCountries);
  53.     }
  54.   };
  55.  
  56. private Button.OnClickListener btnRemoveItemListener =
  57.   new Button.OnClickListener() {
  58.     public void onClick(View v) {
  59.       allcountries.remove(spnCountries.getSelectedItemIndex());
  60.       aspnCountries = new ArrayAdapter<String>(SpinnerSample.this,
  61.         android.R.layout.simple_spinner_item, allcountries);
  62.       spnCountries.setAdapter(aspnCountries);
  63.     }
  64.   };
  65. }
Parsed in 0.040 seconds, using GeSHi 1.0.8.4


Image
Image

P.S. Here's http://www.maximyudin.com/?p=17 my article is on english and russian languages :)
JQ Soft web page:
[b][url=http://jqsoft.ru/]http://jqsoft.ru/[/url][/b]
My proffesional blog: [b][url=http://maximyudin.blogspot.com]http://maximyudin.blogspot.com[/url][/b]
Facebook: [b][url=http://www.facebook.com/maxim.yudin]http://www.facebook.com/maxim.yudin[/url][/b]
User avatar
tum0rc0re
Senior Developer
Senior Developer
 
Posts: 158
Joined: Sun Nov 25, 2007 7:15 am
Location: Moscow, Russia

Top

Postby cabernet1976 » Thu Jan 24, 2008 1:39 pm

Hi tum0rc0re,

I follow your code, but I get the first type spinner not the second. :cry:
I use LinearLayout not RelativeLayout.
Could you help me out?
Thanks.
Upload2Flickr's blog: http://upload2flickr.blogspot.com
cabernet1976
Senior Developer
Senior Developer
 
Posts: 154
Joined: Fri Nov 16, 2007 2:34 am
Location: China

adding and removing from an ArrayAdapter

Postby udi » Fri Jan 25, 2008 1:10 am

thnx for the tutorial. very helpful.

so. i created the initial list of countries from an xml resource and created the adpater by createFromResource(....) etc. it worked fine and saved some code. i was hoping to not have to 'new' an adapter every time the list changes. unfortunately addObject(.....) and removeObject(.....) on the adapter results in 'operation not supported' exception.
is there something i'm missing or is this a missing and undocumented :) functionality.

/udi
udi
Freshman
Freshman
 
Posts: 6
Joined: Fri Dec 14, 2007 12:21 am
Location: Austin TX

Re: adding and removing from an ArrayAdapter

Postby tum0rc0re » Fri Jan 25, 2008 6:02 am

udi wrote:thnx for the tutorial. very helpful.
i was hoping to not have to 'new' an adapter every time the list changes. unfortunately addObject(.....) and removeObject(.....) on the adapter results in 'operation not supported' exception.
is there something i'm missing or is this a missing and undocumented :) functionality.
/udi


I think that it's little bug in Android. I was hoping that methods addObject and removeObject will be work too, but :( I tried almost all known ways for me for solving this problem (new adapter every time), but without result :(

cabernet1976 wrote:I get the first type spinner not the second


Sorry, I understand you bad. In order to use second look of Spinner you have to push central button of navigation in an emulator when Spinner is focused.
JQ Soft web page:
[b][url=http://jqsoft.ru/]http://jqsoft.ru/[/url][/b]
My proffesional blog: [b][url=http://maximyudin.blogspot.com]http://maximyudin.blogspot.com[/url][/b]
Facebook: [b][url=http://www.facebook.com/maxim.yudin]http://www.facebook.com/maxim.yudin[/url][/b]
User avatar
tum0rc0re
Senior Developer
Senior Developer
 
Posts: 158
Joined: Sun Nov 25, 2007 7:15 am
Location: Moscow, Russia

Postby cabernet1976 » Fri Jan 25, 2008 3:32 pm

Hi tum0rc0re,
Sorry, I haven't get your meaning, could you explain detail?
Thank you.
Upload2Flickr's blog: http://upload2flickr.blogspot.com
cabernet1976
Senior Developer
Senior Developer
 
Posts: 154
Joined: Fri Nov 16, 2007 2:34 am
Location: China

Re: adding and removing from an ArrayAdapter

Postby udi » Fri Jan 25, 2008 5:25 pm

tum0rc0re wrote:
udi wrote:thnx for the tutorial. very helpful.
i was hoping to not have to 'new' an adapter every time the list changes. unfortunately addObject(.....) and removeObject(.....) on the adapter results in 'operation not supported' exception.
is there something i'm missing or is this a missing and undocumented :) functionality.
/udi


I think that it's little bug in Android. I was hoping that methods addObject and removeObject will be work too, but :( I tried almost all known ways for me for solving this problem (new adapter every time), but without result :(


i found out that it's a feature not a bug :) . you can use addObject and removeObject as long as the Adapter was created from an ArrayList. if you create it from a resource i guess it's considered final. so here's the code slightly rewritten. it also clears the new country field and puts it in the selected field after you push the button. i thought maybe that feels a bit better :) . thnx again for the tutorial.
/udi


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. import java.util.ArrayList;
  2. import java.util.List;
  3.  
  4. import android.app.Activity;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.ArrayAdapter;
  8. import android.widget.Button;
  9. import android.widget.EditText;
  10. import android.widget.Spinner;
  11.  
  12. public class SpinnerSample extends Activity {
  13.    
  14.     private static final String[] _countries  = {"Russia", "Ukrain", "Belarus", "Germany", "USA" } ;
  15.     private List<String>                _allCountries;
  16.     private ArrayAdapter<String>        _aspnCountries;
  17.     private EditText                    _txtNewCountry;
  18.     private Spinner                     _spnCountries;
  19.      
  20.     private Button.OnClickListener      _btnAddItemListener =
  21.         new Button.OnClickListener() {
  22.         public void onClick(View view){
  23.             String newCountry = _txtNewCountry.getText().toString();
  24.             if (newCountry != null && !newCountry.equals("")) {
  25.                 _txtNewCountry.setText("");   // clear the input
  26.                 _aspnCountries.addObject(newCountry);
  27.                 int position = _aspnCountries.getPosition(newCountry);
  28.                 _spnCountries.setSelection(position);
  29.             }
  30.         }      
  31.     };
  32.    
  33.     private Button.OnClickListener      _btnRemoveItemListener =
  34.         new Button.OnClickListener() {
  35.         public void onClick(View view){
  36.             _aspnCountries.removeObject(_spnCountries.getSelectedItem().toString());
  37.         }      
  38.     };    
  39.    
  40.     /** Called when the activity is first created. */
  41.     @Override
  42.     public void onCreate(Bundle icicle) {
  43.         super.onCreate(icicle);
  44.         setContentView(R.layout.main);
  45.        
  46.         _txtNewCountry = (EditText)findViewById(R.id.txtNewCountry);
  47.         _spnCountries = (Spinner) findViewById(R.id.spnCountries);
  48.         _allCountries = new ArrayList<String>();
  49.        
  50.         for (String s: _countries){
  51.             _allCountries.add(s);
  52.         }
  53.  
  54.         _aspnCountries = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, _allCountries);
  55.         _aspnCountries.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  56.         _spnCountries.setAdapter(_aspnCountries);
  57.        
  58.         final Button btnAddItem = (Button)findViewById(R.id.btnAddItem);
  59.         btnAddItem.setOnClickListener(_btnAddItemListener);
  60.        
  61.         final Button btnRemoveItem = (Button)findViewById(R.id.btnRemoveItem);
  62.         btnRemoveItem.setOnClickListener(_btnRemoveItemListener);
  63.     }
  64. }
Parsed in 0.041 seconds, using GeSHi 1.0.8.4
udi
Freshman
Freshman
 
Posts: 6
Joined: Fri Dec 14, 2007 12:21 am
Location: Austin TX

Top

Re: adding and removing from an ArrayAdapter

Postby tum0rc0re » Fri Jan 25, 2008 6:03 pm

udi, it's awesome :)) Thanx :wink: If you're not against, I will post your code in my blog, ok?
JQ Soft web page:
[b][url=http://jqsoft.ru/]http://jqsoft.ru/[/url][/b]
My proffesional blog: [b][url=http://maximyudin.blogspot.com]http://maximyudin.blogspot.com[/url][/b]
Facebook: [b][url=http://www.facebook.com/maxim.yudin]http://www.facebook.com/maxim.yudin[/url][/b]
User avatar
tum0rc0re
Senior Developer
Senior Developer
 
Posts: 158
Joined: Sun Nov 25, 2007 7:15 am
Location: Moscow, Russia

Re: adding and removing from an ArrayAdapter

Postby udi » Fri Jan 25, 2008 6:32 pm

tum0rc0re wrote:udi, it's awesome :)) Thanx :wink: If you're not against, I will post your code in my blog, ok?



sure go ahead.
/udi
udi
Freshman
Freshman
 
Posts: 6
Joined: Fri Dec 14, 2007 12:21 am
Location: Austin TX

Re: adding and removing from an ArrayAdapter

Postby tum0rc0re » Fri Jan 25, 2008 6:55 pm

udi wrote:sure go ahead.
/udi


I thank you there :wink: http://www.maximyudin.com/?p=17
JQ Soft web page:
[b][url=http://jqsoft.ru/]http://jqsoft.ru/[/url][/b]
My proffesional blog: [b][url=http://maximyudin.blogspot.com]http://maximyudin.blogspot.com[/url][/b]
Facebook: [b][url=http://www.facebook.com/maxim.yudin]http://www.facebook.com/maxim.yudin[/url][/b]
User avatar
tum0rc0re
Senior Developer
Senior Developer
 
Posts: 158
Joined: Sun Nov 25, 2007 7:15 am
Location: Moscow, Russia

Postby WauloK » Fri Feb 08, 2008 6:23 am

Nice! But how do I find out a selected item in my program?
eg. Someone selects something in the dropdown spinner and I get the item and act upon it?
AndroidChi - Applications for the Google Android platform.
User avatar
WauloK
Developer
Developer
 
Posts: 32
Joined: Wed Dec 19, 2007 4:32 am
Location: Sydney, Australia

Postby tum0rc0re » Fri Feb 08, 2008 7:40 am

WauloK wrote:Nice! But how do I find out a selected item in my program?
eg. Someone selects something in the dropdown spinner and I get the item and act upon it?


getSelectedItem(), getSelectedItemIndex(), getSelectedItemId()
JQ Soft web page:
[b][url=http://jqsoft.ru/]http://jqsoft.ru/[/url][/b]
My proffesional blog: [b][url=http://maximyudin.blogspot.com]http://maximyudin.blogspot.com[/url][/b]
Facebook: [b][url=http://www.facebook.com/maxim.yudin]http://www.facebook.com/maxim.yudin[/url][/b]
User avatar
tum0rc0re
Senior Developer
Senior Developer
 
Posts: 158
Joined: Sun Nov 25, 2007 7:15 am
Location: Moscow, Russia

Spinner with image and text?

Postby AltaisULE » Fri May 09, 2008 6:57 pm

If I want to put an ImageView and a TextView in each row of the spinner. What can I do this? I have to create a LinearLayout (for example) and introduce in it this two objects and then add this layout to the spinner?

Thanks
AltaisULE
Freshman
Freshman
 
Posts: 2
Joined: Sat May 03, 2008 10:24 pm

Postby coding_android » Fri May 09, 2008 10:11 pm

That is something I would be interested as well. Or can we just fill String Objects inside a spinner?

Regards!
User avatar
coding_android
Moderator
Moderator
 
Posts: 76
Joined: Mon May 05, 2008 10:22 am
Location: Germany

Postby tum0rc0re » Sat May 10, 2008 3:01 am

coding_android wrote:That is something I would be interested as well. Or can we just fill String Objects inside a spinner?

Regards!


I think we can add any other view in the Spinner, but I don't try it :)
JQ Soft web page:
[b][url=http://jqsoft.ru/]http://jqsoft.ru/[/url][/b]
My proffesional blog: [b][url=http://maximyudin.blogspot.com]http://maximyudin.blogspot.com[/url][/b]
Facebook: [b][url=http://www.facebook.com/maxim.yudin]http://www.facebook.com/maxim.yudin[/url][/b]
User avatar
tum0rc0re
Senior Developer
Senior Developer
 
Posts: 158
Joined: Sun Nov 25, 2007 7:15 am
Location: Moscow, Russia

Postby coding_android » Sat May 10, 2008 5:07 am

Yeah I will do some tests this evening to see if it is possible.
User avatar
coding_android
Moderator
Moderator
 
Posts: 76
Joined: Mon May 05, 2008 10:22 am
Location: Germany

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 4 guests