Alternate the text and background colors by item in a ScrollView

William Hunter

Active Member
Licensed User
Longtime User
Is it possible to alternate the text and background colors by item in a ScrollView? For example, item 1: black background and white text – item 2: white background and black text – and so on.

Any comments would be greatly appreciated. :)
 

William Hunter

Active Member
Licensed User
Longtime User
Thank you for your response Klaus. I should have included more detail with my query. I would like to populate the ScrollView using a For/Next loop similar to the code example below, which uses text strings contained in arrays to populate the ScrollView. It’s within this loop that I would have to make the color changes. I know that I can set the text color etc from within this loop, but it will be affected for every item in the ScrollView. I would like to alternate the background and text colors for each row as the ScrollView is completed. I don’t see a way of doing this, but was hopeful that a fellow forum member might know of a way. I have received some very knowledgeable feedback in the past, and thought it was worth a try.

Regards :)

B4X:
For i = 1 To messageCount
      Dim chk As CheckBox
      chk.Initialize("chkBoxSelection")
      Subject = arraySubject(aIndex) 'get the item from the array
      chk.Text = Subject
      chk.textSize = FontSize
      chk.TextColor = FontColor
      chk.tag = i
      lstChecks.Add(chk)
      Dim lbl1 As Label
      lbl1.Initialize("")
      From = arrayFrom(aIndex) 'get the item from the array
      lbl1.Text = From
      lbl1.Gravity = Gravity.CENTER_VERTICAL
      lbl1.textSize = FontSize
      lbl1.TextColor = FontColor
      Panel1.AddView(chk, 0, Height * (i - 1), 49%x, Height)
      Panel1.AddView(lbl1, 50%x, Height * (i - 1), 49%x, Height)
      aIndex = aIndex + 1
   Next
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
In your loop you could use something like this:
B4X:
If i Mod 2 = 0 Then
    chk.TextColor = FontColor1
    lbl1.TextColor = FontColor1
Else
    chk.TextColor = FontColor2
    lbl1.TextColor = FontColor2
End If
i Mod 2 = 0 if i is even
i Mod 2 = 1 if i is odd

You could also add lines for the background colors.

Best regards.
 
Upvote 0

William Hunter

Active Member
Licensed User
Longtime User
In your loop you could use something like this:
B4X:
If i Mod 2 = 0 Then
    chk.TextColor = FontColor1
    lbl1.TextColor = FontColor1
Else
    chk.TextColor = FontColor2
    lbl1.TextColor = FontColor2
End If
i Mod 2 = 0 if i is even
i Mod 2 = 1 if i is odd

You could also add lines for the background colors.

Best regards.

Thank you Klaus. I had no idea that Mod could be used as an operator for this purpose. I learn something new every time I ask a question. It’s the sharing of knowledge on this forum that makes B4A an even better product.

Regards :icon_clap:
 
Upvote 0
Top