Android Question Changing multiple label colors

RogerGCollin

Member
Licensed User
I am attempting to code for labels (consists of one label with an icon e.g. btn1 and one with text e.g. btn1b) where when one is pressed, the color will change while the other two labels will change to an alternate color. The code below works apart from the line starting with 'If v.name contains'... This is obviously not the correct syntax for B4X, however is there any way to get the name of a label view and determine if that name contains a certain sub string e.g. that v = btn1b and that this contains a 1. I know I could probably do this with tags but I want to use them for something else!
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private btn1, btn1b, btn2, btn2b, btn3, btn3b As Label
    Private pnl1, pnl2, pnl3, pnlBar As B4XView        
End Sub

Sub btn2_Click
    pnl2.BringToFront
    For Each v As Label In pnlBar.GetAllViewsRecursive
        If v.name contains("1") Then  v.textColor = Colors.Red Else v.TextColor = Colors.blue
    Next

Thanks all who reply!
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Views don't have names.

B4X:
Private labels As B4XSet 'globals

labels =B4XCollections.CreateSet2(Array(btn1, btn1b, btn2, btn2b, btn3, btn3b))

...
For Each x As B4XView In pnlBar.GetAllViewsRecursive
 If labels.Contains(x) Then x.TextColor = Colors.Red Else v.TextColor = Colors.Blue
Next

Depends on B4XCollections (already referenced if you are using B4XPages).
 
Upvote 0

RogerGCollin

Member
Licensed User
Thank you Erel, I do appreciate the time you take to answer for newbies like me. Your solution shown in btn2_click (which I slightly adjusted) is nice and elegant compared to my old code in btn1_click.
B4X:
Sub btn1_Click
    pnl1.BringToFront
    btn1.TextColor = 0xFF92BF2C:btn1b.textcolor = 0xFF92BF2C
    btn2.TextColor = 0xFFB4B4B4:btn2b.textcolor = 0xFFB4B4B4
    btn3.TextColor = 0xFFB4B4B4:btn3b.textcolor = 0xFFB4B4B4
End Sub
Sub btn2_Click
    pnl2.BringToFront
    labels =B4XCollections.CreateSet2(Array(btn2, btn2b))
    For Each x As B4XView In pnlBar.GetAllViewsRecursive
        If labels.Contains(x) Then x.TextColor = 0xFF92BF2C Else x.TextColor = 0xFFB4B4B4
    Next
End Sub
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
if you already use an array of buttons why do you get them back with "Getallviewsrecursive"?
you can use the array.

i would do it like this:


B4X:
Sub Process_Globals
    Private xui As XUI
End Sub

Sub Globals
    Private btn(4) As Button
End Sub

Sub Activity_Create(FirstTime As Boolean) 'ignore
    For i = 0 To btn.Length-1
        btn(i).Initialize("myBtn")
        btn(i).Text = $"btn${i}"$
        Activity.AddView(btn(i),50dip,i*75dip,100dip,50dip)
    Next
End Sub

Sub myBtn_Click
    Dim btnSender As Button = Sender 
    For Each b As Button In btn
        If b = btnSender Then
            btnSender.Color = xui.Color_Blue
            btnSender.TextColor = xui.Color_White            
        Else
            b.Color = xui.Color_Green
            b.TextColor = xui.Color_Red
        End If
    Next
End Sub

as you can see i give all buttons the same event name and then with the sender i set the selected button with the wanted color and go through the array and change the other buttons.
 
Upvote 0

RogerGCollin

Member
Licensed User
Hi Ilan. Thank you for your reply. I am just starting to learn basic so I may not have this right, but I think your code will not work for me as my labels are made in Designer and not added using an array when the activity is created. As I have specific text for each label some as an icon and some as text, I think adding label text with the Designer could be the better approach.
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
Hi Ilan. Thank you for your reply. I am just starting to learn basic so I may not have this right, but I think your code will not work for me as my labels are made in Designer and not added using an array when the activity is created. As I have specific text for each label some as an icon and some as text, I think adding label text with the Designer could be the better approach.

of course, it will work. in the designer you just need to set to all labels the same Eventname then you can use the Sender object the same way i did above. the only difference is that you dont add the views as i do in Activity_Create since you already add them in the designer.

changing the textcolor should be the same if you use default font or fontawesome. changing the text itself will be the same way as you do it now. in the designer.

the code should work with minor changes. upload a sample project and i can try to help you.
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
ok i have updated following parts. as all labels are already inside a panel (this is also what i would have done) you can add all labels easily with a simple loop:

B4X:
    myLblList.Initialize
    For Each v As View In  pnlBar.GetAllViewsRecursive
        myLblList.add(v) 'add all labels to the list
    Next

myLblList is a List and what i do i add all views (labels) in the pnlBar to the list when b4x page is created.

now all i need to do is just catch the clicked label with a sender object like this:

B4X:
Sub btn_Click
    Dim myLbl As Label = Sender
    Dim clickedID As String = myLbl.Tag
    For Each lbl As Label In myLblList
        If lbl.tag = clickedID Then lbl.TextColor = 0xFF92BF2C Else lbl.textcolor = 0xFFB4B4B4
    Next
End Sub

what i do i change all labels EVENTNAME in the pnlBar to "bnt" like this each label that is clicked will call this event.
now you have a set of 2 labels so you want always to change 2 labels if one is clicked to do that i give to the paired label the same TAG value.

so btn1 and btn1b has the same tag("0"), btn2 and btn2b has also the same tag ("1"), ...

in the event i just ask for the clicked tag and if in the list a label has the same tag i change the color to green else it will stay gray (or the color you picked i dont understand
0xFFB4B4B4. i use ints for colors :oops:

The project is attached. Good Luck with your Project :)
 

Attachments

  • B4A.zip
    5.1 KB · Views: 168
Upvote 0
Top