Android Question [SOLVED] Spinner dropdown event

adrianstanescu85

Active Member
Licensed User
Longtime User
Hello

I have a simple spinner in my app and I'd like to turn on/off a timer whenever that spinner is clicked on. That is NOT selecting an item inside, for which there is a dedicated even, but I'm interested in catching the even that occurs when the spinner shows its contents by dropdown.

Any ideas?
Thank you!
Adrian
 

LucaMs

Expert
Licensed User
Longtime User
I am not an expert in the use of Reflection, but I have tried to write this:
B4X:
Sub Activity_Create(FirstTime As Boolean)
'...
    If FirstTime Then
        Spinner1.Add("One")
        Spinner1.Add("Two")
        AddOnTouch(Spinner1, "Spinner1_onTouch")
    End If
End Sub

Sub Spinner1_onTouch(V As Object, Action As Int, X As Float, Y As Float, MotionEvent As Object) As Boolean
    Log("Spinner1_onTouch")
    Return False
End Sub


Sub AddOnTouch(V As View, Event As String)
    Private r As Reflector
    r.Target = V
    r.SetOnTouchListener(Event)
End Sub

(You should import the library Reflection)


Are you sure you do not have a more simple solution instead of using a timer, check the opening of the spinner, etc?
 
Upvote 0

adrianstanescu85

Active Member
Licensed User
Longtime User
Thank you for the suggestion, I'll try it on!

The timer is just an example, what I need is just to fire any action, at least a log, when that spinner is pressed.
 
Upvote 0

adrianstanescu85

Active Member
Licensed User
Longtime User
I tried the example above and it gave out an error saying the Spinner should be first initialized. I'm using a Spinner that's embedded in the layout that I designed with the visual designer, so as far as I know those are already initialized.

Any suggestions?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
I tried the example above and it gave out an error saying the Spinner should be first initialized. I'm using a Spinner that's embedded in the layout that I designed with the visual designer, so as far as I know those are already initialized.
Any suggestions?

If it is part of a layout it should be initialized after LoadLayout(..)
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
In this line
AddOnTouch(Spinner1, "Spinner1_onTouch")
you must replace Spinner1 by the name of your spinner !
The code in the example that is in FirstTime should be out of FirstTime.
It would be easier to help if you posted your code.
 
Upvote 0

adrianstanescu85

Active Member
Licensed User
Longtime User
Ready! You were right, I just figured this out too just now! I moved AddOnTouch after loading the Layout and it all runs brilliantly!

THANK YOU!
 
Upvote 0
Top