Android Code Snippet Creating xnGrid on the fly

I've been using B4A for a while for ad-hoc, prototype stuff and created a few small projects. I'm working on something new and needed to create grids, which I see B4A doesn't come with. I found xnGrid, and it took a while to get to where I had something usable. I just thought I'd pass on what I have in case anyone else finds it interesting or useful.

In Globals
B4X:
Private MyGrid as xnGrid 'The Grid I want to use
private MyPanel as Panel 'The Container I want to put it in. Already created on my form

In Activity_Create
B4X:
CreateGrid(MyPanel,MyGrid,5, 20, 305, 170,Array As String("ISBN","Price","Title"),Array As Int(120,80,300),Array As Int(Gravity.left,Gravity.right,Gravity.left))

The CreateGrid Function
B4X:
Sub CreateGrid(pParent As Panel,pGrid As xnGrid, pLeft As Int, pTop As Int, pWidth As Int, pHeight As Int,pTitles() As String, pWidths() As Int,pAlignments() As Int)
    Dim lGridName As String
    If pGrid = MyGrid Then
        lGridName="MyGrid" 'Not Sure of a Better Way to do this
    else if pGrid=grdSessions Then 'Assuming the rest were also grids in the application
        lGridName="grdSessions"
    else if pGrid=grdSessionDetails Then
        lGridName="grdSessionDetails"
    End If
    If pGrid.IsInitialized =False Then
        pGrid.Initialize(lGridName)
        pGrid.RowHeight = 30dip
        pGrid.HeaderColor=Colors.gray
        pGrid.HeaderHeight = 30dip
        pGrid.HeaderTextSize = 16
        pGrid.RowTextSize = 14
        pGrid.HeaderTextColor = Colors.white
        pGrid.RowTextColor = Colors.black
        pGrid.RowEvenColor=Colors.white
        pGrid.RowOddColor=Colors.lightgray
        pGrid.SelectedOddColor = Colors.ARGB(255,222,222,255)
        pGrid.SelectedEvenColor = Colors.ARGB(255,222,222,255)
        pGrid.Color=Colors.white
        pGrid.Color=Colors.LightGray
        Dim cc(pTitles.Length) As xnGridCol
        For forcount=0 To pTitles.Length-1
            cc(forcount).Initialize2 ( pTitles(forcount) , pTitles(forcount) , IntToDIP(pWidths(forcount)) , pAlignments(forcount) )
            pGrid.ColAppend (cc(forcount))
        Next
        pGrid.GridWidth=1
        pGrid.pixelfix=False
        pGrid.ScrollWidth=20dip
        pGrid.ScrollColor=Colors.black
        pParent.AddView(pGrid, IntToDIP(pLeft),IntToDIP(pTop),IntToDIP(pWidth),IntToDIP(pHeight))
        pGrid.Visible=True
        pGrid.GridCreate2(True)
    End If
End Sub

This also makes use of the IntToDip function, by JonPM found here

Comments gratefully received.

Colin
 

colboy

Member
Licensed User
Longtime User
Here's a sample. There's a big gap at the top, that's where my clients log goes, but it's .hidden.
Sample.png
 

LUISTORRES

Member
Licensed User
I've been using B4A for a while for ad-hoc, prototype stuff and created a few small projects. I'm working on something new and needed to create grids, which I see B4A doesn't come with. I found xnGrid, and it took a while to get to where I had something usable. I just thought I'd pass on what I have in case anyone else finds it interesting or useful.

In Globals
B4X:
Private MyGrid as xnGrid 'The Grid I want to use
private MyPanel as Panel 'The Container I want to put it in. Already created on my form

In Activity_Create
B4X:
CreateGrid(MyPanel,MyGrid,5, 20, 305, 170,Array As String("ISBN","Price","Title"),Array As Int(120,80,300),Array As Int(Gravity.left,Gravity.right,Gravity.left))

The CreateGrid Function
B4X:
Sub CreateGrid(pParent As Panel,pGrid As xnGrid, pLeft As Int, pTop As Int, pWidth As Int, pHeight As Int,pTitles() As String, pWidths() As Int,pAlignments() As Int)
    Dim lGridName As String
    If pGrid = MyGrid Then
        lGridName="MyGrid" 'Not Sure of a Better Way to do this
    else if pGrid=grdSessions Then 'Assuming the rest were also grids in the application
        lGridName="grdSessions"
    else if pGrid=grdSessionDetails Then
        lGridName="grdSessionDetails"
    End If
    If pGrid.IsInitialized =False Then
        pGrid.Initialize(lGridName)
        pGrid.RowHeight = 30dip
        pGrid.HeaderColor=Colors.gray
        pGrid.HeaderHeight = 30dip
        pGrid.HeaderTextSize = 16
        pGrid.RowTextSize = 14
        pGrid.HeaderTextColor = Colors.white
        pGrid.RowTextColor = Colors.black
        pGrid.RowEvenColor=Colors.white
        pGrid.RowOddColor=Colors.lightgray
        pGrid.SelectedOddColor = Colors.ARGB(255,222,222,255)
        pGrid.SelectedEvenColor = Colors.ARGB(255,222,222,255)
        pGrid.Color=Colors.white
        pGrid.Color=Colors.LightGray
        Dim cc(pTitles.Length) As xnGridCol
        For forcount=0 To pTitles.Length-1
            cc(forcount).Initialize2 ( pTitles(forcount) , pTitles(forcount) , IntToDIP(pWidths(forcount)) , pAlignments(forcount) )
            pGrid.ColAppend (cc(forcount))
        Next
        pGrid.GridWidth=1
        pGrid.pixelfix=False
        pGrid.ScrollWidth=20dip
        pGrid.ScrollColor=Colors.black
        pParent.AddView(pGrid, IntToDIP(pLeft),IntToDIP(pTop),IntToDIP(pWidth),IntToDIP(pHeight))
        pGrid.Visible=True
        pGrid.GridCreate2(True)
    End If
End Sub

This also makes use of the IntToDip function, by JonPM found here

Comments gratefully received.

Colin
hi ,it looks greate, could you tell me how can i use xngrid library? where can i download it the files ? thanks i hope you help
 
Top