B4J Question Changing Background Color of a Label

Discussion in 'B4J Questions' started by embedded, Jul 20, 2018.

  1. embedded

    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.
     
  2. stevel05

    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:

    Code:
    CSSUtils.SetBackgroundColor(Label2_time,fx.Colors.From32Bit(0xFF18F900))
     
    Erel, DonManfred and embedded like this.
  3. embedded

    embedded Member Licensed User

    Thanks....Its working....
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    Another option is to use jXUI.

    Code:
    Dim x As B4XView = YourLabel
    x.Color = 
    0xFF18F900
     
    embedded, MarkusR and stevel05 like this.
  5. XorAndOr

    XorAndOr Active Member Licensed User

    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
    Code:
    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
     
  6. AnandGupta

    AnandGupta Active 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
     
    XorAndOr likes this.
  7. Daestrum

    Daestrum Well-Known Member Licensed User

    Uses JavaObject library

    Code:
    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
     
    jimmyF, XorAndOr and stevel05 like this.
  8. XorAndOr

    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"
     
  9. Daestrum

    Daestrum Well-Known Member Licensed User

    Can you show where you added the code ?
     
  10. XorAndOr

    XorAndOr Active Member Licensed User

    here

    Code:
    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
     
  11. Daestrum

    Daestrum Well-Known Member Licensed User

    I wonder if there is an extra character in b, a space maybe
    what does
    Code:
    log(b.length)
    return, it should be 10
     
    XorAndOr likes this.
  12. XorAndOr

    XorAndOr Active Member Licensed User


    log(b.length) ---> 11
     
  13. XorAndOr

    XorAndOr Active Member Licensed User

    thanks Daestrum, I try to understand why it is 11
     
  14. Daestrum

    Daestrum Well-Known Member Licensed User

    just change

    Code:
    b = a.Get(0)
    to
    Code:
    b = a.Get(0)
    b = b.trim
    or change
    Code:
    dummylong = jo.RunMethod("parseLong",Array(b.Replace("0x",""),16))
    to
    Code:
    dummylong = jo.RunMethod("parseLong",Array(b.Replace("0x","").Replace(" ",""),16))
     
    Last edited: Oct 1, 2018
    XorAndOr likes this.
  15. XorAndOr

    XorAndOr Active Member Licensed User

    b = b.trim

    yes now works

    thanks :)
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice