Android Tutorial B4A Beginners Guide

This guide is obsolete ! It is no more updated.
Please use the B4X Booklets.


Here is a Beginner's Guide in pdf format.
The source code for the example programs is included.

All suggestions, remarks and typo reports are welcome.

To know what vesion you have, look at the lower right corner of the cover page there is an Edit number.
Latest version Edit 3.3

Best regards.


EDIT 2016.10.26 Edition 3.3
Updated with the new B4A V6.30 features.
A big thank you to widget who sent me a complete review with suggestions and typos.
Almost all suggestions have been added.

EDIT 2015.12.08 Edition 3.2
Updated with the new B4A V5.50 features.

EDIT 2015.10.14 Edition 3.1
Updated with the new B4A V5.20 features.

EDIT 2015.07.26 Edition 3.0
Updated with the new B4A V5.02 features.

EDIT 2014.06.04 Edition 2.9
Updated with the new B4A V3.80 features.
Amended some typos.

EDIT 2014.05.07 Edition 2.8
Updated with the new B4A V3.50 features.
Amended some typos.

EDIT 2014.02.05 Edition 2.7
Updated with the new B4A V3.20 features.

EDIT 2013.10.13 Edition 2.6
Updated with the new B4A V3.00 features.

EDIT 2013.05.19 Edition 2.5
Updated with more recent Android SDKs.
Updated to Basic4Android Version 2.70
Added some code snippets to B4A Code Snippets.

EDIT 2013.01.20 Edition 2.4
Minor update with the changes of B4A Version 2.5

EDIT 2012.12.27 Edition 2.3
Updated to Basic4Android 2.3
Updated some chapters

EDIT 2012.08.30 Edition 2.2
- Updated to B4A 2.02
- Removed chapters Keywords, Views and Collections moved them in a separate guide Keyword_Views.pdf
- Added Chapter 12. Tools
- Chapter 12 Example Programs becomes 13
- Chapter 13 Basic Language becomes 14
- Chapter 14 Basic Graphics / Drawing becomes 15
- Chapter 15 Keywords removed
- Chapter 16 Views removed
- Chapter 17 Collections rmoved
- Chapter 18 VB6 versus B4A becomes 16
- Chapter 19 FAQ's becomes 17
- Chapter 20 Glossary becomes 18
- Added new chapters to FAQ'schapter
- Chapter 17.39 not yet finished

Two new Guides have been added:
- Keyword / Views / Collections these chapters were in the Beginner's Guide before
- B4A Code Snippets, it's a collection of code snippets I took from the forum.

EDIT 2012.04.11 Edition 2.1
- Added chapter 5.3 Screen Orientations
- Added chapter 6.4 Exchanging files with PC
- Added chapter 8.9 Designer Script
- Added chapter 13.5.3 Calling a Sub from another module
- Added chapter 13.11 Lists
- Added chapter 13.12 Maps
- Added chapter 17 Collections
- Chapter 17 becomes 18
- Chapter 18 becomes 19
- Added chapter 19.25 Detect screen orientation
- Added chapter 19.26 Some functions don't work in Activity_Pause
- Added chapter 19.27 Calling internal Calculator
- Added chapter 19.28 Get the Alph / Red / Green / Blue values

EDIT 2012.03.11 Edition 2.0
- Amended some errors
- Updated to B4A version 1.8
New chapter
- Working with different screen sizes / number of layouts

Removed chapters
- SQL moved to the User's Guide
- GPS moved to the User's Guide

EDIT 2012.01.01 Edition 1.6
- Added the Widget tutorials.
- Amended some errors.

EDIT 2011.12.25 Edition 1.5
- Added the GPS Example program with some explanations of the code.
- Added some subchapters inspired by forum questions.

Edit: 2011.11.01 Version 1.4
New chapters:
- 8.7 The Abstract Designer
- 8.8 Adding views by code
- 12.5 GPS (a new GPS program will be added in the next update)
- 13.7 Basic Language / Events
- 13.10 Basic Language / Files
- 18.xx some new FAQ's

Edit: 2011.08.29 Version 1.3
New chapter:
- 18 FAQ's

Edit: 2011.08.15 Version 1.2
Above chapters have been added.

Edit: 2011.06.12 Version 1.1
Still missing chapters:
Modules
Example Programs
- ScrollView example
- Database example
Basic Language
- String manipulations
- Timers
Graphics
 
Last edited:

klaus

Expert
Licensed User
Longtime User
Do you belive this thing is doable?
Yes !
There alraedy do exist some example programs doing what you want to do, perhahps not the synchronisation with the PC.
The example I am preparing for the Beginner's guide will almost do it (without the synchronisation).

Bestr regards.
 

RandomCoder

Well-Known Member
Licensed User
Longtime User
Great Manual

Thank you Klaus for creating such a detailed guide along with so many screen shots and sample source code, it must have taken a considerable amount of your time. This really should be part of the B4A installation and made readily available from the help menu as it is invaluable to anyone that is just starting out.
I’ve been using B4PPC for almost 5 years and whilst B4A looks familiar it is also strangely different. One of the things I liked with B4PPC was how easy it was to get help on specific keywords and this often contained an example usage. B4A just feels like it is geared towards a slightly more advanced programmer, for example in my first app I require a couple of ImageButtons but the keyword help for Button suggests two members relating to the button background....
Background As android.graphics.drawable.Drawable and SetBackgroundImage(Bitmap As android.graphics.Bitmap)
It also suggests that if changing the Button background then StateListDrawable is preferable as it offers two different backgrounds, one for default and one for when pressed.

I’ve had a little play and got an image to work using....
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("frmLoneWorker")
   bmpHand.Initialize( File.DirAssets , "Hand.png" )
   btnAcknowledge.SetBackgroundImage( bmpHand )
End Sub
But I still had no idea what the Background member was expecting to be given because android.graphics.drawable.Drawable might as well be written in Swahili for all the help it is in telling me what syntax to use.
Thankfully the Forum is still an excellent place to find the answers and in this post http://www.b4x.com/forum/41047-post13.html (also by you) it is clearly explained.

I also find it strange that what used to be a Form is now an Activity but whereas each Form had to have a unique identifier this does not appear to be so with Activities? I see from the ThreeActivityExample that when an activity is called all references to Activity.(dot) must relate to the Activity that has the focus and if I’ve interpreted the manual correctly any previous Activities although still present in the background could potentially get removed to free up memory. Have I understood correctly, as this is in direct contrast to B4PPC where the main Form could open several other Forms but if the main Form was closed then all other Forms would close with it?
I noticed a similar issue on Page 101 of the manual, relating to Casting. In the second example all the Buttons are named Btn....
B4X:
Sub Activity_Create(FirstTime As Boolean)
For i = 0 To 9
   Dim btn As Button
   btn.Initialize("btn")
   activity.AddView(btn,10dip,10dip+60dip*i,200dip,50dip)
Next
End Sub
I would have expected this not to work because each Button is not uniquely identified and yet it does, but how?
Anyway, sorry for the long winded reply and a very BIG THANK YOU for the great work you have put into this manual. I’ll now get stuck into using B4A and see what new problems I find.

W.R.T typo’s, the manual is exceptionally well written but I have found a couple of errors you may wish to correct...
P.85 Para heading “Name” – “lblResult”, “lbl” for Editext and “Result” for purpose.
P.99 Both examples to access all items in an array – For i = on all three nested statements instead of i then j then k.
P.129 Second line – declared in the directly in the list

Kind regards,
RandomCoder
 

klaus

Expert
Licensed User
Longtime User
Hi RandomCoder,
Welcome in the Android 'world' and thank you for your nice comments.
I am glad that you find it usefull.

At a first look, B4PPC and B4A seem beeing quite similar.
Yes they are, the basic language is almost the same, there are just a few differences.
No they are not, because the operating system is not the same there are big differences for all the user interace stuff.

Forms don't exist like in WM.
Android uses Activities, and the most important is to understand their life cycle. There is only one active Activity at the same time.

Objects are called Views.
Coming to your question about the buttons having the same name, the Android OS doesn't know the names of the views but knows an internal handler. B4Android does it for you when all names are different.
Different Views can have the same event handling routine, and to know what view raised the event we use the Sender keyword like in B4PPC. The difference in B4A is that Sender doesn't return the name of the View but the view as an object. The only way, in B4A, to know what View has raised the event is the Tag parameter. The example on page 101 doesn't show the Tag parameter principle. Have a look at the code in the Second program there is also a panel with buttons, there they are added in the Designer but they could have been added by code with the same name. In the example they have different names but the important parameter is their Tag parameter. The event handling is explained on pages 39-40.
There is no difference between a Button and an ImageButton like in B4PPC. An Android Button can have several background settings, as you already saw. It is relatively easy to define these parameters in the Designer, but in the code it's somewhat more complicated. That's why I didn't add this in the Beginner's Guide.

Thank you for the typo report, they are amended in my original file.
An update of the Beginner's Guide will come quite soon.

Best regards.
 

RandomCoder

Well-Known Member
Licensed User
Longtime User
Coming to your question about the buttons having the same name, the Android OS doesn't know the names of the views but knows an internal handler. B4Android does it for you

Thanks for the explanation.
I feel it is important to understand the fundamentals before getting to stuck into the programming :sign0098:

Regards,
RandomCoder
 

klaus

Expert
Licensed User
Longtime User
A new update of the Beginner's Guide is available, version 1.2.
Added chapters:
- Modules
- Example Programs
- ScrollView example
- Database example
- Basic Language
- String manipulations
- Timers
- Graphics

- Chapter numbers on top of each page.

The source codes of the example programs are included.

As usual, suggestions, wishes, typo and bug reports are welcome.

Best regards.
 

Brad

Active Member
Licensed User
Longtime User
Edit: Placed in wrong thread...moved to Tips
 
Last edited:

bluejay

Active Member
Licensed User
Longtime User
Thank you Klaus for continuing to update this. This is excellent work.
:sign0142:
 

Hubert Brandel

Active Member
Licensed User
Longtime User
Hi,

thanks for this excellent work, easy to understand and exactly what a Basic4Android newby (like me) needs.

Here I found one thing you might want to change by time ...
In the first Example (MyFirstProgram) on Page 27 you define the "Number" Vars as double, but the program just need INT.
On my example the double Vars looks like 5.0 instead of 5 like your screenshot.
Thirst I thought I have made a mistake in the code, than if there is a formating style in the lables, but then I changed the line 19:

Old: Dim Number1, Number2 As Double
New: Dim Number1, Number2 As Int

And my program behave like yours. And now to the second one :sign0060:

Regards,
Hubert
 
Last edited:

Hubert Brandel

Active Member
Licensed User
Longtime User
Hi Klaus,

I think I can offer another one ;-)

I have a Samsung Galaxy S+ with 480x800-240. In the examples with the menus (UserInterfaceButtonToolbox.b4a, UserInterfaceMenu.b4a, UserInterfaceTabHost.b4a and ThreeActivityExample.b4a) the height of the tabpage is calculated like this:

B4X:
Sub Activity_Create(FirstTime As Boolean)
   ...
   Activity.LoadLayout("Main")    ' Loads "Main" layout file
   ...   
   PanelHeight = Activity.Height - pnlToolbox.Height - 5dip

   pnlPage1.Initialize("")       ' Initializes pnlPage1
   pnlPage1.LoadLayout("Page1")   ' Loads "Page1" layout file
   Activity.AddView(pnlPage1,0,0,100%x,PanelHeight)

the heigth and the TOP of pnlToolbox is defined in the designer.
But my display is higher and so the buttons will be under the TabPanels.

I think there TOP should be calculated too.

B4X:
Sub Activity_Create(FirstTime As Boolean)
   ...
   Activity.LoadLayout("Main")    ' Loads "Main" layout file
   ...   
   PanelHeight = Activity.Height - pnlToolbox.Height - 5dip
   pnlToolbox.Top = Activity.Height - pnlToolbox.Height

   pnlPage1.Initialize("")       ' Initializes pnlPage1
   pnlPage1.LoadLayout("Page1")   ' Loads "Page1" layout file
   Activity.AddView(pnlPage1,0,0,100%x,PanelHeight)

In this way I can see und push them.

Regards,
Hubert
 
Last edited:

klaus

Expert
Licensed User
Longtime User
Hi Hubert,
Thank you for reporting.
You are right, the top property of the ToolBox must also be calculated.
UserInterfaceButtonToolbox.b4a and ThreeActivityExample.b4a will be updated in the next version.
UserInterfaceMenu.b4a and UserInterfaceTabHost.b4a no need.

Best regards.
 

zouriteman

Member
Licensed User
Longtime User
DIP or DIPP

KLAUS ,
in the above post #58 from HUBERT BRADEL , he write
5dipp

it's an error , or a new variant of 5dip ?? :BangHead:
 
Top