Android Tutorial make your app more flexible AND/OR compatible with ios (b4i)

Here's a little sample skeleton for using classes vs activities and making your app more compatible with b4i ;))

It's more flexible (i.e. create multiple "pages" of the same form, a good example is spreadsheet pages), works better with a drawer and more open to multi-platform, at least in b4x. Of course this is not an exhaustive tutorial, but it should be good enough to get you thinking and started. Perhaps one day I'll add a complete demo app but for now it'll have to sit on the back burner with dozens of other items to do.
TFormClass:
B4X:
'Class module
Sub Class_Globals
    #if b4i
    #else if b4a
    Dim Parent As Panel
    Dim Owner As Object
    Dim Event As String
    #End IF
 
    Public Title As String="Your Title"
    #if b4i
    Public Form As Page 'this is the form to hold your layout
    #else if b4a
    Public Form As Panel 'this is the form to hold your layout
    #End IF
    Public Visible As Boolean 'just a place holder
    'views
    Private Button1 As Button 'within layout file
End Sub

'Initializes the object. You can add parameters to this method if needed.
#IF b4a
Public Sub Initialize(AEvent As String, AParent As Panel, ATargetModule As Object) 'add more if you want/need to
    Event=AEvent
    Parent=AParent
    Owner=ATargetModule
    Form.Initialize("Form")
    Parent.AddView(Form,0,0,Parent.Width,Parent.Height)
    Form.LoadLayout("ALayout")
    'seen to need this to make the form/panel opaque
    Dim cd As ColorDrawable
    cd.Initialize2(Colors.ARGB(255,0,0,0),0,0,0)
    Form.Background=cd
    Form.Visible=False
#ELSE IF #b4i 'this is just a quick example of multi platform
Public Sub Initialize 'add more if you want/need to
    Form.Initialize("Form")
    Form.RootPanel.LoadLayout("ALayout") 
    form.pagetitle=title
#END IF
    'do any other initialization required
End Sub

Sub getVisible As Boolean
    Form.Visible
End Sub

Sub setVisible(value As Boolean)
    Form.Visible=value
    If value Then
        #IF b4a
        Form.BringToFront 'just in case
        #ELSE IF b4i
        #END IF
    End If
    'process anything else we want to do
End Sub

#if b4i
sub Form_Appear
    'process anything else we want to do
end sub
#end if

Sub getTitle As String
    Return Title
End Sub

'any view events or other methods for this particular "activity"
Sub BUtton1_Click
    'do something
End Sub
And here is our Activity/Main code:
B4X:
'Activity/Main
#IF b4a
Sub Globals
    Dim ActiveForm As Object 'to hold our current visible form
    Dim FormClass As TFormClass
    Dim Page As Panel 'propably in the main layout, otherwise intialize and add to activity
        'If using a drawer Then assign the drawer's content panel to this one
End Sub
#END IF

Sub Process_Globals
    Dim Pages As List 'could use a list to hold our forms if we want to
    Pages.Initialize
#ELSE IF b4i
    Dim ActiveForm As Object 'to hold our current visible form
    Dim FormClass As TFormClass
#END IF
End Sub

#if b4a
Sub Activity_Create(FirstTime As Boolean)
    'not totally sure yet If we can Do this within FirstTime
    Activity.LoadLayout("Layout")
    pages.clear
    FormClass.Initialize("FormClass",Page,Me)
#ELSE IF b4i
Private Sub Application_Start (Nav As NavigationController)
    NavControl = Nav
    pages.clear 'probably not needed
#END IF
    FormClass.Initialize
    pages.add(formclass)
End Sub

'call this when you want To show a particular Form
Sub ShowPage(value As Object)
#IF b4a
    'could create a dublicate b4i Navigation class To handle all our page related stuff
    Try'in case our activeform is empty
        'hide any Visible one
        CallSub2(ActiveForm,"setVisible",False)
    Catch
    End Try
    ActiveForm=value
    'If we have a toolbar Or action bar Then
    'toolbar.title=CallSub(ActiveForm,"getTitle")'we use callsub since we are using a generic object type
    CallSub2(ActiveForm,"setVisible",True)
#ELSE IF b4i
    ActiveForm=value
    NavControl.ShowPage(ActiveForm)
#END IF
End Sub
 
Last edited:

imbault

Well-Known Member
Licensed User
Longtime User
Pretty good exercise, thank you, but not easy to read source code...
 

qsrtech

Active Member
Licensed User
Longtime User
Unfortunately that's what happens when you do multi-platform coding. But i did comment it more than usual ;)
 

klaus

Expert
Licensed User
Longtime User
Interesting, but as imbault said, not easy to read nor easy to maintain.
The questions is: what is easier to have one project with a lot of #IF b4a which is difficult to read or two projects.
I don't have the answer, never tried the exercise.
I did this kind of exrecise, an easier one, between two B4A projects one for smartphones and the other one for tablets merging both projects into one. At the beginning I wrote only a tablet version and then I wanted it too an a smartphone. So I wrote a smartphone version. But having 90 or 95% of the code beeing the same I found it being not efficient to maintain two projects. At the end it easn't that difficult even having different layout files for tablets and smartphones.
Well, so between two operating systems, interesting.
Good luck:) !
 

sorex

Expert
Licensed User
Longtime User
The questions is: what is easier to have one project with a lot of #IF b4a which is difficult to read or two projects.

it depends on the project ofcourse.

when doing a game I write all game logic, views setup etc in a shared module.

when it works on Android it's then a lot easier to integrate it into an IOS project as you just need to add the minor changes that are between Android & IOS.

the good part is that when you change something in the gamelogic it applies for both versions.

with seperated versions you will simply forget to update something that was already done in the other.

and indeed, what is b41 used for?
 

sorex

Expert
Licensed User
Longtime User
right, that's what I use. move as much to a shared module which can apply for none game apps aswell.

you could copy paste it aswell but then you have that issue that I explained earlier that updates aren't 100% matches.
 

qsrtech

Active Member
Licensed User
Longtime User
With games I understand.
Another approch or combination could be shared Code Modules.

I suppose that it's a typo, I think it should b4j.
lol
It was to see if anyone is actually paying attention ;)
 

qsrtech

Active Member
Licensed User
Longtime User
right, that's what I use. move as much to a shared module which can apply for none game apps aswell.

you could copy paste it aswell but then you have that issue that I explained earlier that updates aren't 100% matches.
and now you see what happens when you copy and paste lol
 
Top