Android Code Snippet Flexible Table Sort Icons

Discussion in 'Code Snippets' started by advansis, Nov 7, 2019.

  1. advansis

    advansis 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:

    Code:
    Public ascBmp,descBmp As B4XBitmap
    3) Added the function (thanks to Erel and Filippo):
    Code:
    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(
    00032dip32dip)
        
    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:

    Code:
    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:

    Code:
    If (dir = -1Then
            
    '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: Nov 8, 2019
    AnandGupta likes this.
  2. klaus

    klaus Expert Licensed User

    Which TableView are you speaking of?
     
  3. advansis

    advansis 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: Nov 8, 2019
  4. klaus

    klaus Expert Licensed User

    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

    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

    I will have a look at it.
     
  5. advansis

    advansis Member Licensed User

    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
     
  6. klaus

    klaus Expert 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.

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

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

    Attached Files:

  7. advansis

    advansis Member Licensed User

    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?
     
  8. klaus

    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.
     

    Attached Files:

    advansis likes this.
  9. advansis

    advansis Member Licensed User

    Tested ok, for my purposes. Thanks
     
    klaus likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice