Android Tutorial Different examples with 2 layouts

As an answer to the thread Forms alike, I thought that the examples below would be of general interest.

Attached there are 4 examples with two same layouts but with different managements:

- TwoPanelActivity
One Activity with 1 Layout with 2 Panels and all views on the panels.
All the code is in the Main module.
Changing the Layout will not resume the activity.
There is only one layout file.

The 2 layout files for the 3 following projects are the same .
- TwoPanelLayouts
One Activity with 2 Panels and 1 Layout file for each loaded in Activity_Create.
All the code is in the Main module.
Changing the Layout will not resume the activity.
There are 2 layout files, plus a third one with the Panels, but these could also be initialized in the code.

- TwoActivityLayouts
One Activity with 2 Layouts.
All the code is in the Main module.
Changing the Layout will not resume the activity.
There are 2 layout files.

- TwoLayoutActivities
2 Activities with 1 Layout for each.
The code is in two modules Main and Layout2.
Changing the Layout will resume the current activity.
There are 2 layout files.

All 4 have advantages and disadvantages, depending on the size of the project. Memory, speed, code in one or several modules etc. An advantage for one project could become a disadvantage for another project.

- TwoPanelActivity
B4X:
Sub Globals
  'These global variables will be redeclared each time the activity is created.
  'These variables can only be accessed from this module.
  Dim btnNext1, btnNext2 As Button
  Dim edtText1, edtText2 As EditText
  Dim rbtTest1, rbtTest2 As RadioButton
  Dim pnlLayout1, pnlLayout2 As Panel
  End Sub

Sub Activity_Create(FirstTime As Boolean)
  Activity.LoadLayout("TwoLayouts")
  pnlLayout1.Top=0
  pnlLayout1.Left=0
  pnlLayout1.Visible=True
  pnlLayout2.Top=0
  pnlLayout2.Left=0
  pnlLayout2.Visible=False
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

Sub btnNext1_Click
  pnlLayout1.Visible=False
  pnlLayout2.Visible=True
End Sub

Sub btnNext2_Click
  pnlLayout1.Visible=True
  pnlLayout2.Visible=False
End Sub

- TwoPanelLayouts
B4X:
Sub Globals
  'These global variables will be redeclared each time the activity is created.
  'These variables can only be accessed from this module.
  Dim btnNext1, btnNext2 As Button
  Dim edtText1, edtText2 As EditText
  Dim rbtTest1, rbtTest2 As RadioButton
  Dim pnlLayout1, pnlLayout2 As Panel
End Sub

Sub Activity_Create(FirstTime As Boolean)
  Activity.LoadLayout("TwoLayouts")
  pnlLayout1.Top=0
  pnlLayout1.Left=0
  pnlLayout1.LoadLayout("TwoLayouts1")
  pnlLayout1.Visible=True
  pnlLayout2.Top=0
  pnlLayout2.Left=0
  pnlLayout2.LoadLayout("TwoLayouts2")
  pnlLayout2.Visible=False
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

Sub btnNext1_Click
  pnlLayout1.Visible=False
  pnlLayout2.Visible=True
End Sub

Sub btnNext2_Click
  pnlLayout1.Visible=True
  pnlLayout2.Visible=False
End Sub

- TwoActivityLayouts
B4X:
Sub Globals
  'These global variables will be redeclared each time the activity is created.
  'These variables can only be accessed from this module.
  Dim btnNext1, btnNext2 As Button
  Dim edtText1, edtText2 As EditText
  Dim rbtTest1, rbtTest2 As RadioButton
End Sub

Sub Activity_Create(FirstTime As Boolean)
  Activity.LoadLayout("TwoLayouts1")
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

Sub btnNext1_Click
  Activity.RemoveAllViews
  Activity.LoadLayout("TwoLayouts2")
End Sub

Sub btnNext2_Click
  Activity.RemoveAllViews
  Activity.LoadLayout("TwoLayouts1")
End Sub

- TwoLayoutActivities
Main module
B4X:
Sub Globals
  'These global variables will be redeclared each time the activity is created.
  'These variables can only be accessed from this module.
  Dim btnNext1 As Button
  Dim edtText1 As EditText
  Dim rbtTest1 As RadioButton
End Sub

Sub Activity_Create(FirstTime As Boolean)
  Activity.LoadLayout("TwoLayouts1")
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

Sub btnNext1_Click
  StartActivity(Layout2)
End Sub
Layout2 module
B4X:
Sub Globals
  'These global variables will be redeclared each time the activity is created.
  'These variables can only be accessed from this module.
  Dim btnNext2 As Button
  Dim edtText2 As EditText
  Dim rbtTest2 As RadioButton
End Sub

Sub Activity_Create(FirstTime As Boolean)
  Activity.LoadLayout("TwoLayouts2")
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

Sub btnNext2_Click
  StartActivity(Main)
End Sub

Best regards.

EDIT: 2011_07_27
Updated files according to post #19

EDIT: 2015_03_07
Updated the zip files with B4A version 4.30
 

Attachments

  • TwoLayoutActivities.zip
    14.1 KB · Views: 2,013
  • TwoPanelActivity.zip
    12.4 KB · Views: 1,668
  • TwoPanelLayouts.zip
    14.5 KB · Views: 1,736
  • TwoActivityLayouts.zip
    8.7 KB · Views: 1,379
Last edited:

anOparator

Active Member
Licensed User
Longtime User
Great reading material right here, the article and the reply comments really help.
 

anormal

Member
Licensed User
Longtime User
Great tutorial, i was looking for the right way for doing the typical settings screen inside an example app i am testing and got here!
It's incredible de useful tutorials in the forum, it's easy to start B4A this way!

do you think using TwoLayoutActivities is the right way to code a settings screen?, then when i click in save settings, i return to the main screen

thanks!
 
Top