Android Question xCustomListView Text center align

Jeanc161

Member
Licensed User
Longtime User
Hi,
I'm using the xCustomlistview in B4A vers 9.5, actually i'm using the xCustomListView1.addtextItem(MyText,myValue)

I want to be able to define the horizontal alignment of each cell as i put it in the listview.
I find no place toe set the horizontal, vertical gravity for each cell as it is inserted in the list

Is there a simple way to tell the newly added textitem to center it's content or leave it as default (left align) wich i don't need to setup since it is always set to left.

I use the csBuilder and it works fine, but when i insert text that is bigger then the cell, the cell itself does not adjust it's size, only if i use the addTextItem with the .asview.width=-2 and asview.height=-2

But if i put the cs string, i loose the auto size adjust, so if i could use the addtextitem and center it's content on the new cell just created, that will be fine.

So anybody out there who knows how to set the horizontal alignment will be greatly appreciated

Here is a small sample of code

B4X:
' csBuilder string  
          cs.Initialize.Size(20).Typeface(Typeface.CreateNew(Typeface.FONTAWESOME,Typeface.STYLE_NORMAL)).Color(convertGreenColor(oPos.Color)).Alignment("ALIGN_CENTER").Append(oPos.Texte.Replace("2014","2020")).popall

    ' standard text no cs here
                TableView1.DefaultTextColor = convertGreenColor(oPos.Color)
                TableView1.DefaultTextBackgroundColor = Colors.White
                TableView1.AsView.Width=-2
                TableView1.AsView.Height=-2
                TableView1.AddTextItem(oPos.Texte, oPos.Link)
' if i use the csBuilder string is
               Tableview1.addTextItem(cs,oPos.link)
' instead of oPos.Texte

Any idea anyone
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
I want to be able to define the horizontal alignment of each cell as i put it in the listview.
I find no place toe set the horizontal, vertical gravity for each cell as it is inserted in the list
You can set it in the designer:
B4A_anYTjDhyig.png
 
Upvote 0

Jeanc161

Member
Licensed User
Longtime User
Yes that i know, but i want to change the alignment dynamicly when i load some items in the clv
if i set it in the designer, all row will have the same alignment thrue all the clv.
I need to have some centered and other left align, that are defined when i load the items from a list.

So is there a method or property for each row or we have to make it globally set for all rows ???????
 
Upvote 0

Jeanc161

Member
Licensed User
Longtime User
There has seem no solution so i created my Own

I used the xCustomListView source code wich i renamed xxCustomListView and change a few things in the addTextItem
I added a few parameters to setup the text alignment, and correct a few things as i added a DefaultFontSize for the label and change the lbl as B4xView to lbl as Label in the defenition and do some text in B4J and B4i and added the compiler directive for each version in the code

The idea of using a layout that to my sense is not practical to display only text in the listview, so it add complexity as to set it's size and other property in the CLV
so here is a sample code in the xCustomListview Class.

B4X:
' In the Class Global added these and modify some
    Public DefaultTextColor As Int = xui.Color_Black
    Public DefaultTextBackgroundColor As Int = xui.Color_White
    Public DefaultFontSize As Int = 14
    Private itemList As List

' in The Class Create initialise that list
Public Sub Initialize (vCallBack As Object, vEventName As String)
    itemList.initialize
    ' and some other stuff before or after the initialize
End Sub

Public Sub AddTextItem(Text As Object, Value As Object, Alignment As Object,Autosize As Boolean)
    InsertAtTextItem(items.Size, Text, Value, Alignment,Autosize)
End Sub

' Inserts a text item at the specified index.
' NOTE added alignment in the format "VERTICAL,HORIZONTAL"
Public Sub InsertAtTextItem(Index As Int, Text As Object, Value As Object, Alignment As Object,Autosize As Boolean)
    Dim align() As String
    Dim lblGravity(2) As Int
        If Alignment = "" Or Alignment = Null Then
            lblGravity(0) = Gravity.NO_GRAVITY   ' that can be changed to CENTER_VERTICAL
            lblGravity(1) = Gravity.NO_GRAVITY   ' that can be changed to CENTER_HORIZONTAL
       Else
               align = Regex.Split(",",Alignment)
       End If
       ' this is the default value if set as "Default,Default"
       If align(0).ToLowerCase = "default" Then lblGravity(0) = Gravity.CENTER_VERTICAL
       If align(1).ToLowerCase = "default" Then lblGravity(1) = Gravity.LEFT
 
       For i = 0 To 1
         Select Case align(i)
            Case "TOP" : lblGravity(i) = Gravity.TOP : Exit
            Case "LEFT" : lblGravity(i) = Gravity.LEFT    : Exit
            Case "RIGHT" : lblGravity(i) = Gravity.RIGHT : Exit
            Case "CENTER" : lblGravity(i) = Gravity.CENTER : Exit
            Case "CENTER_HORIZONTAL" : lblGravity(i) = Gravity.CENTER_HORIZONTAL : Exit
            Case "CENTER_VERTICAL" : lblGravity(i) = Gravity.CENTER_VERTICAL : Exit
            Case "BOTTOM" : lblGravity(i) = Gravity.BOTTOM : Exit
            Case "FILL" : lblGravity(i) = Gravity.FILL : Exit          
         End Select
       Next
    If horizontal Then
        Log("AddTextItem is only supported in vertical orientation.")
        Return
    End If
    Dim pnl As B4XView = CreatePanel("")
    #if B4A
    Dim lbl As Label = CreateLabel("")
    #else if B4I
       ' i'm not shure what to put here
    #else if B4J
       ' neither here
    #end if
    lbl.Height = Max(50dip, lbl.Height)
    lbl.Width = sv.ScrollViewContentWidth -10dip
    lbl.TextSize = DefaultFontSize
    lbl.Text = Text
    'If Autosize Then    ' can only be changed after it insertion in panel
    '    lbl.Height=-2    ' and is currently not used
'        lbl.Width=-2     ' cause this only works with B4XView i think
'    End If
    #if B4J
      lbl.SetTextAlignment(lblGravity(0),lblGravity(1))
    #else if B4A
      lbl.Gravity = Bit.Or(lblGravity(1), lblGravity(0))
    #end if
    ' set the text gravity based on Alignment string
    ' 0 is vertical   1 is Horizontal for lblGravity()
    pnl.AddView(lbl, 5dip, 2dip, sv.ScrollViewContentWidth - 10dip, lbl.Height)
    If xui.IsB4i = False Then
        lbl.TextColor = DefaultTextColor
    End If
    itemList.Add(lbl)
    ' Modified to add alignment of label just created that is inserted in panel
 
    pnl.Color = DefaultTextBackgroundColor
    pnl.Height = lbl.Height + 2dip
    InsertAt(Index, pnl, Value)
    Dim item As CLVItem = GetRawListItem(Index)
    item.TextItem = True
End Sub

' Only used with addTextItem only
' this is used to retreive a specific item in the list by it's index
' and change values directly to the selected label in the list

Public Sub getItemFromList(Index As Int) As Label
    ' extract the label value from the list
    If Index > itemList.Size-1 Then
        Return itemList.Get((itemList.Size-1))
    Else
        Return itemList.Get(Index)
    End If
End Sub


' Here are some code to setup the values of each textItem inside the Main activity
' the cs setup was use before so i let it there if needed.
        If oPos.nodeType="F" Then      ' This for me is a title no link centered
            cs.Initialize.Size(20).Typeface(Typeface.CreateNew(Typeface.FONTAWESOME,Typeface.STYLE_NORMAL)).Color(convertGreenColor(oPos.Color)).Alignment("ALIGN_CENTER").Append(oPos.Texte.Replace("2014","2020")).popall
                TableView1.DefaultTextColor = convertGreenColor(oPos.Color)
                TableView1.DefaultTextBackgroundColor = Colors.White
                TableView1.DefaultFontSize = 20   ' this is new
                TableView1.AsView.Width=-2
                TableView1.AsView.Height=-2
                TableView1.AddTextItem(oPos.Texte, oPos.Link,"CENTER_VERTICAL,CENTER_HORIZONTAL",True)
            else if oPos.nodeType = "D" Then      ' this for me is a link document
            cs.Initialize.Size(18).Typeface(Typeface.CreateNew(Typeface.FONTAWESOME,Typeface.STYLE_NORMAL)).Color(convertGreenColor(oPos.Color)).Alignment("ALIGN_LEFT").Append(" " & Chr(0xF03C) & " " & oPos.Texte).popall
                TableView1.DefaultTextColor = convertGreenColor(oPos.Color)
                TableView1.DefaultTextBackgroundColor = Colors.White
                TableView1.DefaultFontSize = 20
                TableView1.AsView.Width=-2
                TableView1.AsView.Height=-2
                TableView1.AddTextItem(" " & Chr(0xF03C) & " " & oPos.Texte, oPos.Link,"TOP,LEFT",True)
        End If

' NOTE the new itemList as list that keep track of each individual item in the list defined in the Class Global

' also modify the clear method to clear all the items from the list
'Clears all items.
Public Sub Clear
    items.Clear
    itemList.Clear     ' this clear the items in list but do not reinitialize the list
    sv.ScrollViewInnerPanel.RemoveAllViews
    SetScrollViewContentSize(0)
    ResetVisibles
End Sub

I use the example that can be found in the Forum to run some test on the new class

I did use the xCustomListView that come with the B4A Version but that library was not doing what i wanted it to do, so next best thing is to use the source of the xCustomListView and modify it.

I change the name to differentiate it from the internal library so that there will be no conflict between the two class.

The Class added to the project did not slow down the listview from scrolling, a little bit in debug mode but in release mode it is as efficient as the internal library.

I included the sample for testing the xxCustomListView and the xxCustomListView Class source code so if you want to use it and modify it, Have fun

If anyone who have a possibility to convert this to a Library not using the Library maker built in the B4A cause it did not work properly when compiling as a library, i'm not shure why but if someone else can do that and make it work, please advise me, and i will take a look at that.

Thanks all and hopefully someone will used this modify class.
 

Attachments

  • xCustomListView.zip
    187.2 KB · Views: 343
Last edited:
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
So is there a method or property for each row or we have to make it globally set for all rows ???????
It is very simple to do.

Just set clv.DesignerLabel.Gravity as needed before you add the items.
You can also add an item with CLV.AddTextItem and then modify it.

There has seem no solution so i created my Own
This is the wrong solution.
 
Last edited:
Upvote 0

Jeanc161

Member
Licensed User
Longtime User
Is the clv.DesignerLabel.gravity is available when adding TextItem when loading many items ???

Should i do something like

myClv.DesignerLabel.Gravity = Bit.Or(Gravity.CENTER_VERTICAL, Gravity.Center)
and the gravity of the label when addind clv.addTextItem(MyText,myValue) will change before the text is added and the final result will be that the label will have the center_vertical and Center horizontally ???

And secondly how can you addTextItem(myText,myValue) and the modify it, there is no public access for the newly added label that we can use to modify it properties as textsize, gravity, and so on cause i think the label is a B4XView and there is no such properties on a B4xView object.
I even try clv.asview.setTextAlignment("TOP","CENTER" and it return an error when trying do to such a thing,
So i'm king of confuse on how to do this, refer to the code section when i add items to the clv in the first post.
Can you lightup my brain on this ??
 
Upvote 0

Jeanc161

Member
Licensed User
Longtime User
Another thing...
When using the CustomListView Class as source code, i have found that the reachend event is fired multiple time after a couple of calls, the first and second reachend works fine and after that it call the reachend many time on a simple call when it reach the end of list again.
So do you think that there is a bug in the source code that has been corrected in the making of the library, cause when i use the build in library, this work just fine, but when i use the source class CustomListview as a class, this event fire on wrong values.

Is anyone have the same problem when using the source class library in any project and that you use the reachend event for any purpose and have the same kind of problem ?????
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Should i do something like

myClv.DesignerLabel.Gravity = Bit.Or(Gravity.CENTER_VERTICAL, Gravity.Center)
and the gravity of the label when addind clv.addTextItem(MyText,myValue) will change before the text is added and the final result will be that the label will have the center_vertical and Center horizontally ???
Yes.

Please start a new thread for any other question you have.
 
Upvote 0
Top