Android Question Multiple Forms or Pages or whatever they're called - two questions

Hello,

I apologize for how n00b these two questions are, but any help with them would be greatly appreciated. I'm new to B4X (B4A), but am a long time (i.e. old) programmer and VB used to be my favorite programming language.

  1. How on earth do you declare a truly Global variable?
  2. How can I create a table of contents form/screen in a multi-form app, that displays each form in order based on user swiping right to get to the next form, but he/she can swipe up to see a table of contents and select any form in the sequence that they would like to jump to?
Regarding my first question, no matter where I try to declare a truly global variable, e.g. Process_Globals of Main activity module where the default comments state "can be accessed from all modules", I get errors in other modules that the variable is undeclared when I reference it. Is "all modules" in that comment truly all types of modules (classes, code, activity, & service), or only certain types modules (and does the Process that runs this app include all types of modules)?

Regarding my second question, I am trying to make this app cross-platform as much as possible. I started out creating my first form/screen/page as a B4XPages module. I have code that seems to work fine to detect user swipes and taps, but I'm using a Panel for it, that I pop up, if the user swipes up. The user can then click on a drop down box, and select a different page/form/screen and I attempt to display it. I'd like to only have the code for this Table of Contents panel in one place, but I can't seem to find a way to do that either. The code for my main (testing right now) module is below, perhaps it'll enlighten someone as to my n00bness problems here and be able to help straighten me out. Again any help is greatly appreciated!

I apologize for the messiness of the code and lack of comments. I usually start with a minimal code framework that gets everything working, and then I write the real piece with properly named variables and comments throughout, but this is completely raw right now.

pnlTouch is the panel that reads the user's input for swiping left, right, up, etc. pnlToC is the panel to bring up the Table of Contents. Conversation1_1 is a second page/form/screen that I am testing all of these concepts with:

Main:
Sub Process_Globals

    Public ActionBarHomeClicked As Boolean

    Private xui As XUI

    Dim MOVEMENT_THRESHOLD As Int

    MOVEMENT_THRESHOLD = 50dip

    Public DISPLAYPAGE As String = ""

    'Public MsgTst As MessageTest

End Sub


Sub Globals

    Dim pnlToC As Panel

    Dim pnlTouch As Panel

    Dim StartX As Int

    Dim StartY As Int

    Dim Spinner1 As Spinner

    'Public vwConversation1_1 As Conversation1_1

    'Public vwToC As ToC

End Sub


Public Sub test

    'xui.MsgboxAsync("In test", "Testing")

End Sub


'Sub Popup_Click

''    Dim pnlInput As Panel

'    pnlToC.Initialize ("Select")

'    pnlToC.Color  = Colors.ARGB (150,0,0,0)

'    Activity.AddView (pnlToC, 0, 0,   100%x, 100%y)

'End Sub


Sub Select_Click As Boolean

    Return True

End Sub


Private Sub pnlToC_Touch (Action As Int, X As Float, Y As Float) As Boolean

    Return True

End Sub


Sub Activity_Create(FirstTime As Boolean)

    Dim pm As B4XPagesManager

'    vwToC.Initialize

'    B4XPages.AddPage("Table of Contents", vwToC)  

    pm.Initialize(Activity)

    pnlTouch.Initialize("pnlTouch")

    Activity.AddView(pnlTouch, 0, 0, 100%x, 100%y)

    pnlTouch.SendToBack


End Sub


Sub pnlTouch_Touch (Action As Int, X As Float, Y As Float) As Boolean 'Return True to consume the event

    Select Action

        Case Activity.ACTION_DOWN

            StartX = X

            StartY = Y

        Case Activity.ACTION_UP

            If Abs(X - StartX) > MOVEMENT_THRESHOLD And Abs(Y - StartY) < MOVEMENT_THRESHOLD Then

                If X > StartX Then

                    Swipe("Right")

                Else

                    Swipe("Left")

                End If

            Else If Abs(X - StartX) < MOVEMENT_THRESHOLD And Abs(Y - StartY) > MOVEMENT_THRESHOLD Then

                If Y > StartY Then

                    Swipe("Down")

                Else

                    Swipe("Up")

                End If

            Else

                Swipe ("Click/Tap")

                'Log("User just clicked/tapped")

            End If

    End Select

    Return True

End Sub


Sub Swipe(SwipeDir As String)

    'xui.MsgboxAsync(SwipeDir, "Swipe")

    Select SwipeDir

        Case "Left"

        Case "Right"

        Case "Up"

            pnlTouch.Enabled=False

            pnlToC.Initialize ("Select")

            'pnlToC.Color  = Colors.ARGB (150,0,0,0)

            'pnlToC.Color=255

            Activity.AddView (pnlToC, 50dip, 50dip, 75%x, 75%y)

            pnlToC.LoadLayout("vwToC")

            pnlToC.BringToFront

            Spinner1.Add("Main Page")

            Spinner1.Add("Conversation1_1")

            Spinner1.Color=222

            'B4XPages.ShowPageAndRemovePreviousPages("Toc")

        Case "Down"

        Case "Click/Tap"

            'B4XPages.ClosePage(Me)

            'pnlToC.RemoveView

    End Select

End Sub


'Template version: B4A-1.01

#Region Delegates


Sub Activity_ActionBarHomeClick

    ActionBarHomeClicked = True

    B4XPages.Delegate.Activity_ActionBarHomeClick

    ActionBarHomeClicked = False

End Sub


Sub Activity_KeyPress (KeyCode As Int) As Boolean

    Return B4XPages.Delegate.Activity_KeyPress(KeyCode)

End Sub


Sub Activity_Resume

    B4XPages.Delegate.Activity_Resume

End Sub


Sub Activity_Pause (UserClosed As Boolean)

    B4XPages.Delegate.Activity_Pause

End Sub


Sub Activity_PermissionResult (Permission As String, Result As Boolean)

    B4XPages.Delegate.Activity_PermissionResult(Permission, Result)

End Sub


Sub Create_Menu (Menu As Object)

    B4XPages.Delegate.Create_Menu(Menu)

End Sub


#if Java

public boolean _onCreateOptionsMenu(android.view.Menu menu) {

     processBA.raiseEvent(null, "create_menu", menu);

     return true;

 

}

#End If

#End Region


'Program code should go into B4XMainPage and other pages.


Private Sub Button1_Click

    pnlToC.RemoveView

    pnlTouch.Enabled=True

    'xui.MsgboxAsync("Hello world!", "B4X")

End Sub


Private Sub Spinner1_ItemClick (Position As Int, Value As Object)

    'xui.MsgboxAsync(Value, "Chosen")

'    Dim m As B4XPagesManager

'    m.Initialize(Activity)

    Select Value

        Case "Conversation1_1"

            DISPLAYPAGE="Conversation1_1"

            pnlToC.RemoveView

            pnlTouch.Enabled=True

            StartActivity(Conversation1_1)

'            m.ClosePage(Me)

'            m.ShowPage("Conversation1_1")

            'B4XPages.ShowPage("Conversation1_1")

        Case "Main Page"

            pnlToC.RemoveView

            pnlTouch.Enabled=True

'            m.ClosePage(Me)

'            m.ShowPage("B4XMainPage")

            'B4XPages.ShowPage("B4XMainPage")

    End Select


End Sub
Sub Process_Globals
Public ActionBarHomeClicked As Boolean
Private xui As XUI
Dim MOVEMENT_THRESHOLD As Int
MOVEMENT_THRESHOLD = 50dip
Public DISPLAYPAGE As String = ""
'Public MsgTst As MessageTest
End Sub

Sub Globals
Dim pnlToC As Panel
Dim pnlTouch As Panel
Dim StartX As Int
Dim StartY As Int
Dim Spinner1 As Spinner
'Public vwConversation1_1 As Conversation1_1
'Public vwToC As ToC
End Sub

Public Sub test
'xui.MsgboxAsync("In test", "Testing")
End Sub

'Sub Popup_Click
'' Dim pnlInput As Panel
' pnlToC.Initialize ("Select")
' pnlToC.Color = Colors.ARGB (150,0,0,0)
' Activity.AddView (pnlToC, 0, 0, 100%x, 100%y)
'End Sub

Sub Select_Click As Boolean
Return True
End Sub

Private Sub pnlToC_Touch (Action As Int, X As Float, Y As Float) As Boolean
Return True
End Sub

Sub Activity_Create(FirstTime As Boolean)
Dim pm As B4XPagesManager
' vwToC.Initialize
' B4XPages.AddPage("Table of Contents", vwToC)
pm.Initialize(Activity)
pnlTouch.Initialize("pnlTouch")
Activity.AddView(pnlTouch, 0, 0, 100%x, 100%y)
pnlTouch.SendToBack

End Sub

Sub pnlTouch_Touch (Action As Int, X As Float, Y As Float) As Boolean 'Return True to consume the event
Select Action
Case Activity.ACTION_DOWN
StartX = X
StartY = Y
Case Activity.ACTION_UP
If Abs(X - StartX) > MOVEMENT_THRESHOLD And Abs(Y - StartY) < MOVEMENT_THRESHOLD Then
If X > StartX Then
Swipe("Right")
Else
Swipe("Left")
End If
Else If Abs(X - StartX) < MOVEMENT_THRESHOLD And Abs(Y - StartY) > MOVEMENT_THRESHOLD Then
If Y > StartY Then
Swipe("Down")
Else
Swipe("Up")
End If
Else
Swipe ("Click/Tap")
'Log("User just clicked/tapped")
End If
End Select
Return True
End Sub

Sub Swipe(SwipeDir As String)
'xui.MsgboxAsync(SwipeDir, "Swipe")
Select SwipeDir
Case "Left"
Case "Right"
Case "Up"
pnlTouch.Enabled=False
pnlToC.Initialize ("Select")
'pnlToC.Color = Colors.ARGB (150,0,0,0)
'pnlToC.Color=255
Activity.AddView (pnlToC, 50dip, 50dip, 75%x, 75%y)
pnlToC.LoadLayout("vwToC")
pnlToC.BringToFront
Spinner1.Add("Main Page")
Spinner1.Add("Conversation1_1")
Spinner1.Color=222
'B4XPages.ShowPageAndRemovePreviousPages("Toc")
Case "Down"
Case "Click/Tap"
'B4XPages.ClosePage(Me)
'pnlToC.RemoveView
End Select
End Sub

'Template version: B4A-1.01
#Region Delegates

Sub Activity_ActionBarHomeClick
ActionBarHomeClicked = True
B4XPages.Delegate.Activity_ActionBarHomeClick
ActionBarHomeClicked = False
End Sub

Sub Activity_KeyPress (KeyCode As Int) As Boolean
Return B4XPages.Delegate.Activity_KeyPress(KeyCode)
End Sub

Sub Activity_Resume
B4XPages.Delegate.Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
B4XPages.Delegate.Activity_Pause
End Sub

Sub Activity_PermissionResult (Permission As String, Result As Boolean)
B4XPages.Delegate.Activity_PermissionResult(Permission, Result)
End Sub

Sub Create_Menu (Menu As Object)
B4XPages.Delegate.Create_Menu(Menu)
End Sub

#if Java
public boolean _onCreateOptionsMenu(android.view.Menu menu) {
processBA.raiseEvent(null, "create_menu", menu);
return true;

}
#End If
#End Region

'Program code should go into B4XMainPage and other pages.

Private Sub Button1_Click
pnlToC.RemoveView
pnlTouch.Enabled=True
'xui.MsgboxAsync("Hello world!", "B4X")
End Sub

Private Sub Spinner1_ItemClick (Position As Int, Value As Object)
'xui.MsgboxAsync(Value, "Chosen")
' Dim m As B4XPagesManager
' m.Initialize(Activity)
Select Value
Case "Conversation1_1"
DISPLAYPAGE="Conversation1_1"
pnlToC.RemoveView
pnlTouch.Enabled=True
StartActivity(Conversation1_1)
' m.ClosePage(Me)
' m.ShowPage("Conversation1_1")
'B4XPages.ShowPage("Conversation1_1")
Case "Main Page"
pnlToC.RemoveView
pnlTouch.Enabled=True
' m.ClosePage(Me)
' m.ShowPage("B4XMainPage")
'B4XPages.ShowPage("B4XMainPage")
End Select

End Sub
[/CODE]
 
Last edited:
1) Access public Process Globals using the module name as a prefix

Main.ActionBarHomeClicked

2) Pass!

@agraham OK, so I could have sworn I had tried that, but apparently not. Thanks on #1!
Now for #2, any takers (please ignore the code below the code block, it's the same as the code in the block. I messed up on the post somehow)?
 
Upvote 0

MicroDrie

Well-Known Member
Licensed User
1. How on earth do you declare a truly Global variable?
Add in B4XMainPage:
Sub Class_Globals
    Public Variable1 As String = "Variable 1 in B4XMainPage"
End Sub
Read global public in B4XMainPage to label:
    lbl1.Text = B4XPages.MainPage.Variable1

Now for #2, any takers (please ignore the code below the code block, it's the same as the code in the block. I messed up on the post somehow)?
Delete the code after the first [/code] tag in edit mode to clean the first post.

To be able to help you, it is better to first add your program via File Export as a zip file.
 
Upvote 0

AnandGupta

Expert
Licensed User
Longtime User
I use below as template for my apps,

You get a drawer menu and you create and show pages depending on menu selected from drawer.
Looks like standard app behaviour, if not very fancy.
 
Upvote 0
Add in B4XMainPage:
Sub Class_Globals
    Public Variable1 As String = "Variable 1 in B4XMainPage"
End Sub
Read global public in B4XMainPage to label:
    lbl1.Text = B4XPages.MainPage.Variable1


Delete the code after the first [/code] tag in edit mode to clean the first post.

To be able to help you, it is better to first add your program via File Export as a zip file.
@MicroDrie Thanks for the help. I don't see a way to delete the code below the tags because edit mode is no longer available. With help from others, I've been able to figure #2 out now.
 
Upvote 0
I use below as template for my apps,

You get a drawer menu and you create and show pages depending on menu selected from drawer.
Looks like standard app behaviour, if not very fancy.
@AnandGupta Thank you, I had not seen the drawer code before. I think I've figure things out now, thanks for the help!
 
Upvote 0
Top