Android Question Button Change Color

holyraf

Member
Hi, It's me again. Follow up question to my old thread about Quiz App. This is the scenario. At the end of the exam (Result Layout). The user have an option to Retry or Review the Exam. In the Review module, I have generated buttons depending on the Number of Questions. Now let's say the app generated 5 buttons. My question is how can I change the color of the buttons if the user in Correct or Wrong on that specific question. Let's say that the user is correct in Question #1 (Button 1) then the color will be green and then if the user is wrong it will be color red. Here is my Code on how I generate buttons:

B4X:
Sub Activity_Resume
    drawButtons
    setButtons(items)
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub drawButtons
    btns.Clear
    Dim pad As Int = 4dip
    Dim x As Int = pad
    Dim y As Int = pad
    Dim w As Int = (ScrollView1.Width - 5 * pad) / 3
    Dim h As Int = (ScrollView1.Height - 6 * pad) / 3
    For i = 0 To 60 - 1
        Dim btn As Button
        btn.Initialize("btn")
        ScrollView1.Panel.AddView(btn, x, y, w, h)
        btn.Textsize = 40
        btn.TextColor = xui.Color_White
        btn.Color = xui.Color_Blue
    
        x = x + w + pad
        If ((i Mod 3) = 2) Then
            x = pad
            y = y + pad + h
        End If
        btn.Tag = i
        btns.Add(btn)
    Next
End Sub

Sub setButtons(data As List)
    Dim i As Int = 0
    Do While (i < btns.size)
        Dim b As Button
        b = btns.Get(i)
        If (i < items.Size) Then
            b.Text = data.Get(i)
        Else
            b.Text = ""
        End If
        i = i + 1
    Loop
End Sub

I have attached a example video of my current app process here:

 
Solution
Here is an example with a map.
B4A:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    Private ScrollView1 As ScrollView
    Dim btn As Button
    Private map1 As Map
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
    btn.Initialize("btn")
    
    ' for example only *****
    map1.Initialize
    map1.Put(1,True)
    map1.Put(2,True)
    map1.Put(3,False)
    map1.Put(4,True)
    map1.Put(5,False)
    '***********************
     drawButtons
 
End Sub

Sub drawButtons
    Dim pad As Int = 4dip
    Dim x As Int = pad
    Dim y As Int = pad
    Dim w As Int = (ScrollView1.Width - 5 * pad) / 3
    Dim h As Int = (ScrollView1.Height - 6 * pad) / 3
    For i...

zed

Active Member
Licensed User
When the question is asked, put the question number and the "true/false" answer in a map.
You just have to read the map to put the color of the buttons
 
Upvote 0

zed

Active Member
Licensed User
Here is an example with a map.
B4A:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    Private ScrollView1 As ScrollView
    Dim btn As Button
    Private map1 As Map
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
    btn.Initialize("btn")
    
    ' for example only *****
    map1.Initialize
    map1.Put(1,True)
    map1.Put(2,True)
    map1.Put(3,False)
    map1.Put(4,True)
    map1.Put(5,False)
    '***********************
     drawButtons
 
End Sub

Sub drawButtons
    Dim pad As Int = 4dip
    Dim x As Int = pad
    Dim y As Int = pad
    Dim w As Int = (ScrollView1.Width - 5 * pad) / 3
    Dim h As Int = (ScrollView1.Height - 6 * pad) / 3
    For i = 0 To map1.Size-1
        Dim btn As Button
        btn.Initialize("btn")
        ScrollView1.Panel.AddView(btn, x, y, w, h)
        btn.Textsize = 40
        btn.TextColor = xui.Color_White
        If map1.GetValueAt(i) = True Then
            btn.Color = xui.Color_Green
            btn.Text = map1.GetKeyAt(i)
        Else
            btn.Color = xui.Color_Red
            btn.Text = map1.GetKeyAt(i)
        End If
        
        x = x + w + pad
        If ((i Mod 3) = 2) Then
            x = pad
            y = y + pad + h
        End If
        btn.Tag = i
    Next
End Sub
 

Attachments

  • screenshot.png
    screenshot.png
    18.4 KB · Views: 124
  • testButton.zip
    9.5 KB · Views: 139
Upvote 1
Solution

holyraf

Member
Here is an example with a map.
B4A:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    Private ScrollView1 As ScrollView
    Dim btn As Button
    Private map1 As Map
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
    btn.Initialize("btn")
   
    ' for example only *****
    map1.Initialize
    map1.Put(1,True)
    map1.Put(2,True)
    map1.Put(3,False)
    map1.Put(4,True)
    map1.Put(5,False)
    '***********************
     drawButtons
 
End Sub

Sub drawButtons
    Dim pad As Int = 4dip
    Dim x As Int = pad
    Dim y As Int = pad
    Dim w As Int = (ScrollView1.Width - 5 * pad) / 3
    Dim h As Int = (ScrollView1.Height - 6 * pad) / 3
    For i = 0 To map1.Size-1
        Dim btn As Button
        btn.Initialize("btn")
        ScrollView1.Panel.AddView(btn, x, y, w, h)
        btn.Textsize = 40
        btn.TextColor = xui.Color_White
        If map1.GetValueAt(i) = True Then
            btn.Color = xui.Color_Green
            btn.Text = map1.GetKeyAt(i)
        Else
            btn.Color = xui.Color_Red
            btn.Text = map1.GetKeyAt(i)
        End If
       
        x = x + w + pad
        If ((i Mod 3) = 2) Then
            x = pad
            y = y + pad + h
        End If
        btn.Tag = i
    Next
End Sub

Thank you for this. I have one last question: How can I set the ScrollView height based on the number of buttons present?
 
Upvote 0

zed

Active Member
Licensed User
you can add this
B4A:
Dim h As Int = 200dip '(ScrollView1.Height - 6 * pad) / 3
Dim nbrLine As Double = Round(map1.Size/2.6)
Dim svheight As Int = (h+pad)*nbrLine
ScrollView1.Panel.Height = svheight+pad
 

Attachments

  • testButton.zip
    9.6 KB · Views: 160
  • screenshot.png
    screenshot.png
    21.7 KB · Views: 129
Last edited:
Upvote 0
Top