Android Question Loading Layouts into B4XDrawer panel as Class

josejad

Expert
Licensed User
Longtime User
Hi all:

I've made 3 small apps for my job. Simple apps with one or two layouts, in one or two activities, and code a "little" messy
Now, it's time to go one step beyond... I want to join the small apps in a unique app, and add new functionalities, abstract some code into classes, etc...

First step, I'm just triying to make the bare bone of the app.
I want to use the B4XDrawer as main menu, to change between the different pieces.
https://www.b4x.com/android/forum/threads/b4x-b4xdrawer-sliding-drawer.97828/

The left menu will be changing, some times will change panels in the same activity, some times will start a different activity.

So I've adapted this example to use AppCompat Action bar as a class and reuse the code.
https://www.b4x.com/android/forum/t...pat-actionbar-code-appwide.96173/#post-607172

I've created a Class called Drawer:
B4X:
Sub Class_Globals
    Private DrawerSEMI As B4XDrawer
    Private ACToolBarLight1 As ACToolBarLight
    Private ToolbarHelper As ACActionBar
    Private MenuItems As ListView
    Private Menus As List
    Private stringModuleName  As String
    Private pnlPrincipal As B4XView
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize (Actividad As String, Parent As B4XView)
    stringModuleName = Actividad
    DrawerSEMI.Initialize(Me, "Drawer", Parent, 300dip)
    Menus.Initialize
    Menus.AddAll (Array As String("Main Menu", "Checklist NSN" , "Almacén"))
    DrawerSEMI.LeftPanel.LoadLayout("Left")
    DrawerSEMI.CenterPanel.LoadLayout("1")
    Select stringModuleName
        Case "Main"
            For i = 0 To Menus.Size -1
                MenuItems.AddSingleLine (Menus.Get(i))
            Next
   
        Case "ChecklistNSN"
            ACToolBarLight1.SubTitle = "Checklist NSN"
            MenuItems.Clear
            MenuItems.AddSingleLine("Menú Principal")
            For i = 1 To 3
                MenuItems.AddSingleLine("Menu Checklist " & i)
            Next
'            For i = 0 To 20
'                'CLVSites.AddTextItem("Item: " & i, i)
'            Next
            'DrawerSEMI.CenterPanel.LoadLayout("sites")
    End Select
   
    ToolbarHelper.Initialize
    ToolbarHelper.ShowUpIndicator = True 'set to true to show the up arrow
    Dim bd As BitmapDrawable
    bd.Initialize(LoadBitmap(File.DirAssets, "hamburger.png"))
    ToolbarHelper.UpIndicatorDrawable =  bd
    ACToolBarLight1.InitMenuListener
    ACToolBarLight1.Title = "Activity " & stringModuleName
End Sub

Sub ACToolBarLight1_NavigationItemClick
    DrawerSEMI.LeftOpen = Not(DrawerSEMI.LeftOpen)
End Sub

Sub MenuItems_ItemClick (Position As Int, Value As Object)
    DrawerSEMI.LeftOpen = Not(DrawerSEMI.LeftOpen)
    Log(stringModuleName)
    Log(Value)
    Select Value
        Case "Menú Principal"
            If stringModuleName <> "Main"   Then StartActivity (Main)
        Case "Checklist NSN"
            If stringModuleName <> "Checklist NSN"   Then StartActivity (ChecklistNSN)
        Case "Third"
           
        Case 2
       
        Case 3
           
    End Select
End Sub

Public Sub LoadView (layout As String)
    pnlPrincipal.LoadLayout(layout)
End Sub

In the Activities, I use:
B4X:
Sub Globals
    Private MenuMain As Drawer
End Sub

Sub Activity_Create(FirstTime As Boolean)
    MenuMain.Initialize ("Main", Activity)
End Sub

It works. But I don't know how to access from my activity to the UI elements in the layout, due to I load the layout in the class, not in my activity.

Attached a test project


Thanks in advance
 

Attachments

  • B4XDrawer class.zip
    20 KB · Views: 293

JordiCP

Expert
Licensed User
Longtime User
You can simply add a getter to your Drawer class, and load the layout from the Activity.
B4X:
Sub GetPnlPrincipal As B4XView
    Return pnlPrincipal
End Sub

And call this from your ChecklistNSN Activity
B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Private MenuChecklist As Drawer
    Private CLVSites As CustomListView
    Private btn_AddSite As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:

    MenuChecklist.Initialize ("ChecklistNSN", Activity)

    'MenuChecklist.CargarVista("sites")
    MenuChecklist.GetPnlPrincipal.LoadLayout("sites")  '<-- With this change, you load the layout from here and all elements will be accessible,
    For i = 0 To 20
        CLVSites.AddTextItem("Item: " & i, i)
    Next
End Sub

Sub btn_AddSite_Click
    Log("Hello!")
End Sub
 
Upvote 0

josejad

Expert
Licensed User
Longtime User
So easy¡¡
Thanks¡¡ I was trying to figure it out, but I didn't get it¡¡

Gracias Jordi¡¡ y felices fiestas
 
Upvote 0
Top