You are on page 1of 3

18/07/13

Spinners | Android Developers

Spinners
Spinners provide a quick way to select one value from a set. In the default state, a spinner shows its currently selected value. Touching the spinner displays a dropdown menu with all other available values, from which the user can select a new one.
IN THIS DOCUMENT Populate the Spinner with User Choices Responding to User Selections

KEY CLASSES S p i n n e r S p i n n e r A d a p t e r A d a p t e r V i e w . O n I t e m S e l e c t e d L i s t e n e r

You can add a spinner to your layout with the S p i n n e r( / r e f e r e n c e / a n d r o i d / w i d g e t / S p i n n e r . h t m l )object. You should usually do so in your XML layout with a < S p i n n e r > element. For example: < S p i n n e r a n d r o i d : i d = " @ + i d / p l a n e t s _ s p i n n e r " a n d r o i d : l a y o u t _ w i d t h = " f i l l _ p a r e n t " a n d r o i d : l a y o u t _ h e i g h t = " w r a p _ c o n t e n t "/ > To populate the spinner with a list of choices, you then need to specify a S p i n n e r A d a p t e r ( / r e f e r e n c e / a n d r o i d / w i d g e t / S p i n n e r A d a p t e r . h t m l )in your A c t i v i t y ( / r e f e r e n c e / a n d r o i d / a p p / A c t i v i t y . h t m l )or F r a g m e n t( / r e f e r e n c e / a n d r o i d / a p p / F r a g m e n t . h t m l )source code.

Populate the Spinner with User Choices


The choices you provide for the spinner can come from any source, but must be provided through an S p i n n e r A d a p t e r( / r e f e r e n c e / a n d r o i d / w i d g e t / S p i n n e r A d a p t e r . h t m l ) , such as an A r r a y A d a p t e r ( / r e f e r e n c e / a n d r o i d / w i d g e t / A r r a y A d a p t e r . h t m l )if the choices are available in an array or a C u r s o r A d a p t e r ( / r e f e r e n c e / a n d r o i d / w i d g e t / C u r s o r A d a p t e r . h t m l )if the choices are available from a database query. For instance, if the available choices for your spinner are pre-determined, you can provide them with a string array defined in a string resource file (/guide/topics/resources/string-resource.html): < ? x m lv e r s i o n = " 1 . 0 "e n c o d i n g = " u t f 8 " ? > < r e s o u r c e s > < s t r i n g a r r a yn a m e = " p l a n e t s _ a r r a y " > < i t e m > M e r c u r y < / i t e m > < i t e m > V e n u s < / i t e m > < i t e m > E a r t h < / i t e m > < i t e m > M a r s < / i t e m > < i t e m > J u p i t e r < / i t e m > < i t e m > S a t u r n < / i t e m > < i t e m > U r a n u s < / i t e m > < i t e m > N e p t u n e < / i t e m > < / s t r i n g a r r a y > < / r e s o u r c e s > With an array such as this one, you can use the following code in your A c t i v i t y ( / r e f e r e n c e / a n d r o i d / a p p / A c t i v i t y . h t m l )or F r a g m e n t( / r e f e r e n c e / a n d r o i d / a p p / F r a g m e n t . h t m l )to supply the spinner with the array using an instance of A r r a y A d a p t e r

developer.android.com/guide/topics/ui/controls/spinner.html

1/3

18/07/13

Spinners | Android Developers

( / r e f e r e n c e / a n d r o i d / w i d g e t / A r r a y A d a p t e r . h t m l ) :

S p i n n e rs p i n n e r=( S p i n n e r )f i n d V i e w B y I d ( R . i d . s p i n n e r ) ; / /C r e a t ea nA r r a y A d a p t e ru s i n gt h es t r i n ga r r a ya n dad e f a u l ts p i n n e rl a y o u t A r r a y A d a p t e r < C h a r S e q u e n c e >a d a p t e r=A r r a y A d a p t e r . c r e a t e F r o m R e s o u r c e ( t h i s , R . a r r a y . p l a n e t s _ a r r a y ,a n d r o i d . R . l a y o u t . s i m p l e _ s p i n n e r _ i t e m ) ; / /S p e c i f yt h el a y o u tt ou s ew h e nt h el i s to fc h o i c e sa p p e a r s a d a p t e r . s e t D r o p D o w n V i e w R e s o u r c e ( a n d r o i d . R . l a y o u t . s i m p l e _ s p i n n e r _ d r o p d o w n _ i t e m ) ; / /A p p l yt h ea d a p t e rt ot h es p i n n e r s p i n n e r . s e t A d a p t e r ( a d a p t e r ) ; The c r e a t e F r o m R e s o u r c e ( )


( / r e f e r e n c e / a n d r o i d / w i d g e t / A r r a y A d a p t e r . h t m l # c r e a t e F r o m R e s o u r c e ( a n d r o i d . c o n t e n t . C o n t e x t ,i n t ,i n t ) )

method allows you to create an A r r a y A d a p t e r( / r e f e r e n c e / a n d r o i d / w i d g e t / A r r a y A d a p t e r . h t m l )from the string array. The third argument for this method is a layout resource that defines how the selected choice appears in the spinner control. The s i m p l e _ s p i n n e r _ i t e m ( / r e f e r e n c e / a n d r o i d / R . l a y o u t . h t m l # s i m p l e _ s p i n n e r _ i t e m )layout is provided by the platform and is the default layout you should use unless you'd like to define your own layout for the spinner's appearance. You should then call s e t D r o p D o w n V i e w R e s o u r c e ( i n t )
( / r e f e r e n c e / a n d r o i d / w i d g e t / A r r a y A d a p t e r . h t m l # s e t D r o p D o w n V i e w R e s o u r c e ( i n t ) )to specify the layout the

adapter should use to display the list of spinner choices (s i m p l e _ s p i n n e r _ d r o p d o w n _ i t e m ( / r e f e r e n c e / a n d r o i d / R . l a y o u t . h t m l # s i m p l e _ s p i n n e r _ d r o p d o w n _ i t e m )is another standard layout defined by the platform). Call s e t A d a p t e r ( )( / r e f e r e n c e / a n d r o i d / w i d g e t / A d a p t e r V i e w . h t m l # s e t A d a p t e r ( T ) )to apply the adapter to your S p i n n e r( / r e f e r e n c e / a n d r o i d / w i d g e t / S p i n n e r . h t m l ) .

Responding to User Selections


When the user selects an item from the drop-down, the S p i n n e r( / r e f e r e n c e / a n d r o i d / w i d g e t / S p i n n e r . h t m l ) object receives an on-item-selected event. To define the selection event handler for a spinner, implement the A d a p t e r V i e w . O n I t e m S e l e c t e d L i s t e n e r ( / r e f e r e n c e / a n d r o i d / w i d g e t / A d a p t e r V i e w . O n I t e m S e l e c t e d L i s t e n e r . h t m l )interface and the corresponding o n I t e m S e l e c t e d ( )
( / r e f e r e n c e / a n d r o i d / w i d g e t / A d a p t e r V i e w . O n I t e m S e l e c t e d L i s t e n e r . h t m l # o n I t e m S e l e c t e d ( a n d r o i d . w i d g e t . A d a p t e r V i e w < ? > ,a n d r o i d . v i e w . V i e w ,i n t ,l o n g ) )callback method. For example, here's an implementation of the

interface in an A c t i v i t y( / r e f e r e n c e / a n d r o i d / a p p / A c t i v i t y . h t m l ) : p u b l i cc l a s sS p i n n e r A c t i v i t ye x t e n d sA c t i v i t yi m p l e m e n t sO n I t e m S e l e c t e d L i s t e n e r{ . . . p u b l i cv o i do n I t e m S e l e c t e d ( A d a p t e r V i e w < ? >p a r e n t ,V i e wv i e w , i n tp o s ,l o n gi d ){ / /A ni t e mw a ss e l e c t e d .Y o uc a nr e t r i e v et h es e l e c t e di t e mu s i n g / /p a r e n t . g e t I t e m A t P o s i t i o n ( p o s ) } p u b l i cv o i do n N o t h i n g S e l e c t e d ( A d a p t e r V i e w < ? >p a r e n t ){ / /A n o t h e ri n t e r f a c ec a l l b a c k } } The A d a p t e r V i e w . O n I t e m S e l e c t e d L i s t e n e r


( / r e f e r e n c e / a n d r o i d / w i d g e t / A d a p t e r V i e w . O n I t e m S e l e c t e d L i s t e n e r . h t m l )requires the o n I t e m S e l e c t e d ( ) ( / r e f e r e n c e / a n d r o i d / w i d g e t / A d a p t e r V i e w . O n I t e m S e l e c t e d L i s t e n e r . h t m l # o n I t e m S e l e c t e d ( a n d r o i d . w i d g e t . A d a p t e r V i e w < ? > ,a n d r o i d . v i e w . V i e w ,i n t ,l o n g ) )and o n N o t h i n g S e l e c t e d ( ) ( / r e f e r e n c e / a n d r o i d / w i d g e t / A d a p t e r V i e w . O n I t e m S e l e c t e d L i s t e n e r . h t m l # o n N o t h i n g S e l e c t e d ( a n d r o i d . w i d g e t . A d a p t e r V i e w < ? > ) )callback methods.

Then you need to specify the interface implementation by calling s e t O n I t e m S e l e c t e d L i s t e n e r ( )


( / r e f e r e n c e / a n d r o i d / w i d g e t / A d a p t e r V i e w . h t m l # s e t O n I t e m S e l e c t e d L i s t e n e r ( a n d r o i d . w i d g e t . A d a p t e r V i e w . O n I

developer.android.com/guide/topics/ui/controls/spinner.html

2/3

18/07/13

Spinners | Android Developers

t e m S e l e c t e d L i s t e n e r ) ) :

S p i n n e rs p i n n e r=( S p i n n e r )f i n d V i e w B y I d ( R . i d . s p i n n e r ) ; s p i n n e r . s e t O n I t e m S e l e c t e d L i s t e n e r ( t h i s ) ; If you implement the A d a p t e r V i e w . O n I t e m S e l e c t e d L i s t e n e r


( / r e f e r e n c e / a n d r o i d / w i d g e t / A d a p t e r V i e w . O n I t e m S e l e c t e d L i s t e n e r . h t m l )interface with your A c t i v i t y ( / r e f e r e n c e / a n d r o i d / a p p / A c t i v i t y . h t m l )or F r a g m e n t( / r e f e r e n c e / a n d r o i d / a p p / F r a g m e n t . h t m l )(such as in

the example above), you can pass t h i s as the interface instance.

developer.android.com/guide/topics/ui/controls/spinner.html

3/3

You might also like