Spinner Tutorial in Android using Basic4Android

Good day! This is my other tutorial on controls. The one that i had tutored was the AutoCompleteEditText, and now the Spinner. 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. Spinner is the same as ComboBox in Visual Basic. On this, you need to create one Label to have a result when you choose an item in the Spinner. Named it as tgtLabel. Your abstract designer will be like this one below. Name your designer as "main". abstractdesign Declare your variable in the Sub_Global like this below. Be sure to type it correctly.
  1. Sub Globals
  2. 'These global variables will be redeclared each time the activity is created.
  3. 'These variables can only be accessed from this module.
  4. Dim i As Int
  5. Dim tgtlabel As Label
  6. Dim tgtspin As Spinner
  7. Dim myarray(12) As String
  8. End Sub

Note:

Dim tgtlabel As Label - we initialized tgtlabel as our Label to display the item you clicked in the Spinner. Dim tgtspin As Spinner - we initialized tgtspin as Spinner to be the highlight of this tutorial. Take note that we can have Spinner even though we didn't put them in our abstract design.
  1. Dim myarray(12) As String<vb> - we initialized myarray as aour variable in Array as String. Take note that we have 12 values of our array so that we can display the months in the calendar.
  2.  
  3.  
  4. Now create a new activity. It should look like this:
  5. <vb>
  6. Sub Activity_Create(FirstTime As Boolean)
  7. Activity.LoadLayout("main")
  8.  
  9. myarray(0)="January"
  10. myarray(1)="February"
  11. myarray(2)="March"
  12. myarray(3)="April"
  13. myarray(4)="May"
  14. myarray(5)="June"
  15. myarray(6)="July"
  16. myarray(7)="August"
  17. myarray(8)="September"
  18. myarray(9)="October"
  19. myarray(10)="November"
  20. myarray(11)="December"
  21. tgtspin.Initialize("spin")
  22. tgtspin.Prompt="Select Month"
  23. tgtspin.AddAll(myarray)
  24.  
  25. Activity.AddView(tgtspin,10dip,10dip,200dip,40dip)
  26. End Sub
  27.  
Note: myarray as String has twelve values but an array will start in index 0. So myarray now has the following value in index 0 to 11 containing the values of the month in the calendar.  tgtspin.Prompt="Select Month" - this syntax displays the title of our Spinner entitled "Select Month". tgtspin.AddAll(myarray) - this syntax will add all the values of myarray as it holds the 12 months in the calendar. Activity.AddView(tgtspin,10dip,10dip,200dip,40dip) - this syntax adds a view containing our spinner even if we didn't put it in the abstract design containing the views in the Left, Top, Width, and the Height. Now your activity when run will look like this: firstrun Next, type the following code below. That code means to display the clicked item in the Spinner to the Label.
  1. Sub spin_ItemClick (Position As Int, Value As Object)
  2. tgtlabel.Text = Value
  3. End Sub
The code above means when clicking an item in the spinner, the tgtlabel will display the value clicked in the Spinner. When you click the Spinner it will look like this: final Here's the full code for this tutorial:
  1. Sub Process_Globals
  2. 'These global variables will be declared once when the application starts.
  3. 'These variables can be accessed from all modules.
  4.  
  5. End Sub
  6.  
  7. Sub Globals
  8. 'These global variables will be redeclared each time the activity is created.
  9. 'These variables can only be accessed from this module.
  10. Dim i As Int
  11. Dim tgtlabel As Label
  12. Dim tgtspin As Spinner
  13. Dim myarray(12) As String
  14. End Sub
  15.  
  16. Sub Activity_Create(FirstTime As Boolean)
  17. Activity.LoadLayout("main")
  18.  
  19. myarray(0)="January"
  20. myarray(1)="February"
  21. myarray(2)="March"
  22. myarray(3)="April"
  23. myarray(4)="May"
  24. myarray(5)="June"
  25. myarray(6)="July"
  26. myarray(7)="August"
  27. myarray(8)="September"
  28. myarray(9)="October"
  29. myarray(10)="November"
  30. myarray(11)="December"
  31. tgtspin.Initialize("spin")
  32. tgtspin.Prompt="Select Month"
  33. tgtspin.AddAll(myarray)
  34.  
  35. Activity.AddView(tgtspin,10dip,10dip,200dip,40dip)
  36. End Sub
  37.  
  38. Sub Activity_Resume
  39.  
  40. End Sub
  41.  
  42. Sub Activity_Pause (UserClosed As Boolean)
  43.  
  44. End Sub
  45.  
  46. Sub spin_ItemClick (Position As Int, Value As Object)
  47. tgtlabel.Text = Value
  48. End Sub
Download the source code below and try it! For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment