ListView text alignment

adp

Member
Licensed User
Longtime User
The following code works fine:
----------------------------------------------------------------------
Cursor = SQL1.ExecQuery("SELECT * FROM register order by points desc, name")

For i = 0 To Cursor.RowCount - 1

Cursor.Position = i

ListView1.AddSingleLine(Cursor.getString("name") & " " &
Cursor.GetString("points"))

Next
Cursor.Close
----------------------------------------------------------------------
Now, all I need is to align the second column to the extreme right and the first column staying on the left side of the ListView. Any ideas?
 

barx

Well-Known Member
Licensed User
Longtime User
I did something similar in VB6 a few years ago. I had to use a font that used the same amount of space no matter what the letter. i.e. most fonts use less width for letters like 'i' so makes calculating the length very difficult.

Basically, in VB I found out how many characters the element could hold, find out how many characters each of the two items in each column used and then fill in the middle with spaces " ". Worked well for that instance but will be very different on android as the listview could show different amount of characters dipending on device loaded on.

I would either, as Erel says, use 2 listviews and link their positions in code, Or use a scrollview with some labels.
 
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
You can use the following font to achieve the same characters width for all characters:
B4X:
Typeface.MONOSPACE


That's what they call them, Mono Space. Kind of in the name, but it had escaped me.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
The code below generates a two column ListView, setting the positions and dimensions of the two labels.
B4X:
Sub Globals
    Dim lsvTest As ListView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    lsvTest.Initialize("lsvTest")
    Activity.AddView(lsvTest, 0, 0, 100%x, 100%y)
    lsvTest.TwoLinesLayout.ItemHeight = 40dip
    lsvTest.TwoLinesLayout.Label.Top = 0
    lsvTest.TwoLinesLayout.Label.Left = 0
    lsvTest.TwoLinesLayout.Label.Width = 50%x
    lsvTest.TwoLinesLayout.Label.Height = 40dip
    lsvTest.TwoLinesLayout.Label.Gravity = Gravity.CENTER_VERTICAL
    lsvTest.TwoLinesLayout.Label.Color = Colors.Red
    lsvTest.TwoLinesLayout.Label.TextSize = 18
    
    lsvTest.TwoLinesLayout.SecondLabel.Top = 0
    lsvTest.TwoLinesLayout.SecondLabel.Left = 50%x
    lsvTest.TwoLinesLayout.SecondLabel.Width = 50%x
    lsvTest.TwoLinesLayout.SecondLabel.Height = 40dip
    lsvTest.TwoLinesLayout.SecondLabel.Gravity = Gravity.CENTER_VERTICAL
    lsvTest.TwoLinesLayout.SecondLabel.Color = Colors.Blue
    lsvTest.TwoLinesLayout.SecondLabel.TextColor = Colors.Yellow
    lsvTest.TwoLinesLayout.SecondLabel.TextSize = 18

    For i = 0 To 100
        lsvTest.AddTwoLines("Test1" & i, "Test2" & i)
    Next
End Sub
Best regards.
 

Attachments

  • ListViewTwoColumns.jpg
    74.5 KB · Views: 548
Upvote 0

adp

Member
Licensed User
Longtime User
Thank you all for your prompt replies. I am new to this forum and, no doubt in my mind, it looks very professional.

Both solutions, Typeface.MONOSPACE and Klaus’s works very well for me.

Another question regarding the same code: How can I limit the number of lines to be displayed in the ListView?

I am using the following code:

Cursor = SQL1.ExecQuery("SELECT * FROM register order by points Asc, name")

For i = 0 To Cursor.RowCount - 1

Cursor.Position = i

ListView1.AddTwoLines(" " & Cursor.getString("name"), " " & Cursor.GetString("points"))
Next
Cursor.Close
 
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
Depending on your requirements, you could add a 'LIMIT' parameter to your sql statement
 
Upvote 0

adp

Member
Licensed User
Longtime User
Sorry for my lack of knowledge, how do you add a 'LIMIT' parameter to your sql statement?
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…