Spinner Tutorial in Android using Basic4Android
Submitted by donbermoy on Saturday, February 1, 2014 - 19:27.
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".
Declare your variable in the Sub_Global like this below. Be sure to type it correctly.
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.
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:
Here's the full code for this tutorial:
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
- Sub Globals
- 'These global variables will be redeclared each time the activity is created.
- 'These variables can only be accessed from this module.
- Dim i As Int
- Dim tgtlabel As Label
- Dim tgtspin As Spinner
- Dim myarray(12) As String
- 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.
- 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.
- Now create a new activity. It should look like this:
- <vb>
- Sub Activity_Create(FirstTime As Boolean)
- Activity.LoadLayout("main")
- myarray(0)="January"
- myarray(1)="February"
- myarray(2)="March"
- myarray(3)="April"
- myarray(4)="May"
- myarray(5)="June"
- myarray(6)="July"
- myarray(7)="August"
- myarray(8)="September"
- myarray(9)="October"
- myarray(10)="November"
- myarray(11)="December"
- tgtspin.Initialize("spin")
- tgtspin.Prompt="Select Month"
- tgtspin.AddAll(myarray)
- Activity.AddView(tgtspin,10dip,10dip,200dip,40dip)
- End Sub
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:
Next, type the following code below. That code means to display the clicked item in the Spinner to the Label.
- Sub spin_ItemClick (Position As Int, Value As Object)
- tgtlabel.Text = Value
- End Sub
- Sub Process_Globals
- 'These global variables will be declared once when the application starts.
- 'These variables can be accessed from all modules.
- End Sub
- Sub Globals
- 'These global variables will be redeclared each time the activity is created.
- 'These variables can only be accessed from this module.
- Dim i As Int
- Dim tgtlabel As Label
- Dim tgtspin As Spinner
- Dim myarray(12) As String
- End Sub
- Sub Activity_Create(FirstTime As Boolean)
- Activity.LoadLayout("main")
- myarray(0)="January"
- myarray(1)="February"
- myarray(2)="March"
- myarray(3)="April"
- myarray(4)="May"
- myarray(5)="June"
- myarray(6)="July"
- myarray(7)="August"
- myarray(8)="September"
- myarray(9)="October"
- myarray(10)="November"
- myarray(11)="December"
- tgtspin.Initialize("spin")
- tgtspin.Prompt="Select Month"
- tgtspin.AddAll(myarray)
- Activity.AddView(tgtspin,10dip,10dip,200dip,40dip)
- End Sub
- Sub Activity_Resume
- End Sub
- Sub Activity_Pause (UserClosed As Boolean)
- End Sub
- Sub spin_ItemClick (Position As Int, Value As Object)
- tgtlabel.Text = Value
- End Sub
Add new comment
- 756 views