Share My Creation B4X Template Code Generator 1.00

I have created this code generator, using a code template to use the same name as the views in the layout.

The B4X Dialog Custom template (B4XPreferenceDialog) generates the necessary class to create the dialog using the names of the designer views.

The Layout as class template creates a class that allows you to use the designer view names directly from the class. If the layout has more chained layouts like "scrollview1" it will try to find the layout with the same name ("scrollview1.bal") add it to the class with the same names of the views that we put in the designer.

I used Erel's layout to JSON converter: https://www.b4x.com/android/forum/t...e-layouts-files-to-json-and-vice-versa.41623/

Download Tool Here

Download Sample Here

ScreenShot Template Code Generator 1.00.png
B4AScreenShoot.png
B4AScreenShoot2.png
B4JScreenShoot1.png
B4JScreenShoot2.png


Sample Code Generated for a Custom Dialog:
'CODE GENERATED BY TOOL. TEMPLATE: B4X DIALOG CUSTOM. LAYOUT: "layout5"
'Warning: if you change design names must be regenerate code.
'Parent: mContent
'Views: TitleBar,Forms,Label1,Icon,Title,Minimize,Maximize,Close

Sub Class_Globals
    Private xui As XUI
    Private mContent As B4XView
    Private mDialog As B4XDialog
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize(Parent As B4XView)
    mDialog.Initialize(Parent)
    mContent = xui.CreatePanel("")
    #if B4A
        setSize(80%x, 80%y) 'Set default size 80%
    #Else
        setSize(Parent.Width * 0.8, Parent.Height * 0.8) 'Set default size 80%
    #End if
End Sub

Private Sub LoadAllContent
    If mContent.IsInitialized Then mContent.RemoveAllViews
    mContent.LoadLayout("layout5") 'Here if need access views else BEFORE show   
    
End Sub

'You call first after initiliaze
Sub setSize(Width As Int, Height As Int)
    mContent.Width = Width
    mContent.Height = Height
    LoadAllContent
End Sub

'B4XDialog
Sub getDialog() As B4XDialog
    Return mDialog
End Sub

'Get the panel that contains this layout: "layout5"
Sub getContent() As B4XView 'Panel
    Return mContent
End Sub

Public Sub getTitleBar() As B4XView 'Panel
    Return GetView( "TitleBar" )
End Sub

Public Sub getIcon() As B4XView 'ImageView
    Return GetViewgetTitleBar( "Icon" )
End Sub

Public Sub getTitle() As B4XView 'Label
    Return GetViewgetTitleBar( "Title" )
End Sub

Public Sub getMinimize() As B4XView 'ImageView
    Return GetViewgetTitleBar( "Minimize" )
End Sub

Public Sub getMaximize() As B4XView 'ImageView
    Return GetViewgetTitleBar( "Maximize" )
End Sub

Public Sub getClose() As B4XView 'ImageView
    Return GetViewgetTitleBar( "Close" )
End Sub

'TitleBar Views: "Icon","Title","Minimize","Maximize","Close"
Sub GetViewgetTitleBar(Name As String) As B4XView
    Dim Views As List: Views.Initialize2(Array As String("Icon","Title","Minimize","Maximize","Close"))
    Return getTitleBar.GetView(Views.IndexOf(Name))
End Sub

Public Sub getForms() As B4XView 'Panel
    Return GetView( "Forms" )
End Sub

Public Sub getLabel1() As B4XView 'Label
    Return GetViewgetForms( "Label1" )
End Sub

'Forms Views: "Label1"
Sub GetViewgetForms(Name As String) As B4XView
    Dim Views As List: Views.Initialize2(Array As String("Label1"))
    Return getForms.GetView(Views.IndexOf(Name))
End Sub

'Activity Views: "TitleBar","Forms"
Sub GetView(Name As String) As B4XView
    Dim Views As List: Views.Initialize2(Array As String("TitleBar","Forms"))
    Return mContent.GetView(Views.IndexOf(Name))
End Sub

'Show a custom dialog using B4XDialog
Public Sub Show(Yes As Object, Cancel As Object, No As Object) As Object
    'LoadAllContent 'Here if not access views BEFORE show, else SetSize
    
    Dim res As Object = mDialog.ShowCustom(mContent, Yes, Cancel, No)

    TempFixedHeight 'Temp

    'Random Animate   
    AnimateDialog(Array As String("bottom", "top", "left", "right")(Rnd(0, 4)) )
            
    Return res
End Sub

'Close custom B4XDialog
Public Sub CloseDialog(Result As Int) As Boolean
    Return mDialog.Close(Result)
End Sub

'Animate Dialog Fragment Code from forum by Erel
Public Sub AnimateDialog (FromEdge As String)
    Dim base As B4XView = mDialog.Base
    Dim top As Int = base.Top
    Dim left As Int = base.Left
    Select FromEdge.ToLowerCase
        Case "bottom": base.Top = base.Parent.Height
        Case "top": base.Top = -base.Height
        Case "left": base.Left = -base.Width
        Case "right": base.Left = base.Parent.Width
    End Select
    base.SetLayoutAnimated(300, left, top, base.Width, base.Height)
End Sub

'Fixed buttons top
Sub TempFixedHeight
    For Each b As B4XView In mDialog.Base.GetAllViewsRecursive
        #if b4a
        If (Not(b Is Panel)) Then
        #end if
        #if b4j
        If (Not(b Is Pane)) Then
        #end if   
            If b Is Label Then b.Top = getContent.Top + getContent.Height + 1dip 'Fixed Top
        Else
            Exit
        End If
    Next
End Sub
 

Jhonn

Member
Correct code!!

Sample Code Generated for a Custom Dialog:
'CODE GENERATED BY TOOL. TEMPLATE: B4X DIALOG CUSTOM. LAYOUT: "layout5"
'Warning: if you change design names must be regenerate code.
'Parent: mContent
'Views: TitleBar,Forms,Label1,Icon,Title,Minimize,Maximize,Close

Sub Class_Globals
    Private xui As XUI
    Private mContent As B4XView
    Private mDialog As B4XDialog
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize(Parent As B4XView)
    mDialog.Initialize(Parent)
    mContent = xui.CreatePanel("")
    #if B4A
    setSize(80%x, 80%y) 'Set default size 80%
    #Else
        setSize(Parent.Width * 0.8, Parent.Height * 0.8) 'Set default size 80%
    #End if
End Sub

Private Sub LoadAllContent
    If mContent.IsInitialized Then mContent.RemoveAllViews
    mContent.LoadLayout("layout5") 'Here if need access views else BEFORE show
    
End Sub

'You call first after initiliaze
Sub setSize(Width As Int, Height As Int)
    mContent.Width = Width
    mContent.Height = Height
    LoadAllContent
End Sub

'B4XDialog
Sub getDialog() As B4XDialog
    Return mDialog
End Sub

'Get the panel that contains this layout: "layout5"
Sub getContent() As B4XView 'Panel
    Return mContent
End Sub

Public Sub getTitleBar() As B4XView 'Panel
    Return GetView( "TitleBar" )
End Sub

Public Sub getIcon() As B4XView 'ImageView
    Return GetViewTitleBar( "Icon" )
End Sub

Public Sub getTitle() As B4XView 'Label
    Return GetViewTitleBar( "Title" )
End Sub

Public Sub getMinimize() As B4XView 'ImageView
    Return GetViewTitleBar( "Minimize" )
End Sub

Public Sub getMaximize() As B4XView 'ImageView
    Return GetViewTitleBar( "Maximize" )
End Sub

Public Sub getClose() As B4XView 'ImageView
    Return GetViewTitleBar( "Close" )
End Sub

'TitleBar Views: "Icon","Title","Minimize","Maximize","Close"
Sub GetViewTitleBar(Name As String) As B4XView
    Dim Views As List: Views.Initialize2(Array As String("Icon","Title","Minimize","Maximize","Close"))
    Return getTitleBar.GetView(Views.IndexOf(Name))
End Sub

Public Sub getForms() As B4XView 'Panel
    Return GetView( "Forms" )
End Sub

Public Sub getLabel1() As B4XView 'Label
    Return GetViewForms( "Label1" )
End Sub

'Forms Views: "Label1"
Sub GetViewForms(Name As String) As B4XView
    Dim Views As List: Views.Initialize2(Array As String("Label1"))
    Return getForms.GetView(Views.IndexOf(Name))
End Sub

'Activity Views: "TitleBar","Forms"
Sub GetView(Name As String) As B4XView
    Dim Views As List: Views.Initialize2(Array As String("TitleBar","Forms"))
    Return mContent.GetView(Views.IndexOf(Name))
End Sub

'Show a custom dialog using B4XDialog
Public Sub Show(Yes As Object, Cancel As Object, No As Object) As Object
    'LoadAllContent 'Here if not access views BEFORE show, else SetSize
    
    Dim res As Object = mDialog.ShowCustom(mContent, Yes, Cancel, No)

    TempFixedHeight 'Temp

    'Random Animate
    AnimateDialog(Array As String("bottom", "top", "left", "right")(Rnd(0, 4)) )
            
    Return res
End Sub

'Close custom B4XDialog
Public Sub CloseDialog(Result As Int) As Boolean
    Return mDialog.Close(Result)
End Sub

'Animate Dialog Fragment Code from forum by Erel
Public Sub AnimateDialog (FromEdge As String)
    Dim base As B4XView = mDialog.Base
    Dim top As Int = base.Top
    Dim left As Int = base.Left
    Select FromEdge.ToLowerCase
        Case "bottom": base.Top = base.Parent.Height
        Case "top": base.Top = -base.Height
        Case "left": base.Left = -base.Width
        Case "right": base.Left = base.Parent.Width
    End Select
    base.SetLayoutAnimated(300, left, top, base.Width, base.Height)
End Sub

'Fixed buttons top
Sub TempFixedHeight
    #IF B4A
    For Each b As B4XView In mDialog.Base.GetAllViewsRecursive
        If (Not(b Is Panel)) Then
            If b Is Label Then b.Top = getContent.Top + getContent.Height + 1dip 'Fixed Top
        Else
            Exit
        End If
    Next
    #END IF
End Sub
 
Top