B4J Question Changing Background Color of a Label

embedded

Member
Licensed User
I am using Label2_time.Style="-fx-background-color: green" during running of my program...working ok. But the color is dull. I want to set color #FF18F900 this one.
Trying label2_time.Style="-fx-background-color:#FF18F900" but not working. Please help.
 

stevel05

Expert
Licensed User
You should use the CSSUtils lib to set the background color as it will not overwrite any other styles on the view.

Try this:

B4X:
CSSUtils.SetBackgroundColor(Label2_time,fx.Colors.From32Bit(0xFF18F900))
 

XorAndOr

Active Member
Licensed User
Another option is to use jXUI.

B4X:
Dim x As B4XView = YourLabel
x.Color = 0xFF18F900
Hi, sorry if I do not open another thread. I have this sub that reads the data of a sqlite database where I have the color codes. I would like to color a label by taking its color but I do not know where I'm wrong. thank you
B4X:
Sub ComboBox3_SelectedIndexChanged(Index As Int, Value As Object)
    Label3.Text = Index +1
    
    If Index > -1 Then

        dbCursor = dbSQL.ExecQuery2("SELECT ColoriCode FROM TbGeneri WHERE Colori LIKE ?", Array(Value))
        Do While dbCursor.NextRow
            Dim valori(dbCursor.ColumnCount) As String
            For col = 0 To dbCursor.ColumnCount - 1
                valori(col) = dbCursor.GetString2(col)
            Next
            
                        
            Dim a As List
            a.Initialize
            a.AddAll(valori)
            Dim b As String
            b = a.Get(0)
            
            Log(b)'  <---------------- 0xFF18F900
                                    
            
            Dim x As B4XView = Label3
            x.Color = 0xFF18F900       ' this works
            
            
            ' I need this code
            Dim x As B4XView = Label3
            x.Color = b                ' this don't works
            
            
        Loop
    End If
    dbCursor.Close
        
End Sub
 

AnandGupta

Well-Known Member
Licensed User
Using quick thinking of FoxPro/Clipper here, as '0xFF18F900' is literal but 'b' is variable, so we may need to do '(b)' or '&b' or something like that.

Will be happy if it helps.

Regards,

Anand
 

Daestrum

Well-Known Member
Licensed User
Uses JavaObject library

B4X:
 Dim c As String = "0xFF18F900"    ' your value from DB
 Dim dummylong As Long
 Dim jo As JavaObject = dummylong
 ' remove the 0x prefix and parse into a long value 
 dummylong = jo.RunMethod("parseLong",Array(c.Replace("0x",""),16))  
 Log(dummylong)    ' dummylong contains the value you can use
 

XorAndOr

Active Member
Licensed User
thanks for the answers.
Daestrum The above code works,
I have inserted it in my code, but taking data from my database in variable b gives me an error
Caused by: java.lang.NumberFormatException: For input string: "0xFF18F900"
 

XorAndOr

Active Member
Licensed User
Can you show where you added the code ?
here

B4X:
Sub ComboBox3_SelectedIndexChanged(Index As Int, Value As Object)
    Label3.Text = Index +1
    
    If Index > -1 Then

        dbCursor = dbSQL.ExecQuery2("SELECT ColoriCode FROM TbGeneri WHERE Colori LIKE ?", Array(Value))
        Do While dbCursor.NextRow
            Dim valori(dbCursor.ColumnCount) As String
            For col = 0 To dbCursor.ColumnCount - 1
                valori(col) = dbCursor.GetString2(col)
            Next
            
                        
            Dim a As List
            a.Initialize
            a.AddAll(valori)
            
            Dim b As String
            
            b = a.Get(0)
            
            Log(b)'  <---------------- 0xFF18F900           

            Dim dummylong As Long
            Dim jo As JavaObject = dummylong
            
            ' remove the 0x prefix and parse into a long value
            dummylong = jo.RunMethod("parseLong",Array(b.Replace("0x",""),16))
            Log(dummylong)
            
            
            Dim x As B4XView = Label3
            x.Color = dummylong
            
            
        Loop
    End If
    dbCursor.Close
        
End Sub
 

Daestrum

Well-Known Member
Licensed User
I wonder if there is an extra character in b, a space maybe
what does
B4X:
log(b.length)
return, it should be 10
 

Daestrum

Well-Known Member
Licensed User
just change

B4X:
b = a.Get(0)
to
B4X:
b = a.Get(0)
b = b.trim
or change
B4X:
 dummylong = jo.RunMethod("parseLong",Array(b.Replace("0x",""),16))
to
B4X:
 dummylong = jo.RunMethod("parseLong",Array(b.Replace("0x","").Replace(" ",""),16))
 
Last edited:
Top