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: 1,679
  • TwoPanelActivity.zip
    12.4 KB · Views: 1,375
  • TwoPanelLayouts.zip
    14.5 KB · Views: 1,436
  • TwoActivityLayouts.zip
    8.7 KB · Views: 1,051
Last edited:

fabero

Member
Licensed User
Hi!

I use the TwoActivityLayouts example. I've an Activity and 2 layouts. Each layout had the horizontal and vertical variant.

But, If I'm vertical, and in second layout, when I rotate the phone to horizontal, Activity reload the first layout and don't remain to the second and use the horizontal variant..

How I can solve this?
 
Last edited:

dank

Member
Licensed User
Adding StateManager

Hi. I have an app with one activity and three layouts. It's based loosely on the SQL Example app. When the orientation changes, the first layout reappears with the selected row on my scrollview empty. If I have the second or third layout open, the reorientation also brings back the first layout. (Understandable since it goes through Activity_Create and _Resume again).

Is there a way to save the "data" on each layout and show the current one on an orientation change? Do I need to make each layout an activity instead? Any help will be appreciated!!
 

klaus

Expert
Licensed User
I would suggest you to use an Activity for each layout, that's the 'standard' way Android works.
When you change the orientation the current activity is displayed.

Best regards.
 

JaunLukePicard

Member
Licensed User
Relating to TwoPanelActivity

Klaus,

I downloaded and tried this example. I thought this would be close to what I was aiming for with a menu. Let me explain further,

I have an Activity that is an editor that loads a list of Stores. Once the list is loaded if I hit the Menu key on the device that a Layout file would be loaded which displays the menu choices. I decided to go this rout because I did not want to have way too many controls in the Designer. It can be hard to navigate.

So when you hit the Menu key the menu pops up and when you hit Back is disappears.

The stock popup menu doesn't seam to allow me to make the buttons appear on one line (i.e. Can I format them??)

I did see the library AHQuickAction, but that library didn't seam to be able to use buttons that I wanted unless I am missing something.

Any ideas? Thanks for the assistance...:sign0085:
 

JaunLukePicard

Member
Licensed User
A custom menu

Klaus,

I want to design a custom menu with the standard button Views. I thought I could use your twoPanelActivity to allow me to make a separate panel that I could add buttons to that I could invoke as needed.

I've seen examples where in the Designer I can hide a Panel, but the problem is that the views are still visible in the Designer cloging up what I see.

Does this help better explain what I am looking for?
 

klaus

Expert
Licensed User
Is the attached test program what you are looking for.
The program has a main layout (Designer).
A menu panel added by code to the main activity.
The content of the menu panel is a seperate layout (Designer).

Best regards.
 

Attachments

  • ManuPanel.zip
    7.5 KB · Views: 420

IslamQabel

Active Member
Licensed User
Dear Klaus:

Concerning Example Two Activity Layouts...( Main Activity, layout 1:TwoLayouts1,Layout2: TwoLayouts2)....when you move from the current layout to the second one....i see from code that you remove all views.....so this movement will cause loss of these views or not?...(In case of lose,do you need to add them again when you return to the first layout??? )

Thanks
 

klaus

Expert
Licensed User
..i see from code that you remove all views..
This is needed, because if you load a new layout it will be added to the previous one, if you don't want to show the previous layout you need to remove these views.
.so this movement will cause loss of these views or not?
Yes, if you want to display the first layout again you need to remove the current views and reload it.

The TwoActivityLayouts example is only to show the possibiliy.
It is not good practice to use this type of user interface.

You should either use :
- TwoLayoutActivities, this is the 'standard' way Android is designerd for. One activity for each layout.
- TwoPanelLayouts, this program uses two panels one for each layout and you manage the display by showing and hiding the panels.

Which one is better ?
There is no simple answer, it depends on the kind of program, the devices your target and what you want to show at the same time.
When Android ran only on smartphones the TwoLayoutActivities (multi-activities) method was the best .
Now with small and big screens (tablets) the TwoPanelLayouts (milti-panels) method can be useful because you can show two panels at the same time on tablets in landscape and one panel at the same time on the phones in portrait, this would be difficult to manage with different ativities. That's what I did in one of my latest project.
 

IslamQabel

Active Member
Licensed User
Dear Klaus....Thanks a lot for your reply.....Actually i will tell you about some issues:
  • My program is consist of two activities ("Main" with layout called "1" ,"activity2" with layout called "2")....I added ImageView objects in layout 1 and then load images for each object from my mobile...Then by click on a button ....these images are transferred to layout "2" ....the problem is i want to add more images in layout "1" but it is not enough to add more so i was thinking by sliding or by adding another layout....so How to do that?
  • This is important question....i would like to help me to know the Answer.....The philosophy of designing a program... if i want to buy my program in Google play for example, i must design my app to be compatible with all API levels.....and to do that.....should i make the target for the highest API level or what to do??? as well as screens of devices......
 

IslamQabel

Active Member
Licensed User
A question please....
Can i add ImageView object by designer to each layout related to each panel or it need a code???
 

klaus

Expert
Licensed User

IslamQabel

Active Member
Licensed User
Thanks for reply...
I want to add more objects in the layout related to Main activity.........for example i can put 10 images....if i want to add 30 images in the same layout, the layout will not enough to put them by designer ....understand me?? The user can slide the layout or click to show the the other images....
Understand me?
 

IslamQabel

Active Member
Licensed User
For the Flicker example,....the shown images are nine ....if i want to 20 or 30 ...in code i can declare more images Dim imageview10 as Imageview......till Dim ImageView40 As ImageView..... ..but how to show them on the screen when the programm is ruuning..?
 
Top