Android Tutorial Two activities example

An improved version, based on CallSubDelayed is available here.
This example demonstrates how to work with more than one activity and how to pass information between the activities.
In this example we have two activities. One activity is the "main" activity. When the user presses a button a "list" activity will be displayed. The user will choose an item from the list and then return to the main activity:

two_activities.png



It is recommended that you first read the Activities life cycle tutorial if you haven't read it before.

In order to add a new or existing activity to your project you should choose Project - Add New / Existing Module.

Variables declared in Sub Process_Globals are public variables and can be accessed from other activities. Therefore we will save the chosen value in such a variable.

When the user presses on the "choose item" button we open the second activity:
B4X:
Sub Button1_Click
    StartActivity(Activity2)
End Sub
When we open an activity the current one is first paused and then the target activity is resumed (and created if needed).
You can see it in the logs:

twoactivities_logcat.png


The second activity is pretty simple:
B4X:
Sub Process_Globals
    Dim result As String
    result = ""
End Sub

Sub Globals
    Dim ListView1 As ListView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("2")
    For i = 1 To 100
        ListView1.AddSingleLine("Item #" & i)
    Next
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub ListView1_ItemClick (Position As Int, Value As Object)
    result = value 'store the value in the process global object.
    StartActivity(Main) 'show the main activity again
End Sub
When the user presses on an item we store the value in the 'result' variable and we return to the main activity.

The main activity Resume event will be raised. So in this event we check if 'result' variable is not empty and change the label's text.
B4X:
Sub Activity_Resume
    If Activity2.result.Length > 0 Then
        Label1.Text = "You have chosen: " & Activity2.result
    Else
        Label1.Text = "Please press on the button and choose an item."
    End If
End Sub
In more complex applications with more than two activities you can use a process global variable in the main activity. Before starting an activity you can set this variable to some constant and then in Sub Activity_Resume check the value of this variable to know which activity was started and act accordingly.

The project is attached.
 

Attachments

  • TwoActivities.zip
    6.7 KB · Views: 5,964

margret

Well-Known Member
Licensed User
Longtime User
Panel.Layout

What do you have in Activity2 ? Do you really need it ?
Couldn't you work instead with Panels in the main Activity ?
It is possible to set a Layout file for each Panel and load them with Panel.Layout.
So you won't have the trouble when closing the application.

Best Regards.

Can you use the designer to create the panels file? If so, can you give a small code sample to load the panel layout file. How is the Panel file created if you can not use the designer?

Thanks,

Margret
 

klaus

Expert
Licensed User
Longtime User
Can you use the designer to create the panels file?
Yes.

If so, can you give a small code sample to load the panel layout file.
You can add the Panels either in the main layout of the activity or add the Panels in the code.

Loading a layout file into a panel is done with:
B4X:
Panel1.LoadLayout("FileName1")
Panel2.LoadLayout("FileName2")

Example:
Different examples with 2 layouts
TwoPanelLayouts

Best regards.
 

margret

Well-Known Member
Licensed User
Longtime User
Panel Layouts

Yes.

You can add the Panels either in the main layout of the activity or add the Panels in the code.

Loading a layout file into a panel is done with:
B4X:
Panel1.LoadLayout("FileName1")
Panel2.LoadLayout("FileName2")

Example:
Different examples with 2 layouts
TwoPanelLayouts

Best regards.

Hello

I have my .bal file I have created for my panel with my controls. In my main activity how would I open this file and display the layout. I have tried many things and none of them work. I looked at the samples and I must be just dumb. I don't want the panel to cover the complete screen, I have a small space around the edge. I want the panel just big enough to hold the controls.

From the main activity, I just need a panel of my selected size to popup that has my edittext, radiobuttons, checkbox, etc on it, then I want it gone when I hit save or cancel. I don't want to have to code this screen, I want it from the .bal file I have already created. Just a popup window with my controls. This is so simple in any other language I have programmed in. HELP....


Thanks,

Margret
 

kickaha

Well-Known Member
Licensed User
Longtime User
Some code to pop up a layout:
B4X:
   Dim pnlInput As Panel                ' This panel will contain the layout
   Dim pnlSelect As Panel               ' This panel will block input to the activity
   pnlSelect.Initialize ("Select")               ' we need a Select_Click event to ensure clicks do not go astray
   pnlInput.Initialize ("")
   pnlInput.LoadLayout ("YourBalFile")            ' load your layout into pnlInput
   pnlSelect.Color  = Colors.ARGB (150,0,0,0)         ' set colour and transparency so  pnlSelect dims app 
   Activity.AddView (pnlSelect, 0, 0,   100%x, 100%y)      ' fill screen with pnlSelect
   pnlSelect.AddView (pnlInput, 50dip, 150dip ,200dip, 200dip )   ' add pnlInput to pnlSelect (this will show your layout)   

' the values were chosen to roughly centre a 200 X 200 panel on the screen.
' They need adjusting for your layout size

You also need this sub in your activity
B4X:
Sub Select_Click ' Stop clicks on Select panel getting to panel underneath
End Sub

Then to exit the pop up and hide it simply
B4X:
pnlSelect.RemoveView
When you are finished with it.
 
Last edited:

margret

Well-Known Member
Licensed User
Longtime User
Panel View Popup

Then to exit the pop up and hide it simply
B4X:
pnlSelect.RemoveView
When you are finished with it.

I got this in and working all but the last part. The panel pops up and the controls work. However, the pnlSelect.RemoveView gives an error and says it is undefined. Here is my code:

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 pnlInput As Panel             
   Dim pnlSelect As Panel            
   Dim cancelweb, saveweb As Button
   Dim edittexturl As EditText
   Dim panelweb As Panel
End Sub

Sub WebSettings
   pnlSelect.Initialize ("Select")      
   pnlInput.Initialize ("")
   pnlInput.LoadLayout ("WebSet.bal")   
   pnlSelect.Color  = Colors.ARGB (150,0,0,0)         
   Activity.AddView (pnlSelect, 0, 0,   100%x, 100%y)   
   pnlSelect.AddView (pnlInput, 260dip, 100dip ,510dip, 340dip )    
End Sub

Sub CancelWeb_Click
   'Pressed the cancel button on panel form
   pnlSelect.RemoveView
End Sub

Sub Select_Click ' Stop clicks on Select panel getting to panel underneath
End Sub
HERE IS THE ERROR MESSAGE

Compiling code. Error
Error parsing program.
Error description: Undeclared variable 'pnlselect' is used before it was assigned any value.
Occurred on line: 361
pnlSelect.RemoveView
 
Last edited:

kickaha

Well-Known Member
Licensed User
Longtime User
:signOops: my mistake.
B4X:
Dim pnlSelect As Panel
Should be in the globals, so that other subs can see it.

Sorry, I see it is!.

So what is happening is CancelWeb_Click is being called before WebSettings. Put a breakpoint at the start of CancelWeb_Click and have a look at the value of pnlSelect.
 
Last edited:

kickaha

Well-Known Member
Licensed User
Longtime User
I just pasted your code into an empty project, and it compiled with no error message!

When you type pnlSelect, does it give you the autocomplete options? I can only think that there is an error somewhere else.
 

margret

Well-Known Member
Licensed User
Longtime User
Panel Issue

I just pasted your code into an empty project, and it compiled with no error message!

When you type pnlSelect, does it give you the autocomplete options? I can only think that there is an error somewhere else.

When I type pnlSelect I did not get the auto complete. I removed all the code from the project and saved the project. Then I reloaded the project and typed it in and did not use copy & paste this time. It now works and so does the auto complete. I had this issue once before on a c++ program. So some hidden/extended character code pasted in with the copy/paste.

Thanks so much for your help!

Margret
 

margret

Well-Known Member
Licensed User
Longtime User
Panel Issue

Very odd, but at least you have it working now.

I was running Hotkeys.com. It's an old DOS program that keeps anything you have ever typed at the CMD prompt in memory and allows you to edit the command entry list. I removed it as it may of had something to do with it. It was never meant to work with windows. I got it back around DOS 3.1! That's the only thing I could think that might have caused the issue.

Now another question. The active area that is left to display the panel has square corners and the panel, by default has round. The round looks nice and I would like to keep them but you have these little areas at each corner that looks bad when the panel displays. Plus it takes some extra effort to align and size the two, panel and viewport. If you popup a msgbox(), it's modal and turns off all the controls under it and the shaded area fits the corners. Why can't the panel work like msgbox(). Why can't you just set the panel modal and load from the .bal file and it handle the rest like the msgbox()? Or, is there a way to set the size of the msgbox() and to add controls to it, or am I asking for to much? Please remember I have no experience with Android, this is my first.

Thanks,

Margret
 
Last edited:

agraham

Expert
Licensed User
Longtime User
Android does not actually support modality. Basic4 android implements it's own form of modality for Msgbox. Look at my Dialogs library for a set of modal dialogs that use the Msgbox modality mechanism, including a couple of custom dialogs that accept a Panel that can contain other views. If you have not found it a list of additional libraries for use by registered users can be found here.
 

margret

Well-Known Member
Licensed User
Longtime User
Custom Dialogs

Android does not actually support modality. Basic4 android implements it's own form of modality for Msgbox. Look at my Dialogs library for a set of modal dialogs that use the Msgbox modality mechanism, including a couple of custom dialogs that accept a Panel that can contain other views. If you have not found it a list of additional libraries for use by registered users can be found here.

O.K. I downloaded and installed the libs. I ran the sample code and looked through it. Can you point me to any docs for what this library supports or is the code samples displaying the only options/parameters? I need to see if you can change font size, normal/bold, alignment, etc. The few samples I ran worked fine but the prompt text is far to small and I would like to center the input text in the field, etc.

These are really great. I have about 2500 lines of code already and I would like to match the look and size of what I already have. I am using 22point on font size. I am working on a project for one tablet brand with a 7" screen. Thanks for any help and what a GREAT job you have done for everyone!!


Thanks again,

Margret
 

agraham

Expert
Licensed User
Longtime User
Can you point me to any docs for what this library supports
Every library comes with an XML document that (more or less depending upon the author) documents it. The XML may beviewed with my off-line viewer or the same information can be seen online here.
 

margret

Well-Known Member
Licensed User
Longtime User
Panel Issues

Every library comes with an XML document that (more or less depending upon the author) documents it. The XML may beviewed with my off-line viewer or the same information can be seen online here.

Hello,

Is there any way in B4A to do macros? I would like to build the commands and put them into String variables and have B4A execute the variables as commands? Any ideas? Also, can you declare a variable public from a Sub?

Thanks,

Margret
 
Last edited:

agraham

Expert
Licensed User
Longtime User
Is there any way in B4A to do macros? I would like to build the commands and put them into String variables and have B4A execute the variables as commands?
Depending upon what you want your commands to do you might be able to use my BasicLib interpreter or, for expression evaluation only, it's ExpressionEvaluator subset.
 

margret

Well-Known Member
Licensed User
Longtime User
panel issue

Depending upon what you want your commands to do you might be able to use my BasicLib interpreter or, for expression evaluation only, it's ExpressionEvaluator subset.

I am wanting to get any panel height and width, so I need myvar.width to pull the data. Myvar can be anything that is passed to it. Is this something you think can be done?

Thanks again for all your great work!

Margret
 

margret

Well-Known Member
Licensed User
Longtime User
Panel Issue

Why can't you just do myvar.width if myvar contains a reference to the Panel you want? You can make myvar a Panel or a View variable and assign any Panel object to it.

O.K. Below is a sample of the code I want to write to popup panel dialogs that I have created with the designer. I don't want to have to center each one, try to set the view to match, etc. I started working on this and it works fine until I try to use a variable for the panel name I wish to work with. There are four places that the panelname needs macro substitution. If I use the real panel variable in those loactions it runs fine but it will have to be replicated for each layout. I want this code once and to use it for all panel layouts.

B4X:
Call the function with:

PanelPop("Panel's name", "Layout File.bal", 0, 0)
'Last two parameters are panel offsets to change screen posistion from center


Sub PanelPop ( panelname, mlayout, hc, wc )
   pnlSelect.Initialize ( "Select")      
   pnlInput.Initialize ("")
   pnlInput.LoadLayout ( mlayout )    
   pnlSelect.Color  = Colors.ARGB (150,0,0,0)         
   Activity.AddView (pnlSelect, 0, 0,   100%x, 100%y)   
   mh1=(pnlSelect.Height/2) - (panelname.Height/2) + hc
   mw1=pnlSelect.Width/2 - (panelname.Width/2) + wc
   mh2=panelname.Height
   mw2=panelname.Width
   pnlSelect.AddView (pnlInput, mw1, mh1, mw2, mh2)
   'Example: edittexturl.Text=HomePage
End Sub

Sub SaveWeb_Click
   'your code here for save clicked
   ToastMessageShow(" You Clicked Save " , False)
   pnlSelect.RemoveView
End Sub

Sub CancelWeb_Click
   'Pressed the cancel button on panel form
   pnlSelect.RemoveView
End Sub

Sub Select_Click ' Stop clicks on Select panel getting to panel underneath
End Sub
 
Last edited:

kickaha

Well-Known Member
Licensed User
Longtime User
You need to send the Panel (not its name) to your sub, for example if your panel is called pnlMyInput:
B4X:
PanelPop(pnlMyInput,"Layout File.bal", 0, 0)
'Last two parameters are panel offsets to change screen posistion


Sub PanelPop ( panelname, mlayout, hc, wc )
   pnlSelect.Initialize ( "Select")      
   pnlInput.Initialize ("")
   pnlInput.LoadLayout ( mlayout )    
   pnlSelect.Color  = Colors.ARGB (150,0,0,0)         
   Activity.AddView (pnlSelect, 0, 0,   100%x, 100%y)   
   mh1=(pnlSelect.Height/2) - (panelname.Height/2) + hc
   mw1=pnlSelect.Width/2 - (panelname.Width/2) + wc
   mh2=panelname.Height
   mw2=panelname.Width
   pnlSelect.AddView (pnlInput, mw1, mh1, mw2, mh2)
   'Example: edittexturl.Text=HomePage
End Sub
 
Top