B4J Question Change background-color by program code

HinkePank

New Member
Hello,

I am very new to B4X, so this question might look stupid for most of you, but I stumble and do not get on with it.

First : I am developing on my PC, so I am using B4J

What I am aiming to is a little app ( later running on an android device ) which is monitoring some things and raises an alert
via sound ( that was easy ) and also visual by flashing ( changing colors ) of the app-background or even the background of
a label on the screen.
I found something "Root.Color = 123" which did not work for me. I also tried "Root.SetColorAnimated(iColor1,iColor2,1000)"
also without any visible effect.

I am using the basic Hello World program and just added a label to show that I really entered the change-routine.

I am feeling a little bit silly. It is surely something absolutely easy what I am missing.
I would like to get a gente kick to the right direction 😉.

Did I post my code the correct way ?


#Region Shared Files
#CustomBuildAction: folders ready, %WINDIR%\System32\Robocopy.exe,"..\..\Shared Files" "..\Files"
'Ctrl + click to sync files: ide://run?file=%WINDIR%\System32\Robocopy.exe&args=..\..\Shared+Files&args=..\Files&FilesSync=True
#End Region

'Ctrl + click to export as zip: ide://run?File=%B4X%\Zipper.jar&Args=Project.zip

Sub Class_Globals
Private Root As B4XView
Private xui As XUI
Private Label1 As Label

Private Bclose As Button
Private Bexit As Button
Private Btest As Button

Private iNowColor, iColor1, iColor2 As Int
End Sub

Public Sub Initialize
' B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
Root = Root1
Root.LoadLayout("MainPage")
iNowColor= Root.Color
iColor1= iNowColor
iColor2=255

End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Private Sub Bclose_Click
xui.MsgboxAsync("Hello world!", "B4X")

End Sub

' Test
private Sub Btest_Click
'Dim mp As MediaPlayer

'mp.Initialize("mp", File.GetUri(File.DirAssets, "bubbles.mp3"))
'mp.Play


Label1.Text="Abfrage Türstatus ... " & iNowColor


If (iNowColor= iColor1) Then
'Root.Color= iColor2 '**** Did not work

Root.SetColorAnimated(iColor1,iColor2,1000)

iNowColor = iColor2
Else
'Root.Color = iColor1 '**** Did not work

Root.SetColorAnimated(iColor1,iColor2,1000)

iNowColor= iColor1
End If




End Sub

private Sub Bexit_Click

ExitApplication

End Sub
 

zed

Well-Known Member
Licensed User
Using CSS in B4J (via the .Style property) allows for fine-grained control over the appearance of your interface elements, far beyond what classic properties like .Color or .TextSize offer.

B4J:
Sub Class_Globals
    Private Root As B4XView
    Private fx As JFX
    Private xui As XUI
    
    Private mForm As Form
End Sub

Public Sub Initialize
    ' B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    mForm = B4XPages.GetNativeParent(Me)

End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Private Sub Button1_Click
    mForm.RootPane.Style = "-fx-background-color: #007AFF;"
End Sub

Private Sub Button2_Click
    mForm.RootPane.Style = "-fx-background-color: #34eb8c;"
End Sub
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Did I post my code the correct way ?
No.
The best way to show a problem is to post a small project.
In your case your project.
In the IDE click on this line on top pf the B4XMainPage module, this will zip your project, then add the zip file in your post.
1760883391956.png


Or, if you want to post code click on the icon:
1760883373989.png


Then you get a window where you can copy your code which will be displayed like in the previous post.
This is explained in detail HERE.

The main problem in your code is this line:
iColor2 = 255
This is a transparent color, therefor nothing happens.
Set it, to any other color like:
iColor2 = xui.Color_Red
And you will see that it works also with:
Root.Color = iColor1

Then, these lines are wrong:
Root.SetColorAnimated(iColor1,iColor2,1000)
The time must be the first parameter:
Root.SetColorAnimated(1000, iColor1, iColor2)

And in these calls the two colors are wrong.

This works:
B4X:
' Test
private Sub Btest_Click
    Label1.Text="Abfrage Türstatus ... " & iNowColor
    If (iNowColor= iColor1) Then
'        Root.Color= iColor2 '**** Did not work
        Root.SetColorAnimated(1000, iNowColor,iColor2)
        iNowColor = iColor2
    Else
'        Root.Color = iColor1 '**** Did not work
        Root.SetColorAnimated(1000, iNowColor, iColor1)
        iNowColor= iColor1
    End If
End Sub
 
Last edited:
Upvote 0

HinkePank

New Member
Hi Klaus,

also thanks a lot for your detailed explanation and taking care for my (yes little stupid) try with the square brackets on the right side. 😳

Always a wonder : As soon as you do it right, it works !

I will never forget again.
 
Upvote 0
Top