Android Code Snippet Flexible Table Sort Icons

advansis

Active Member
Licensed User
Hi to everybody,
when I use Flexible Table (https://www.b4x.com/android/forum/threads/class-flexible-table.30649/), most times I forget to include the sorting icons in fileassets.
Furthermore, I wanted a dynamical way of creating such icons, so I modified the library code. If you find useful, please include in future versions.

1) I added the XUI library

2) In Class_Globals, added the Bitmaps vars:

B4X:
Public ascBmp,descBmp As B4XBitmap
3) Added the function (thanks to Erel and Filippo):
B4X:
Public Sub FontToBitmap (text As String, IsMaterialIcons As Boolean, FontSize As Float, color As Int) As B4XBitmap
    Dim xui As XUI
    Dim p As Panel = xui.CreatePanel("")
    p.SetLayoutAnimated(0, 0, 0, 32dip, 32dip)
    Dim cvs1 As B4XCanvas
    cvs1.Initialize(p)
    Dim t As Typeface
    If IsMaterialIcons Then t = Typeface.MATERIALICONS Else t = Typeface.FONTAWESOME
    Dim fnt As B4XFont = xui.CreateFont(t, FontSize)
    Dim r As B4XRect = cvs1.MeasureText(text, fnt)
    Dim BaseLine As Int = cvs1.TargetRect.CenterY - r.Height / 2 - r.Top
    cvs1.DrawText(text, cvs1.TargetRect.CenterX, BaseLine, fnt, color, "CENTER")
    Dim b As B4XBitmap = cvs1.CreateBitmap
    cvs1.Release
    Return b
End Sub
I didn't try to obtain the same functionality without XUI...

4) Changed the Initialize sub to have default images:

B4X:
Public Sub Initialize (CallBack As Object, EventName As String)
    cEventName = EventName
    cCallBack = CallBack
    ascBmp=FontToBitmap(Chr(0xF0DE),False,10,Colors.white)
    descBmp=FontToBitmap(Chr(0xF0DD),False,10,Colors.white)
End Sub
5) Changed the showHeaderSorting sub:

B4X:
    If (dir = -1) Then
        'sortingView.SetBackgroundImage(LoadBitmapSample(File.DirAssets, "sort_asc.png", ll, ll))
        sortingView.SetBackgroundImage(ascBmp)
    Else
        'sortingView.SetBackgroundImage(LoadBitmapSample(File.DirAssets, "sort_desc.png", ll, ll))
        sortingView.SetBackgroundImage(descBmp)
    End If
Hope this will be helpful...
 
Last edited:

advansis

Active Member
Licensed User
Forgive me, @klaus, it is your Flexible Table: https://www.b4x.com/android/forum/threads/class-flexible-table.30649/, but I'm not able to change the title of the post...

PS (@klaus): Sometimes parameters (col,row) are inverted: (row,col).
For example the method: Public Sub SetValue(Col As Int, Row As Int, Value As String)
While the event: CellClick(row As Int, col As Int)
Is there a reason for it?
 
Last edited:

klaus

Expert
Licensed User
but I'm not able to change the title of the post...
You can!
In the top right of the thread you have the option Thread Tools, where you can edit the title.

upload_2019-11-8_10-39-59.png


While the event: CellClick(row As Int, col As Int)
Sorry, but I don't understand. The Click event is CellClick(col As Int, row As Int)

upload_2019-11-8_10-45-41.png


If you find useful, please include in future versions.
I will have a look at it.
 

advansis

Active Member
Licensed User
You can!
In the top right of the thread you have the option Thread Tools, where you can edit the title.

View attachment 85367


Sorry, but I don't understand. The Click event is CellClick(col As Int, row As Int)

View attachment 85370


I will have a look at it.
1) Thank you, I've changed the title.

2) You are right: CellClick event was fixed in version Version 2.15, I took a bad version.

3) Probably this one must be reversed: Public Sub JumpToRowAndSelect(Row As Int, Col As Int)

Thanks again
 

klaus

Expert
Licensed User
Can you please test the demo project below.
JumpToRowAndSelect amended.
No need for the two image files any more. The two sorting bitmaps are generated directly in the class without XUI.
The width and color are properties.

upload_2019-11-9_9-49-29.png


To make the properties active you need to run the Designer and close it.
 

Attachments

Last edited:

advansis

Active Member
Licensed User
Can you please test the demo project below.
JumpToRowAbdSelect amended.
No need for the two image files any more. The two sorting bitmaps are generated directly in the class without XUI.
The width and color are properties.

View attachment 85385

To make the properties active you need to run the Designer and close it.
Very good, @klaus. I would leave the sorting bitmaps as public: so they could be changed as needed at runtime...

Another thing (sorry for asking you so many): column datatypes definition changes from "NUMBER"/"TEXT" in SetColumnDataType to "I"/"R"/"T" in LoadSQLiteDB2. Why this difference?
 

klaus

Expert
Licensed User
Can you test the attached project?
Added SetSortingBitmaps method and changed signature in LoadSQLiteDB2, it remains compatible with the older versions.
 

Attachments

Top