Android Question Derive text from Spinner

Matteo Granatiero

Active Member
Licensed User
I would like to get text from Spinner, that is I have a list and based on what I chose then there is a button that gets the element (text) chosen
 

Peter Simpson

Expert
Licensed User
Longtime User
You don't need a button to select the item, you just use the spinner event called ItemClick. Anyway here are both ways for you to learn from.

B4X:
Sub Globals
    Private Spinner1 As Spinner
    Private Button1 As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Spinner1.Initialize("Spinner1")
    Button1.Initialize("Button1")
 
    Activity.AddView(Spinner1, 50%x - 100dip, 10%y, 200dip, 25dip)
    Activity.AddView(Button1, 50%x - 50dip, 50%y - 50dip, 100dip, 100dip)
 
    For i = 0 To 10
        Spinner1.Add("Option " & i)
    Next
End Sub

Sub Spinner1_ItemClick (Position As Int, Value As Object)
    Log($"Position = ${Position}, Value = ${Value} "$)
End Sub

Sub Button1_Click
    Log($"Position = ${Spinner1.SelectedIndex}, Value = ${Spinner1.SelectedItem} "$)
End Sub

Goodnight...
 
Last edited:
Upvote 0

Matteo Granatiero

Active Member
Licensed User
Now I try it ;)
You don't need a button to select the item, you just use the spinner event called ItemClick. Anyway here are both ways for you to learn from.

B4X:
Sub Globals
    Private Spinner1 As Spinner
    Private Button1 As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Spinner1.Initialize("Spinner1")
    Button1.Initialize("Button1")
 
    Activity.AddView(Spinner1, 50%x - 100dip, 10%y, 200dip, 25dip)
    Activity.AddView(Button1, 50%x - 50dip, 50%y - 50dip, 100dip, 100dip)
 
    For i = 0 To 10
        Spinner1.Add("Option " & i)
    Next
End Sub
Now I try it
Sub Spinner1_ItemClick (Position As Int, Value As Object)
    Log($"Position = ${Position}, Value = ${Value} "$)
End Sub

Sub Button1_Click
    Log($"Position = ${Spinner1.SelectedIndex}, Value = ${Spinner1.SelectedItem} "$)
End Sub

Goodnight...
 
Upvote 0
Top