Beginner's Guide
Last update : 2013.01.19
Copyright: © 2011, 2012, 2013 Anywhere Software Edition 2.4
Table of contents 2 Basic4Android Beginner's Guide
1 Getting started.........................................................................................................................7
1.1 Trial version ....................................................................................................................8
1.2 Installing Basic4Android and Android SDK ....................................................................9
1.2.1 Install and configure Basic4Android ......................................................................11
1.3 Installing Android Emulator...........................................................................................12
2 My first program (MyFirstProgram.b4a)...............................................................................17
3 Second program (SecondProgram.b4a).................................................................................37
4 The IDE ................................................................................................................................51
4.1 Menu and Toolbar..........................................................................................................52
4.1.1 Toolbar ..................................................................................................................52
4.1.2 File menu...............................................................................................................53
4.1.3 Edit menu...............................................................................................................53
4.1.4 Project menu..........................................................................................................54
4.1.5 Tools menu ............................................................................................................54
4.2 Code area.......................................................................................................................57
4.2.1 Undo – Redo .............................................................................................58
4.2.2 Collapse a subroutine.............................................................................................58
4.2.3 Collapse a Region ..................................................................................................59
4.2.4 Collapse the whole code.........................................................................................60
4.2.5 Commenting and uncommenting code .....................................................61
4.2.6 Bookmarks .................................................................................61
4.2.7 Indentation ...............................................................................................62
4.2.8 Autocomplete .................................................................................................64
4.2.9 Built in documentation...........................................................................................66
4.2.10 Jump to a subroutine ..............................................................................................67
4.2.11 Highlighting occurrences of words.........................................................................68
4.2.12 Debugging .............................................................................................................69
4.2.13 Breakpoints............................................................................................................71
4.3 Tabs...............................................................................................................................74
4.3.1 Module and subroutine lists ...............................................................74
4.3.2 Files .......................................................................................................75
4.3.3 Logs ......................................................................................................76
4.3.4 Libs ........................................................................................................77
5 Screen sizes and resolutions...................................................................................................78
5.1 Special functions like 50%x, 50dip ..............................................................................83
5.1.1 PerXToCurrent, PerYToCurrent - 50%x.................................................................83
5.1.2 DipToCurrent - 50dip.............................................................................................83
5.2 Working with different screen sizes / number of layouts ................................................84
5.3 Screen orientations.........................................................................................................90
5.4 Supporting multiple sceens - tips and best practices .......................................................91
5.4.1 How to change the views size and text size? AutoScale........................................92
6 The Emulator.........................................................................................................................93
6.1 Launch an Emulator.......................................................................................................93
6.2 Create a new Emulator...................................................................................................95
6.3 Emulator problems.........................................................................................................98
6.3.1 Process timeout......................................................................................................99
6.4 Exchanging files with the PC....................................................................................... 100
7 Connecting a real device USB / B4A Bridge.................................................................... 103
7.1 USB............................................................................................................................. 103
Table of contents 3 Basic4Android Beginner's Guide
7.2 B4A Bridge .................................................................................................................104
7.2.1 First you need to install B4A-Bridge on your device. ...........................................104
7.2.2 Run B4A-Bridge on your device. .........................................................................105
7.2.3 Connect the IDE to the device.............................................................................. 105
8 The Designer....................................................................................................................... 109
8.1 The menu..................................................................................................................... 110
8.1.1 File menu.............................................................................................................110
8.1.2 AddView menu....................................................................................................110
8.1.3 The Tools menu ................................................................................................... 111
8.2 Tools ...........................................................................................................................112
8.2.1 Generate Members............................................................................................... 112
8.2.2 BringTo Front......................................................................................................113
8.2.3 Send To Back.......................................................................................................113
8.2.4 Duplicate Selected View ......................................................................................113
8.2.5 Remove Selected View ........................................................................................114
8.2.6 Change grid.......................................................................................................... 114
8.2.7 Connect device or emulator.................................................................................. 115
8.3 General settings ........................................................................................................... 115
8.4 Image files...................................................................................................................116
8.5 Properties list...............................................................................................................117
8.6 Layout variants ............................................................................................................ 119
8.7 The Abstract Designer ................................................................................................. 124
8.7.1 The menu.............................................................................................................125
8.7.2 Context menus .....................................................................................................126
8.7.3 Select views.........................................................................................................127
8.7.4 Example............................................................................................................... 128
8.8 Adding views by code.................................................................................................. 131
8.9 Designer Scripts........................................................................................................... 134
8.10 AutoScale .................................................................................................................... 140
8.10.1 Simple AutoScale example with only one layout variant ......................................141
8.10.2 Same AutoScale example with portrait and lansdscape layout variants................. 146
8.10.3 AutoScale more advanced examples.....................................................................149
8.11 UI Cloud......................................................................................................................153
9 Process and Activity life cycle.............................................................................................155
9.1 How do we handle it ? .................................................................................................156
9.2 Process global variables............................................................................................... 157
9.3 Activity variables.........................................................................................................157
9.4 Sub Activity_Create (FirstTime As Boolean)...............................................................157
9.5 Globals versus FirstTime ............................................................................................. 158
9.6 Sub Activity_Resume Sub Activity_Pause (UserClosed As Boolean) .......................... 158
9.7 Activity.Finish / ExitApplication ...............................................................................159
10 Variables and objects.......................................................................................................160
10.1 Variable Types.............................................................................................................160
10.2 Names of variables ...................................................................................................... 162
10.3 Declaring variables...................................................................................................... 162
10.3.1 Simple variables...................................................................................................162
10.3.2 Array variables.....................................................................................................163
10.3.3 Array of views (objects).......................................................................................165
10.3.4 Type variables...................................................................................................... 166
10.4 Casting ........................................................................................................................167
10.5 Scope...........................................................................................................................168
10.5.1 Process variables..................................................................................................168
10.5.2 Activity variables................................................................................................. 168
Table of contents 4 Basic4Android Beginner's Guide
10.5.3 Local variables.....................................................................................................168
10.6 Tips .............................................................................................................................169
11 Modules...........................................................................................................................170
11.1 Activity modules..........................................................................................................171
11.2 Class modules.............................................................................................................. 172
11.3 Code modules.............................................................................................................. 173
11.4 Service modules........................................................................................................... 174
12 Tools ............................................................................................................................... 177
12.1 Search function in the forum........................................................................................ 177
12.2 The B4A Documentation Wiki.....................................................................................178
12.3 B4AHelp ..................................................................................................................... 179
13 Example programs...........................................................................................................182
13.1 User interfaces............................................................................................................. 182
13.1.1 Menu example (UserInterfaceMenu.b4a)............................................................. 183
13.1.2 TabHost example (UserInterfaceTabHost.b4a)................................................... 184
13.1.3 Button toolbox example (UserInterfaceButtonToolbox.b4a)................................185
13.2 Program with 3 Activities (ThreeActivityExample.b4a)..............................................186
13.3 ScrollView examples ...................................................................................................193
13.3.1 ScrollView example program...............................................................................194
14 Basic language................................................................................................................. 205
14.1 Program flow............................................................................................................... 205
14.1.1 Process_Globals routine.......................................................................................205
14.1.2 Globals routine.....................................................................................................206
14.1.3 Activity_Create (FirstTime As Boolean) routine ..................................................206
14.1.4 Activity_Resume routine...................................................................................... 206
14.1.5 Activity_Pause (UserClosed As Boolean) routine................................................. 206
14.2 Expressions.................................................................................................................. 208
14.2.1 Mathematical expressions .................................................................................... 208
14.2.2 Relational expressions.......................................................................................... 209
14.2.3 Boolean expressions............................................................................................. 209
14.3 Conditional statements.................................................................................................210
14.3.1 If – Then – End If................................................................................................. 210
14.3.2 Select – Case........................................................................................................ 212
14.4 Loop structures............................................................................................................ 214
14.4.1 For – Next............................................................................................................ 214
14.4.2 For - Each............................................................................................................ 215
14.4.3 Do - Loop ............................................................................................................ 216
14.5 Subs............................................................................................................................. 218
14.5.1 Declaring ............................................................................................................. 218
14.5.2 Calling a Sub........................................................................................................218
14.5.3 Calling a Sub from another module......................................................................218
14.5.4 Naming................................................................................................................ 219
14.5.5 Parameters ........................................................................................................... 219
14.5.6 Returned value..................................................................................................... 219
14.6 Events.......................................................................................................................... 220
14.7 Libraries ...................................................................................................................... 223
14.7.1 Standard libraries ................................................................................................. 223
14.7.2 Additional libraries folder ....................................................................................223
14.7.3 Error message "Are you missing a library reference?".......................................... 224
14.8 String manipulation ..................................................................................................... 225
14.9 Timers .........................................................................................................................226
14.10 Files......................................................................................................................... 227
14.10.1 File object ........................................................................................................227
Table of contents 5 Basic4Android Beginner's Guide
14.10.2 Filenames......................................................................................................... 229
14.10.3 Subfolders........................................................................................................ 229
14.10.4 TextWriter........................................................................................................230
14.10.5 TextReader....................................................................................................... 231
14.10.6 Text encoding................................................................................................... 232
14.11 Lists......................................................................................................................... 234
14.12 Maps........................................................................................................................ 236
15 Graphics / Drawing.......................................................................................................... 238
15.1 Overview.....................................................................................................................238
15.2 Drawing test programs................................................................................................. 240
15.2.1 First steps............................................................................................................. 240
15.2.2 Drawing rotating bitmaps / RotatingNeedle.......................................................... 245
15.2.3 Simple draw functions.......................................................................................... 250
16 VB6 versus B4A..............................................................................................................259
17 FAQ ................................................................................................................................ 264
17.1 "Please save project first" message.............................................................................264
17.2 "Are you missing a library reference" message...........................................................264
17.3 How loading / updating a library..................................................................................265
17.4 When do we need to 'Initialize' and when not............................................................. 265
17.5 Split a long line into two or more lines.........................................................................266
17.6 Avoid closing an application / capture keycodes like Back / Menu...............................266
17.7 Unwanted events like Click, Touch or others ............................................................... 267
17.8 Adding a Menu item .................................................................................................... 267
17.9 How do I remove a View with the Designer.................................................................268
17.10 "Process has timeout" message............................................................................... 268
17.11 Getting a picture from the gallery.............................................................................269
17.12 How to delete x.bal files or other files from a project .............................................270
17.13 Block a screen orientation........................................................................................ 271
17.14 Close second Activity ..............................................................................................272
17.15 Taking a screenshot programaticaly .........................................................................272
17.16 After compiling, where are the files .........................................................................272
17.17 Run an application from another one........................................................................ 273
17.18 How to pass an Array to a Sub .................................................................................273
17.19 Getting language and country from device ............................................................... 274
17.20 Where is the apk file ................................................................................................274
17.21 Why is my apk filename result.apk .......................................................................... 274
17.22 Why is my apk filename xxx_DEBUG.apk..............................................................275
17.23 Select True / Case trick ............................................................................................275
17.24 Fill an array with random numbers without repetition .............................................. 275
17.25 Detect screen orientation..........................................................................................276
17.26 Some functions don't work in Activity_Pause...........................................................276
17.27 Calling the internal Calculator..................................................................................276
17.28 Get the Alpha / Red / Green / Blue...........................................................................277
17.29 Get device type ........................................................................................................ 277
17.30 Generate a Click event ............................................................................................. 277
17.31 "Out of memory" Error / Bitmaps.............................................................................278
17.32 Remove the scrollbar from a ScrollView..................................................................278
17.33 Check if directory exists...........................................................................................278
17.34 Set Full Screen in code............................................................................................. 278
17.35 Change EditText input modes .................................................................................. 279
17.36 Sorting a file list according to last modified time......................................................281
17.37 Get the dpi values of the device (dots per inch) ........................................................ 281
17.38 Finding java program lines.......................................................................................282