Android Question Change button by click on error

Farzam

Member
I want, like in the sample picture, when I click on the wrong answer, the correct answer will be green and the wrong answer will be red...
I make it red with this command, but I can't make the correct answer green

B4X:
Private Sub ButAnswer_Click
    Dim btn As Button = Sender
    Dim gd As ColorDrawable
    
    If AnsverNum = btn.Text Then
        gd.Initialize(Colors.RGB(92,161,58) , 10dip)
        btn.Background = gd
        Sleep(300)
        PlayGame
        Coine
        gd.Initialize(Colors.RGB(41,119,156), 10dip)
        btn.Background = gd
    Else
        gd.Initialize(Colors.Red,10dip)
        btn.Background = gd
        Error
    End If
End Sub
 

Attachments

  • Screenshot 2024-08-24 at 22-05-59 Math Games Brain iq riddles - Apps on Google Play.png
    16.3 KB · Views: 60

Brian Dean

Well-Known Member
Licensed User
Longtime User
Here is one way to do it . . .
B4X:
    btn.As(B4XView).Color = xui.Color_Red
 
Last edited:
Upvote 0

Farzam

Member
Here is one way to do it . . .
B4X:
    btn.As(B4XView).Color = xui.Color_Red


Brother...
I have the command to click on the button and change the color of the button, and I wrote...
If I click, it will show green if it is correct and red if it is wrong...

But when I click on the wrong one, I want it to show me the right option out of 4 options

When I click on each one, the text value of the button is compared with the variable of the correct answer. If it is correct, it goes to the next question.

B4X:
Private Sub ButAnswer_Click
    Dim btn As Button = Sender
    Dim gd As ColorDrawable
    If AnsverNum = btn.Text Then
        gd.Initialize(Colors.RGB(92,161,58) , 10dip)
        btn.Background = gd
        Sleep(300)
        PlayGame
        Coine
        gd.Initialize(Colors.RGB(41,119,156), 10dip)
        btn.Background = gd
    Else
        gd.Initialize(Colors.Red,10dip)
        btn.Background = gd
        Error
    End If
End Sub
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
But when I click on the wrong one, I want it to show me the right option out of 4 options
There are many ways that this can be done - there is not one best answer. Here is how I would do it. The buttons holding the answer values are in a list called "buttons".
B4X:
Private Sub nextQuestion
    Dim answer As Int = Rnd(0, 4)       ' Choose a random button hold correct answer
    Dim i As Int = 0
    For Each btn As Button In buttons   ' Set up choices in buttons
        ' In a real quiz incorrect answers would be used instead of "Wrong"
        If (i = answer) Then btn.Text = "Correct" Else btn.Text = "Wrong"
        btn.As(B4XView).Color = xui.Color_LightGray
        i = i + 1
    Next
End Sub

Private Sub btn_Click
    Dim btn As Button = Sender
    If (btn.Text = "Wrong") Then btn.As(B4XView).Color = xui.Color_Red
    For Each b As Button In buttons
        If (b.Text = "Correct") Then b.As(B4XView).Color = xui.Color_Green
    Next
    Sleep(3000)                          ' Delay to next question
    nextQuestion
End Sub

Here is a complete sample application
 

Attachments

  • Quiz.zip
    9.2 KB · Views: 49
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…