Android WheelView Tutorial using Basic4Android

Today, I will introduce another tutorial called WheelView. WheelView is a control in android that is used for scrolling, customized items, customized labels, and has event listeners. On this, you need to create this Sub Globals like this:
  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 val(12) As String
  5.  Dim svstep As Int
  6.  Dim wv1,wv2,wv3 As WheelView
  7.  Dim overlay As Panel
  8.  Dim result As Label
  9. End Sub

Note:

val(12) - our val here is our array variable for creating the months and it has twelve elements. svstep - our variable for initializing a dip wv1,wv2,wv3 - variables used for WheelView overlay - our variable for creating panel result - our variable used for displaying the value of the WheelView Take note: We can use this initialization above without using the designer. Next, we will initialize the value of our wheels. Type the code below:
  1. Sub init_wheels
  2. Dim lst As List
  3. lst.Initialize
  4. val(0) = "Jan"
  5. val(1) = "Feb"
  6. val(2) = "Mar"
  7. val(3) = "Apr"
  8. val(4) = "May"
  9. val(5) = "Jun"
  10. val(6) = "Jul"
  11. val(7) = "Aug"
  12. val(8) = "Sep"
  13. val(9) = "Oct"
  14. val(10) = "Nov"
  15. val(11) = "Dec"
  16.  
  17. For i = 0 To 11
  18.         lst.Add(val(i))
  19. Next
  20. svstep = 36dip
  21. wv1.Initialize(svstep,1,31,True,"wv1")
  22. wv2.Initialize1(svstep,lst,True,"wv2") ' using lst as list
  23. 'wv2.Initialize2(svstep,val,True,"wv2") ' using val as array
  24. wv3.Initialize(svstep,2000,2030,False,"wv3")
  25.  
  26. Activity.AddView(wv1,100dip, 170dip,40dip,svstep*3)
  27. Activity.AddView(wv2,140dip, 170dip,50dip,svstep*3)
  28. Activity.AddView(wv3,190dip, 170dip,70dip,svstep*3)
  29. overlay.Initialize("")
  30. overlay.SetBackgroundImage(LoadBitmap(File.DirAssets,"cover.png"))
  31. Activity.AddView(overlay,100dip,170dip,160dip,svstep*3)
  32. DoEvents
  33. End Sub

Note:

Dim lst As List - we initialize lst to have our control as List.
  1. val(0) = "Jan"
  2. val(1) = "Feb"
  3. val(2) = "Mar"
  4. val(3) = "Apr"
  5. val(4) = "May"
  6. val(5) = "Jun"
  7. val(6) = "Jul"
  8. val(7) = "Aug"
  9. val(8) = "Sep"
  10. val(9) = "Oct"
  11. val(10) = "Nov"
  12. val(11) = "Dec"
This line of code above has the value of the months. We initialized the val variable as the container of the array.
  1. For i = 0 To 11
  2.         lst.Add(val(i))
  3. Next
The looping above puts the value of our String Month to our list and will put in the WheelView as we initialized it.
  1. svstep = 36dip
  2. wv1.Initialize(svstep,1,31,True,"wv1")
  3. wv2.Initialize1(svstep,lst,True,"wv2") ' using lst as list
  4. 'wv2.Initialize2(svstep,val,True,"wv2") ' using val as array
  5. wv3.Initialize(svstep,2000,2030,False,"wv3")
The code above shows the wheelstep that is equal to svstep or 36dip, 1 as first step, the 31 us the last step, True here is for cyclic wheelview (Boolean), and "wv1" as the initialization. Then we put the wheelview to the activity where the code is like this:
  1. Activity.AddView(wv1,100dip, 170dip,40dip,svstep*3)
  2. Activity.AddView(wv2,140dip, 170dip,50dip,svstep*3)
  3. Activity.AddView(wv3,190dip, 170dip,70dip,svstep*3)
The activity adds the view of WheelView for wv1,wv2,wv3 as it concerns with the left size, top size, width, and the height that is being multiplied by 3. Next, we will initialize the label just like the code below:
  1. Sub init_label
  2. result.Initialize("")
  3. result.TextSize = 24
  4. result.Gravity = Gravity.CENTER_HORIZONTAL
  5. Activity.AddView(result,100dip,100dip,150dip,40dip)
  6. End Sub

Note:

The code above will display the resulting date which is equal to the present date. Next, we will set the value of wv1 to day, wv2 to month, and wv3 to year. Type this code and named this procedure show_Today:
  1. Sub show_today
  2. wv1.SetToValue( DateTime.GetDayOfMonth(DateTime.Now))
  3. wv2.SetToValue( DateTime.GetMonth(DateTime.Now))
  4. wv3.SetToValue(DateTime.Getyear(DateTime.Now)-2000)
  5. End Sub
After setting to the present date make a sub procedure and named it as show_result. This means that it will read the result of the wheel in wv1, wv2, and wv3. Then the result will be displayed to the label named "result".
  1. Sub show_result
  2. result.Text =   wv1.ReadWheel & " " & wv2.ReadWheel & "  " & wv3.ReadWheel
  3. End Sub
Then, code also for showing the result of our WheelView wv1, wv2, and wv3 to display the result in our sub procedure show_result.
  1.  
  2. Sub wv1_tick
  3. show_result
  4. End Sub
  5.  
  6. Sub wv2_tick
  7. show_result
  8. End Sub
  9.  
  10. Sub wv3_tick
  11. show_result
  12. End Sub
Here's the complete 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. End Sub
  5.  
  6. Sub Globals
  7.         'These global variables will be redeclared each time the activity is created.
  8.         'These variables can only be accessed from this module.
  9. Dim val(12) As String
  10. Dim svstep As Int
  11. Dim wv1,wv2,wv3 As WheelView
  12. Dim overlay As Panel
  13. Dim result As Label
  14. End Sub
  15.  
  16. Sub Activity_Create(FirstTime As Boolean)
  17. Activity.Title = "WheelView Tutorial - Lyndon Bermoy"
  18. init_label
  19. init_wheels
  20. show_today
  21. End Sub
  22.  
  23. Sub Activity_Resume
  24.  
  25. End Sub
  26.  
  27. Sub Activity_Pause (UserClosed As Boolean)
  28.  
  29. End Sub
  30.  
  31. Sub init_label
  32. result.Initialize("")
  33. result.TextSize = 24
  34. result.Gravity = Gravity.CENTER_HORIZONTAL
  35. Activity.AddView(result,100dip,100dip,150dip,40dip)
  36. End Sub
  37.  
  38. Sub init_wheels
  39. Dim lst As List
  40. lst.Initialize
  41. val(0) = "Jan"
  42. val(1) = "Feb"
  43. val(2) = "Mar"
  44. val(3) = "Apr"
  45. val(4) = "May"
  46. val(5) = "Jun"
  47. val(6) = "Jul"
  48. val(7) = "Aug"
  49. val(8) = "Sep"
  50. val(9) = "Oct"
  51. val(10) = "Nov"
  52. val(11) = "Dec"
  53.  
  54. For i = 0 To 11
  55.         lst.Add(val(i))
  56. Next
  57. svstep = 36dip
  58. wv1.Initialize(svstep,1,31,True,"wv1")
  59. wv2.Initialize1(svstep,lst,True,"wv2") ' using lst as list
  60. 'wv2.Initialize2(svstep,val,True,"wv2") ' using val as array
  61. wv3.Initialize(svstep,2000,2030,False,"wv3")
  62. wv1.In
  63. Activity.AddView(wv1,100dip, 170dip,40dip,svstep*3)
  64. Activity.AddView(wv2,140dip, 170dip,50dip,svstep*3)
  65. Activity.AddView(wv3,190dip, 170dip,70dip,svstep*3)
  66. overlay.Initialize("")
  67. overlay.SetBackgroundImage(LoadBitmap(File.DirAssets,"cover.png"))
  68. Activity.AddView(overlay,100dip,170dip,160dip,svstep*3)
  69. DoEvents
  70. End Sub
  71.  
  72. Sub show_today
  73. wv1.SetToValue( DateTime.GetDayOfMonth(DateTime.Now))
  74. wv2.SetToValue( DateTime.GetMonth(DateTime.Now))
  75. wv3.SetToValue(DateTime.Getyear(DateTime.Now)-2000)
  76. End Sub
  77.  
  78. Sub wv1_tick
  79. show_result
  80. End Sub
  81.  
  82. Sub wv2_tick
  83. show_result
  84. End Sub
  85.  
  86. Sub wv3_tick
  87. show_result
  88. End Sub
  89.  
  90. Sub show_result
  91. result.Text =   wv1.ReadWheel & " " & wv2.ReadWheel & "  " & wv3.ReadWheel
  92. End Sub
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 R. Bermoy IT Instructor/System Developer/Android Developer STI College - Surigao City Mobile: 09488225971 E-mail:[email protected] Follow and add me in my Facebook Account: https://www.facebook.com/donzzsky

Add new comment