Android Question xCustomView - Panel with 3 Label

Gianni Sassanelli

Active Member
Licensed User
Longtime User
Hi
i'm developing a xCustomView with 3 label into
I know how to do it, but don't know how to handle the Label_1_Click, Label2_click... of each view inserted into my custom class.
i'm starting from xClsCustomButtonClass of Booklet documentation

Can i Have help please?
my class code is
Class code:
'xCustomButton CustomView cross platform class.
'Version 1.0
'Author: Klaus CHRISTL (klaus)
'
'Events declarations
#Event: Click
'#Event: LongClick
'#Event: CheckdChange(Checked As Boolean)
#RaisesSynchronousEvents: Click
'#RaisesSynchronousEvents: LongClick
'#RaisesSynchronousEvents: CheckdChange

'Designer property declarations
#DesignerProperty: Key: IconsTextColor, DisplayName: IconsTextColor, FieldType: Color, DefaultValue: 0xFFFFFFFF, Description: ForeGroundColor of ToolBar Icons.
#DesignerProperty: Key: HomeIconLib, DisplayName: Home Icon Library, FieldType: String, DefaultValue: MATERIAL, List: MATERIAL|AWESOME, Description: Select Library of Home Icon.
#DesignerProperty: Key: MenuIconLib, DisplayName: Menu Icon Library, FieldType: String, DefaultValue: MATERIAL, List: MATERIAL|AWESOME, Description: Select Library of Menù Icon.
#DesignerProperty: Key: BackgroundColor, DisplayName: BackgroundColor, FieldType: Color, DefaultValue: 0x00FFFFFF, Description: Background color.

'#DesignerProperty: Key: ButtonType, DisplayName: ButtonType, FieldType: String, DefaultValue: STANDARD, List: STANDARD|TOGGLE, Description: Select the buttton Type.
'#DesignerProperty: Key: Text, DisplayName: Text, FieldType: String, DefaultValue: Text, Description: Text at the bottom of the button.
'#DesignerProperty: Key: OnStateText, DisplayName: OnStateText, FieldType: String, DefaultValue: ON, Description: Text at the bottom of the button.
'#DesignerProperty: Key: PressedColor, DisplayName: PressedColor, FieldType: Color, DefaultValue: 0xFFFFFF00, Description: Color when the button is pressed.
'#DesignerProperty: Key: OnStateColor, DisplayName: OnStateColor, FieldType: Color, DefaultValue: 0xFFFF00FF, Description: Color for the ON state.
'#DesignerProperty: Key: OnStateBackgroundColor, DisplayName: OnStateBackgroundColor, FieldType: Color, DefaultValue: 0x00FFFFFF, Description: Color of the background for the ON state.

'xCustomButton is a button based on a Panel with two Labels
'one with a Material Icon and the other with text.
'It has two events: Click and LongClick.
Sub Class_Globals
#If B4J
    Private fx As JFX
#End If
    Private xui             As XUI
    Private mEventName     As String
    Private mCallBack     As Object
    Private xBase             As B4XView
    Private xParent         As B4XView   
    Private mLeft, mTop, mWidth, mHeight As Int
    Private mTag             As Object
  
    Private xLabelFont, xIconFont                 As B4XFont
    Private mIconTextSize, mCaptionTextSize     As Double
    Private mIconsColor, mTextColor                 As Int
    Private mCaptionText, mHomeIconText, mMenuIconText As String
    
    Private HomeIcon, MenuIcon, CaptionLabel As B4XView
End Sub
Public Sub Initialize (Callback As Object, EventName As String)
    mEventName         = EventName
    mCallBack         = Callback
    mHomeIconText     = Chr(0xF03C)
    mCaptionText     = "Standard Caption"
    mTextColor         = xui.Color_Red
    
End Sub
Public Sub DesignerCreateView (Base As Object, Lbl As Label, Props As Map)
    Private mDummy As B4XView
    mDummy             = Base
    mLeft             = mDummy.Left
    mTop                 = mDummy.Top
    mWidth             = mDummy.Width
    mHeight            = mDummy.Height
    
    
   
    xBase                 = xui.CreatePanel("xBase")
    xBase.Color         = Props.Get("BackgroundColor")
    xParent                 = mDummy.Parent
    xParent.AddView(xBase, mLeft, mTop, mWidth, mHeight)
    mTag                     = mDummy.Tag
    
    mTextColor             = xui.PaintOrColorToColor(Lbl.TextColor)
    mCaptionTextSize    = Lbl.TextSize
    mIconsColor         = xui.PaintOrColorToColor(Props.Get("IconsTextColor"))
    mHomeIconText         = Chr(0xE88A)
    mMenuIconText        = Chr(0xE23D)
'    mDummy.RemoveViewFromParent
    Dim dummyIcon As Label
    dummyIcon.Initialize("")
    dummyIcon.Typeface     = Typeface.MATERIALICONS
    dummyIcon.Text            = Chr(0xE88A)
    dummyIcon.TextSize    = GetMaxIconTextsize(dummyIcon)
    #If B4A
        xIconFont     = xui.CreateFont(Typeface.MATERIALICONS, dummyIcon.TextSize)
        xLabelFont    = xui.CreateFont(Lbl.Typeface, Lbl.TextSize)
    #Else
        xIconFont     = dummyIcon.Font
        xLabelFont    = Lbl.Font
    #End If
    
    HomeIcon                  = dummyIcon
    HomeIcon.Font             = xIconFont
    HomeIcon.SetTextAlignment("CENTER", "CENTER")
    HomeIcon.Text             = mHomeIconText
    HomeIcon.TextSize     = dummyIcon.TextSize
    HomeIcon.TextColor     = xui.PaintOrColorToColor(mTextColor)
    dummyIcon.Initialize("")
    MenuIcon                    = dummyIcon
    MenuIcon.Font             = xIconFont
    MenuIcon.SetTextAlignment("CENTER", "CENTER")
    MenuIcon.Text             = mMenuIconText
    MenuIcon.TextSize     = HomeIcon.TextSize
    MenuIcon.TextColor    = HomeIcon.TextColor
    'dummyCaption.Initialize("")
    CaptionLabel                = Lbl
    CaptionLabel.Font         = xLabelFont
    CaptionLabel.SetTextAlignment("CENTER", "LEFT")
    CaptionLabel.Text         = Lbl.Text
    CaptionLabel.Visible        = Lbl.Visible
    CaptionLabel.TextSize     = mCaptionTextSize
    CaptionLabel.TextColor    = HomeIcon.TextColor


    xBase.AddView(HomeIcon, 1dip, 1dip, xBase.Height, xBase.Height)
    xBase.AddView(MenuIcon, xBase.Width -xBase.Height- 1dip, 1dip, xBase.Height, xBase.Height)
    xBase.AddView(CaptionLabel, xBase.Height + 10dip,0dip,  xBase.Width -(2*xBase.Height) -12dip ,xBase.Height)
End Sub
Private Sub GetMaxIconTextsize(l As Label) As Double
    Dim h, ts As Double
    Dim cv As Canvas
    cv.Initialize(xBase)
    ts = 10 ' valore base di partenza
    h = cv.MeasureStringHeight(l.Text ,l.Typeface,ts)
    Do While h < xBase.Height - 20dip
        ts = ts * 105 / 100
        h = cv.MeasureStringHeight(l.Text ,l.Typeface,ts)
    Loop   
    Return ts
End Sub


Sub xBase_Click
    'Is this the right point?
    Dim xObj As B4XView = Sender
    Dim xObj2 As B4XView = xObj.GetView(0)
    Dim xObj3 As B4XView = xObj.GetView(1)
    
    If xui.SubExists(mCallBack, mEventName & "_Click", 0) Then
        CallSubDelayed(mCallBack, mEventName & "_Click")
    End If
End Sub


Public Sub GetBase As Object
    Return xBase
End Sub
 

klaus

Expert
Licensed User
Longtime User
What kind of CustomView do you want to make ?
Only a Panel and 3 Labels ?
But in your code you are also using icons, so there also images.
For the Click event you should set the event name of all your Labels to the same name and give the Tag property a different value.
And then, in the Click event routine, Sender is the clicked Label, and with the Tag property you know which Label was clicked.

In the xCustomButton project I use the mBase_Touch event because I want to create an external Click event and an external LongClick event.
And I use the mBase_Touch event because I want an event raised when the user touches everywhere on the base Panel and not on the individual views on the base Panel.

You need to give more information on what exactly you want to do.
You could also post your project so we could deeper look into it and give you concrete advice.
 
Upvote 0

Gianni Sassanelli

Active Member
Licensed User
Longtime User
thank's you Klaus.
the attached image is my xCustomView that i use as ActionBar for all my Activity and B4xPages because is more simply for me setting some attributes as icon, color, background and text

Now the class works thanks to the invaluable help received from other members

I added other event called Label_1_Click, Label_3_Click and use it in the some way of xBase_Click

1628408293531.png
 
Upvote 0
Top