• We just launched and are currently in beta. Join us as we build and grow the community.

Spinner Tutorial in Android using Basic4Android

shane098

Crypto Community Manager
S Rep
0
0
0
Rep
0
S Vouches
0
0
0
Vouches
0
Posts
168
Likes
23
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 400 XP
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_1.png


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. Now create a new

    activity. It should look like

    this:
  3. <vb>
  4. Sub

    Activity_Create(FirstTime As

    Boolean

    )
  5. Activity.LoadLayout("main"

    )

  6. myarray(0)="January"
  7. myarray(1)="February"
  8. myarray(2)="March"
  9. myarray(3)="April"
  10. myarray(4)="May"
  11. myarray(5)="June"
  12. myarray(6)="July"
  13. myarray(7)="August"
  14. myarray(8)="September"
  15. myarray(9)="October"
  16. myarray(10)="November"
  17. myarray(11)="December"
  18. tgtspin.Initialize("spin"

    )
  19. tgtspin.Prompt="Select Month"
  20. tgtspin.AddAll(myarray)

  21. Activity.AddView(tgtspin,10dip,10dip,200dip,40dip)
  22. End

    Sub




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_1.png


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:

finalimage_2.png


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. End

    Sub

  5. Sub

    Globals
  6. 'These global variables will be redeclared each time the activity is created.
  7. 'These variables can only be accessed from this module.
  8. Dim

    i As

    Int
  9. Dim

    tgtlabel As

    Label
  10. Dim

    tgtspin As

    Spinner
  11. Dim

    myarray(12) As

    String
  12. End

    Sub

  13. Sub

    Activity_Create(FirstTime As

    Boolean

    )
  14. Activity.LoadLayout("main"

    )

  15. myarray(0)="January"
  16. myarray(1)="February"
  17. myarray(2)="March"
  18. myarray(3)="April"
  19. myarray(4)="May"
  20. myarray(5)="June"
  21. myarray(6)="July"
  22. myarray(7)="August"
  23. myarray(8)="September"
  24. myarray(9)="October"
  25. myarray(10)="November"
  26. myarray(11)="December"
  27. tgtspin.Initialize("spin"

    )
  28. tgtspin.Prompt="Select Month"
  29. tgtspin.AddAll(myarray)

  30. Activity.AddView(tgtspin,10dip,10dip,200dip,40dip)
  31. End

    Sub

  32. Sub

    Activity_Resume

  33. End

    Sub

  34. Sub

    Activity_Pause (UserClosed As

    Boolean

    )

  35. End

    Sub

  36. Sub

    spin_ItemClick (Position As

    Int, Value As

    Object

    )
  37. tgtlabel.Text = Value
  38. 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


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,340

326,200

326,208

Top