Android Question What is best way to handle custom colors across many layouts?

Steve Miller

Active Member
Licensed User
Longtime User
I have a tabbed screen with 11 layouts. I want to give the user the ability to pick their own colors for background, label text, and tabs. This will allow them to match to their company colors.

What I did was write a program for the computer that updates a database with the selected colors of each item. The tablet has the same database on it and gets a copy of this updated color table. Now, I want to apply the customers colors to each item across all the screens. What is the most efficient method of doing this?
 

LucaMs

Expert
Licensed User
Longtime User
If I have not been too distracted, it should be like this:
B4X:
Sub SetCustomColors

    If cColors.IsInitialized=False Then
        cColors.Initialize
    End If

    Dim r As Reflector
    r.Target = TabHost1
    r.Target = r.RunMethod("getTabWidget")
    For t = 0 To TabHost1.TabCount - 1
        Dim tabParentPanel As Panel
        tabParentPanel = r.RunMethod2("getChildAt", t, "java.lang.int")
        For x = 0 To tabParentPanel.NumberOfViews - 1
            Log("Tab #" & t & " View #" & x)
            SetViewColors(tabParentPanel.GetView(x))
        Next
    Next

End Sub

In fact, I was distracted, I put the code in another thread
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
Ok, I implemented the below code and put some logging in the setting of the color for the label view. The looping takes about 1 minute to go through a tabhost with 12 tabs on it, updating the foreground and background colors of the views in each tab.

What I am seeing is a repeat pattern as follows:

Tab #0 View #0
Tab #1 View #0
Tab #1 View #1
Tab #2 View #0
Tab #2 View #1
Tab #2 View #2
Tab #3 View #0
Tab #3 View #1
Tab #3 View #2
Tab #3 View #3

Here is the output in the log file for just 3 tabs (it repeats this pattern for all 12 tabs):
B4X:
Tab #0 View #0
Text=Welcome screen text here.
Text=Copyright ©2014 xxxx
Text=Welcome screen text here.
Text=Copyright ©2014 xxxx
Tab #1 View #0
Text=Welcome screen text here.
Text=Copyright ©2014 xxxx
Text=Welcome screen text here.
Text=Copyright ©2014 xxxx
Tab #1 View #1
Text=Text on tab 1
Text=More text on tab 1
Text=Text on tab 1
Text=More text on tab 1
Tab #2 View #0
Text=Welcome screen text here.
Text=Copyright ©2014 xxxx
Text=Welcome screen text here.
Text=Copyright ©2014 xxxx
Tab #2 View #1
Text=Text on tab 1
Text=More text on tab 1
Text=Text on tab 1
Text=More text on tab 1
Tab #2 View #2
Text=Text on tab 2
Text=More text on tab 2
Text=Text on tab 2
Text=More text on tab 2
Tab #3 View #0
Text=Welcome screen text here.
Text=Copyright ©2014 xxxx
Text=Welcome screen text here.
Text=Copyright ©2014 xxxx
Tab #3 View #1
Text=Text on tab 1
Text=More text on tab 1
Text=Text on tab 1
Text=More text on tab 1
Tab #3 View #2
Text=Text on tab 2
Text=More text on tab 2
Text=Text on tab 2
Text=More text on tab 2
Tab #3 View #3
Text=Text on tab 3
Text=More text on tab 3
Text=Text on tab 3
Text=More text on tab 3

Here is the code. Can anyone tell me what I'm doing wrong and how to correct this?

B4X:
Sub SetCustomColors
    Dim i As Int
    If cColors.IsInitialized=False Then
        cColors.Initialize
    End If
   
    For i = 0 To TabHost1.TabCount - 1
        TabHost1.CurrentTab=i
        Dim r As Reflector
        r.Target = TabHost1
        Dim tabParentPanel As Panel
        tabParentPanel = r.RunMethod("getTabContentView")
        For x = 0 To tabParentPanel.NumberOfViews - 1
            Log("Tab #" & i & " View #" & x)
            SetViewColors(tabParentPanel.GetView(x))
        Next       
    Next

End Sub
Sub SetViewColors(v As View)

    Select Case True
        Case v Is CheckBox
            Dim chk As CheckBox
            chk = v
            chk.Color=cColors.GetBG("CHECKBOX")
            chk.TextColor=cColors.GetFG("CHECKBOX")
        Case v Is EditText
            Dim txt As EditText
            txt = v
            txt.Color=cColors.GetBG("TEXTBOX")
            txt.TextColor=cColors.GetFG("TEXTBOX")
        Case v Is RadioButton
            Dim rad As RadioButton
            rad = v
            rad.Color=cColors.GetBG("RADIOBUTTON")
            rad.TextColor=cColors.GetFG("RADIOBUTTON")
        Case v Is Button
            Dim but As Button
            but = v
            but.Color=cColors.GetBG("BUTTON")
            but.TextColor=cColors.GetFG("BUTTON")
        Case v Is Spinner
            Dim spi As Spinner
            spi = v
            spi.Color=cColors.GetBG("LISTBOX")
            spi.TextColor=cColors.GetFG("LISTBOX")
        Case v Is Panel
            Dim pnl As Panel
            pnl = v
            pnl.Color=cColors.GetBG("BACKGROUND")
            For Each v1 As View In pnl.GetAllViewsRecursive
                SetViewColors(v1)
            Next
        Case v Is Label           
            Dim lab As Label
            lab = v
            Log("Text=" & lab.Text)
            If lab.Tag="CAPTION" Then
                lab.Color=cColors.GetBG("CAPTION")
                lab.TextColor=cColors.GetFG("CAPTION")
            Else
                lab.Color=cColors.GetBG("LABEL")
                lab.TextColor=cColors.GetFG("LABEL")
            End If
    End Select

I see nothing obviously wrong with your code but it's iterating over Labels twice?
Can you put a small sample project together and upload it and i'll try to debug it here.

Martin.
 
Upvote 0
Top