Android Question Not selected spinner

rossati

Active Member
Licensed User
Longtime User
Hello

When the spinner is visible it shows the first value even if it has not been selected yet.
Is there a spinner property that goes through this behavior instead of inserting an empty item?

Thanks

John Rossati
 

ronell

Well-Known Member
Licensed User
Longtime User
B4X:
Private Spinner1 As Spinner

spinner1.add("firstitem")
spinner1.add("seconditem")
it will already show "firstitem" by default even if it is not selected ,
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
Yes, it behaves differently to windows VB where (if I recall correctly) when selecting index=-1 showed an empty box.

As @ronell said, in Android the default is to show an existing item. To overcome this behaviour, you could change the adapter
or make the trick by playing with colors.

Fast working example which shows "nothing" until one item is selected, playing with colors
B4X:
Sub Activity_Create(FirstTime As Boolean)
  Dim sp As Spinner
   sp.Initialize("sp")   
   sp.DropdownTextColor = Colors.White                   '<--- Define whichever you want according to app theme
   sp.DropdownBackgroundColor = Colors.Blue
   sp.TextColor = sp.DropdownBackgroundColor          '<--- Assign same color for text and background
   sp.Color = sp.DropdownBackgroundColor
   sp.AddAll(Array As String("one","two","three"))   
   sp.Tag =True                                           '<---- will mark it as "empty"   
   Activity.AddView(sp,20dip,50%Y,120dip,40dip)

End Sub

Sub  sp_ItemClick (Position As Int, Value As Object)
 
  Dim sp As Spinner = Sender
   Dim empty As Boolean = sp.tag
   If empty Then                 'Will only need it the first time
     sp.TextColor = sp.DropdownTextColor
     sp.Clear                       'Clear and re-populate the spinner. This is the only way to force redraw everything. Invalidate would also work but needs an additional click
     sp.AddAll(Array As String("one","two","three"))
     sp.Tag=False
   End If
    
   '...process the event normally
End Sub
 
Upvote 0
Top