Android Question Catch touch method on a label on a panel

DeclanOK

Member
Licensed User
Is there any way to do this. I want the button to swipe right or left on a question.

Thanks as ever
 

Brian Dean

Well-Known Member
Licensed User
Longtime User
I want the button to swipe right or left on a question.
If you simply want to swipe right/left for yes/no answers then this is quite easy, although in this situation you cannot use SetLayoutAnimated and have to animate the label movement yourself. Put a panel over the label to catch the swipes. Here is some code, and I also attach a sample project.

B4X:
Sub Process_Globals
    Private xui As XUI
    Private aTimer As Timer
End Sub

Sub Globals
    Private lblQuestion As Label
    Private pnlTouch As Panel  
    Private xDown As Float                                        ' Initial touch position
    Private move As Int = 0                                        ' Non-zero while moving label
    Private number As Int = 1                                    ' Current question number
    Private left As Int                                                ' Used to animate label movement  
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
    lblQuestion.Text = "Question 1. Swipe right or left"
    left = lblQuestion.left
    aTimer.Initialize("timer", 10)
    aTimer.Enabled = True
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Private Sub timer_Tick
    If move = 0 Then Return                                    ' Check if label animation in progress
    Dim L As Int = lblQuestion.Left + move    ' Apply animation movement
    lblQuestion.SetLayout(L, lblQuestion.Top, lblQuestion.Width, lblQuestion.Height)
    If (lblQuestion.Left > 100%x) Or (lblQuestion.Left < -(lblQuestion.Width)) Then nextQuestion
End Sub

Private Sub nextQuestion
    move = 0                                                                ' Turn off animation
    number = number + 1                                            ' Prepare for next question
    lblQuestion.left = left
    lblQuestion.Text = "This is question number " & number & "."
    lblQuestion.SetLayout(left, lblQuestion.Top, lblQuestion.Width, lblQuestion.Height)
End Sub

Private Sub pnlTouch_Touch (Action As Int, X As Float, Y As Float)
    If (Action = Activity.ACTION_DOWN) Then xDown = X
    If (Action = Activity.ACTION_MOVE) Then
        If (Abs(x - xDown) > 20%x) Then                ' Check threshold for swipe
            ' Note : Change value of [move] to change swipe speed
            If (x > xDown) Then move = 24 Else move = -24          
        End If
    End If  
End Sub

Here is the sample project ...
 

Attachments

  • SwipeLR.zip
    9.5 KB · Views: 55
Upvote 0
Top