0% found this document useful (0 votes)
329 views

Android - How To Use Spinner - Stack Overflow

- The document discusses how to use spinners in Android applications. It provides code samples and explanations for populating spinners with data, setting spinner adapters, and handling item selection events. - When an item is selected in the first spinner, the second spinner's options need to be dynamically updated based on the selection. Multiple responses provide examples of how to populate the second spinner based on the first selection. - Additional context and tutorials for using spinners in Android are recommended, including downloading a complete sample project to better understand implementation.

Uploaded by

Ajay J Verma
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
329 views

Android - How To Use Spinner - Stack Overflow

- The document discusses how to use spinners in Android applications. It provides code samples and explanations for populating spinners with data, setting spinner adapters, and handling item selection events. - When an item is selected in the first spinner, the second spinner's options need to be dynamically updated based on the selection. Multiple responses provide examples of how to populate the second spinner based on the first selection. - Additional context and tutorials for using spinners in Android are recommended, including downloading a complete sample project to better understand implementation.

Uploaded by

Ajay J Verma
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

8/19/13

android - how to use spinner - Stack Overflow L o g . i ( " A A A " , " A u s t r a l i a " ) ; i f ( o b j e c t . e q u a l s ( a u s ) ) { c i t y=a u s . g e t T y p e ( l o c ) ; } e l s e { b r e a k ; Stack Overflow is a question and } answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Tell me more

how to use spinner

A r r a y A d a p t e r < S t r i n g >a d a p t e r=n e wA r r a y A d a p t e r < S t r i n g > ( A c t i v i t y . t h i s ,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 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 _ d r o p d o w n _ i t e m _ 1 l i n e ) ; s p i n n e r 2 . s e t A d a p t e r ( a d a p t e r ) ; L o g . i ( " A A A " , " s p i n n e r 2 " ) ; b r e a k ;

d e f a u l t : L o g . i ( " A A A " ," d e f a u l t1 " ) ; b r e a k ; I am very new to} android. I want to use 2 spinners in my application, one shows the countries list, when any country is selected the other spinner should show the list of cities of that country. when city is } selected some action is performed. plz help me with some sample code. thanks in anticipation @ O v e r r i d e
android {spinner

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 < ? >a r g 0 ) L o g . i ( " A A A " , " N o t h i n gS 1 " ) ;

asked Mar 8 '10 at 3:16 } corotana 26 } ) ;1 1 2

Check this blog, it'll let you know something nice. Ie am also working on same concept, I'll put the answer s p i n n e r 2 . 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 ( n wO n I t e m S e l e c t e d L i s t e n e r ( ) here, when I get a complete solution. Regards, Haps. Harpreet Feb 27 '12 at 11:12 {

@ O v e r r i d e 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 < ? >a r g 0 ,V i e wa r g 1 ,i n tp o s ,l o n gi d ) Answers { i n tl o c=p o s ; s w i t c h( l o c ) Here is something what we can use to add options to spinner2 w.r.t to spinner 1. { c a s e1 : i f ( o b j e c t . e q u a l s ( a u s ) ) { a r e a=a u s . g e t T i t l e ( l o c ) ; } A r r a y A d a p t e r < S t r i n g >a d a p t e r=n e wA r r a y A d a p t e r < S t r i n g > ( A c t i v i t y . t h i s ,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 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 _ d r o p d o w n _ i t e m _ 1 l i n e ) ; s p i n n e r 3 . s e t A d a p t e r ( a d a p t e r ) ; L o g . i ( " " , " s p i n n e r 3 " ) ; b r e a k ; d e f a u l t : b r e a k ; } } @ O v e r r i d e 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 < ? >a r g 0 ) { / /T O D OA u t o g e n e r a t e dm e t h o ds t u b } } ) ; } / /o n c r e a t e

@ O v e r r i d e p u b l i cv o i do n C l i c k ( V i e wv ) { s w i t c h( v . g e t I d ( ) ) { c a s eR . i d . b t n S u b m i t :

stackoverflow.com/questions/2399086/how-to-use-spinner

1/3

8/19/13
b r e a k ; c a s eR . i d . b t n C a n c e l : f i n i s h ( ) ; b r e a k ; d e f a u l t : b r e a k ; } } }

android - how to use spinner - Stack Overflow

If you find this useful, then do give it up vote, so that others can find a good answer easily. For each Country, you have to create a class for it, to just add State, City & Area. So that it doesn't become a mesh at a single at single page. Have fun. Regards, Haps.
answered Feb 27 '12 at 13:28 Harpreet 712 5 24

Here is a sample code which depicts the usage of spinner and action performed when spinner item is selected i m p o r ta n d r o i d . o s . B u n d l e ; i m p o r ta n d r o i d . a p p . A c t i v i t y ; i m p o r ta n d r o i d . v i e w . V i e w ; i m p o r ta n d r o i d . w i d g e t . A d a p t e r V i e w ; i m p o r ta n d r o i d . w i d g e t . A r r a y A d a p t e r ; i m p o r ta n d r o i d . w i d g e t . S p i n n e r ; i m p o r ta 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 ; p u b l i cc l a s sM a i n A c t i v i t ye x t e n d sA c t i v i t y{ S p i n n e rs p i n ; S t r i n gs p i n _ v a l ; S t r i n g [ ]g e n d e r={" M a l e " ," F e m a l e "} ; / / a r r a yo fs t r i n g su s e dt op o p u l a t et h es p i n n e r @ O v e r r i d e p u b l i cv o i do n C r e a t e ( B u n d l es a v e d I n s t a n c e S t a t e ){ s u p e r . o n C r e a t e ( s a v e d I n s t a n c e S t a t e ) ; s e t C o n t e n t V i e w ( R . l a y o u t . a c t i v i t y _ m a i n ) ; / / s e t t i n gl a y o u t s p i n=( 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 _ i d ) ; / / f e t c h i n gv i e w ' si d / / R e g i s t e rac a l l b a c kt ob ei n v o k e dw h e na ni t e mi nt h i sA d a p t e r V i e wh a sb e e ns e l e c t e d s p i n . 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 ( n e wO n I t e m S e l e c t e d L i s t e n e r ( ){ @ O v e r r i d e 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 < ? >a r g 0 ,V i e wa r g 1 , i n tp o s i t i o n ,l o n gi d ){ / /T O D OA u t o g e n e r a t e dm e t h o ds t u b s p i n _ v a l=g e n d e r [ p o s i t i o n ] ; / / s a v i n gt h ev a l u es e l e c t e d

} @ O v e r r i d e 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 < ? >a r g 0 ){ / /T O D OA u t o g e n e r a t e dm e t h o ds t u b

stackoverflow.com/questions/2399086/how-to-use-spinner

2/3

8/19/13

android - how to use spinner - Stack Overflow


To add a list of values to the spinner, you then need to specify a SpinnerAdapter in your Activity, which extends Adapter class.. A spinner adapter allows to define two different views: one that shows the data in the spinner itself and one that shows the data in the drop down list when the spinner is pressed.You can also download & refer to the complete spinner_demo example project for better understanding.
answered Oct 20 '12 at 10:41 Rachita Nanda 738 9

Check the following examples : http://developer.android.com/intl/fr/guide/tutorials/views/hello-spinner.html http://www.designerandroid.com/?cat=4


answered Mar 8 '10 at 9:58 Karan 5,013 1 16 21

Not the answer you're looking for? Browse other questions tagged or ask your own question.

android spinner

stackoverflow.com/questions/2399086/how-to-use-spinner

3/3

You might also like