B4J Question treeView parent background color

atiaust

Active Member
Licensed User
Longtime User
Hi All,

Is it possible to change the background color of each parent line in a treeView?

Thanks
 

atiaust

Active Member
Licensed User
Longtime User
Hi Erel,

Parent (change this line color)
Child
Grandchild
Parent (change line color) etc
Child
Parent (change line color)

Change the Parent line color?

Possible?

Thanks
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I don't see any line.

SS-2017-04-07_09.02.54.png
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
Yes, using CSS!
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
SS-2017-04-09_09.39.53.png


Code to change the text color of specific items:
B4X:
Sub AppStart (Form1 As Form, Args() As String)
   MainForm = Form1
   MainForm.RootPane.LoadLayout("1")
   For i = 1 To 10
     Dim parent As TreeItem = CreateTreeItem("Parent " & i, fx.Colors.Red, fx.DefaultFont(20))
     Dim child As TreeItem = CreateTreeItem("Child", fx.Colors.Blue, fx.DefaultFont(14))
     parent.Children.Add(child)
     TreeView1.Root.Children.Add(parent)
   Next
   MainForm.Show
End Sub

Sub CreateTreeItem(Text As String, Color As Paint, Font As Font) As TreeItem
   Dim ti As TreeItem
   ti.Initialize("", "")
   Dim tf As TextFlow
   tf.Initialize
   tf.AddText(Text)
   tf.SetColor(Color)
   tf.SetFont(Font)
   Dim jo As JavaObject = ti
   Dim p As Pane = tf.CreateTextFlow
   jo.RunMethod("setGraphic", Array(p))
   Return ti
End Sub
TextFlow: https://www.b4x.com/android/forum/t...-similar-to-b4a-b4i-richstring.61237/#content

I'm not sure that it is possible to change the background color of specific items.
 
Upvote 0

atiaust

Active Member
Licensed User
Longtime User
Hi Erel,

I have just discovered that using the code above creates the treeItems OK but if you use treeView1_SelectedItemChanged and try to get the text for the item, selectedItem.text returns "" (empty string).

Can you suggest how to get around this?

Have attached an example app.

Thanks
 

Attachments

  • B4J-TreeTest.zip
    2.4 KB · Views: 302
Upvote 0

atiaust

Active Member
Licensed User
Longtime User
Thanks.

Fixed by adding p.Tag = Text to the CreateTreeItem sub

And in the treeView1_SelectedItemChanged added Dim jo etc..

B4X:
Sub CreateTreeItem(Text As String, Color As Paint, Font As Font) As TreeItem
    Dim ti As TreeItem
    ti.Initialize("", "")
    Dim tf As TextFlow
    tf.Initialize
    tf.AddText(Text)
    tf.SetColor(Color)
    tf.SetFont(Font)
    Dim jo As JavaObject = ti
    Dim p As Pane = tf.CreateTextFlow
    p.Tag = Text
    jo.RunMethod("setGraphic", Array(p))
    Return ti
End Sub

Sub TreeView1_SelectedItemChanged(SelectedItem As TreeItem)
    Log("SelectedItem text = "&SelectedItem.Text)
    Dim jo As JavaObject = SelectedItem
    Dim p As Pane = jo.RunMethod("getGraphic", Null)
    Log(p.Tag)
End Sub
 
Upvote 0
Top