Android Question right/left alignment with ListView.AddTwoLines

Didier9

Well-Known Member
Licensed User
Longtime User
When adding a TwoLine item to a ListView, is there a way to have the top line left justified and the second line right-justified?
I want to have text on the first line and a number on the second line and it would look better that way.
TIA
 

toby

Well-Known Member
Licensed User
Longtime User
Below is the code using XCustomListView which is recommended
B4X:
    Dim csb As CSBuilder
    csb.Initialize.Alignment("ALIGN_NORMAL").Append("First line").Append(CRLF).Pop
    csb.Alignment("ALIGN_OPPOSITE").Append("123").PopAll
    clv1.AddTextItem(csb, csb)
 
Upvote 0

toby

Well-Known Member
Licensed User
Longtime User
If you still want to use ListView,


B4X:
    Dim csb1, csb2 As CSBuilder
    csb1.Initialize.Alignment("ALIGN_NORMAL").Append("First line").PopAll
    csb2.initialize.Alignment("ALIGN_OPPOSITE").Append("123").PopAll
    lv.AddTwoLines(csb1, csb2)
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
It also works with Listview. But it is recommended to use xclv instead.

B4X:
    ListView1.TwoLinesLayout.Label.Gravity = Gravity.LEFT
    ListView1.TwoLinesLayout.Label.TextColor = Colors.Black
    ListView1.TwoLinesLayout.SecondLabel.Gravity = Gravity.RIGHT
    ListView1.TwoLinesLayout.SecondLabel.TextColor = Colors.Red
   
    ListView1.AddTwoLines("Bla","1")
    ListView1.AddTwoLines("Blub","2")
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
I guess that's why the CustomListView exists
Not really.
This is not to entice you to use xClv, because everyone has their own reason for using what they want to use, but you can see that the syntax can be simpler for xClv than Listview
B4X:
lbl1.Text=$"Didier9 ${x} From Florida, America"$  'The 2 labels: lbl1, lb2 are declared as B4Xviews
        lbl2.SetTextAlignment("CENTER", "RIGHT")
        lbl2.Text=$"${Power(x,3)}"$
1697386940129.png
 
Upvote 0

Didier9

Well-Known Member
Licensed User
Longtime User
Not really.
This is not to entice you to use xClv, because everyone has their own reason for using what they want to use, but you can see that the syntax can be simpler for xClv than Listview
B4X:
lbl1.Text=$"Didier9 ${x} From Florida, America"$  'The 2 labels: lbl1, lb2 are declared as B4Xviews
        lbl2.SetTextAlignment("CENTER", "RIGHT")
        lbl2.Text=$"${Power(x,3)}"$
View attachment 146892
Thank you, yes this particular operation is simpler, but I am not familiar with the xClv and my app at the moment uses the regular ListView so I would have to do more research how to use it and possibly more changes to the existing code, which would certainly be a worthwhile endeavor for the long term but it's getting in the way of what I am trying to do right now :)
The app I am working on is 4-5 years old and I use it every day (expense tracker), I am just trying to add a bit of functionality.
 
Upvote 0
Top