Android Question Switching from one panel to the next

Sanxion

Active Member
Licensed User
Longtime User
Guys

I have been considering the most efficient method of switching from one panel to the next in an app that comprises of a single "layout" and many panels. Each panel has its own tag ID and only one panel can be visible at any time. The "next" and "back" buttons are located on the layout.

The logic appears to be:

Loop through the panels to get the ID of the current visible panel and store its ID
Loop through the panels again
If the panel ID is the same as the stored ID + 1 then make that panel visible
Else make the panel invisible

Is there a better way?

Thanks
 

JonPM

Well-Known Member
Licensed User
Longtime User
If it works without any noticeable delay then it is a good method (at least in my book) :)
 
Upvote 0

Sanxion

Active Member
Licensed User
Longtime User
I will give it a try and see how it functions...as a related point, is it necessary to set both the enabled and visible properties of the current panel to false when switching to the next or is it sufficient to set the visible property to false only?
 
Upvote 0

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
For what it's worth, here's the basic logic prototype for how I handle multiple full-screen panels in one activity.

B4X:
Sub Process_Globals

End Sub

Sub Globals
    Private AppScreens(3) As Panel
    Private CurrScreen As Int = 0

End Sub

Sub Activity_Create(FirstTime As Boolean)
    CreatePanels
End Sub

Sub Activity_Resume
    ShowCurrentScreen
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Private Sub CreatePanels
    Dim piIndex As Int
    Dim psTag As String
    ' I create layouts for each panel, saved in the format "layPanel_##"
    For piIndex = 0 To AppScreens.Length - 1
        psTag = NumberFormat2(piIndex, 2, 0, 0, False)
        AppScreens(piIndex).Initialize("AppPanel" & psTag)
        Activity.AddView(AppScreens(piIndex), 0, 0, Activity.Width, Activity.Height)
        AppScreens(piIndex).LoadLayout("layPanel_" & psTag)
        ' Set panel tag to our index value so we can compare against it in the show screen method
        AppScreens(piIndex).Tag = piIndex        
    Next
End Sub

Private Sub ShowCurrentScreen
    For Each poPan As Panel In AppScreens
        If poPan.Tag = CurrScreen Then
            poPan.Visible = True
            poPan.BringToFront
        Else
            poPan.Visible = False
        End If
    Next
End Sub
There are many ways to implement this, this is just the one I like the best. It allows me to add/remove panels without having to mess with the create/display logic every time.

With this method, you could increment/decrement the "CurrScreen" variable in your next/previous events then call the ShowCurrentScreen method.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Your solution looks complicated to me.
You have an array of Panels and you have a variable CurrScreen for the current screen.
Why not use:
B4X:
Private Sub ShowCurrentScreen(NewScreen As Int)
    AppScreens(CurrScreen).Visible = False
    CurrScreen = NewScreen
    AppScreens(CurrScreen).Visible = True
End Sub
 
Upvote 0

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
That is indeed a more direct solution; I was more interested in presenting the concept to the OP rather than providing a fully-coded, complete solution for them to use. ;)
 
Upvote 0
Top