Write and sell Android apps with Basic 4 Android: Part 1
Now you’re ready to try compiling and running the sample program. Hit the blue “Run” icon on the toolbar (or select Compile & Run from the Project menu).
The first time you do this you’ll be prompted to provide a filename, and then a Package Name – a unique identifier for your app, which is structured as a sort of backwards URL, such as uk.co.pcpro.ourfirstapp. It isn’t necessary to actually register this as a real subdomain; the idea is simply to come up with an identifier that’s guaranteed not to clash with an existing one. For more details, see Oracle’s official documentation on package names.
Finally you’ll be asked for an application label – the name that will appear below the app’s icon in the Android app menu.
The program should now compile, and B4A will try to install it to a connected Android device, so make sure your AVD is up and running. You can also test on an actual smartphone or tablet if you enable Debugging mode on the device – see the B4A documentation for more information about this.
If all is well, you should see the message box appear on the screen of your AVD. Click on it and it will close. Congratulations! You’ve compiled and run your first Android app. Next time you run it, all of the settings you just entered will be remembered, so it will compile and run straight away. If you want to change the package name or application label, you’ll find the options under the Project menu.
Note that Android apps don’t shut down automatically (unless you write code to make them do so), so if you’re running in Debug mode you’ll have to stop execution by clicking the green Stop button in the debug panel at the bottom of the IDE (see Debug mode, below).
If you’re using the full version of B4A, the process is much the same, except that the first time you open the IDE you’ll be prompted to register. One user is permitted to run B4A across two computers, but be warned that the licence file is deleted after registration, so keep a copy of it somewhere safe.
You’ll also notice that the full version of B4A opens with an empty program framework. If you want to test that everything is working as above, add a single line of code so that lines 14-16 of the framework read as follows:
-
Sub Activity_Create(FirstTime As Boolean)
-
Msgbox(“This is my first Android app!”,”Information”)
-
End Sub
As you type, your code is automatically colour-coded and capitalised to help you follow what’s going on. Once you’ve entered this single line, you can compile and run the program, and click on the message box (you’ll see the second string, “Information”, is its title) to see that everything is working as it should.

The structure of a B4A program
As we’ve mentioned, B4A is very similar to VB. Programs don’t proceed linearly from the first line to the last; instead, they’re event driven. You build up an app by creating standalone sections of code to handle specific events, such as when a particular button is pressed. These sections of code are called “subs”, just as they are in VB.
The framework that opens when you first open B4A is pre-populated with several subs. The one containing the code that makes the message box appear is called “Activity_Create” (don’t worry about the part in brackets for the moment). “Activity” is Android jargon, referring to the application’s full-screen graphical interface. The suffix “_Create” specifies that this sub is triggered when the activity first appears. In other words, code placed here will execute automatically when the program runs.
You can write your own subs to handle other events. As an example, let’s write some code to place a button onscreen, and a sub that will be called when it’s clicked. We’ll start by editing Activity_Create so it reads as follows:
-
Sub Activity_Create(FirstTime As Boolean)
-
Dim myButton As Button
-
myButton.Initialize(“myButton”)
-
myButton.Text=”Click here to open the message box”
-
Activity.Addview(myButton,20,100,440,60)
-
End Sub
B4A uses familiar object-oriented syntax, with objects having methods and properties that can be accessed by appending their names to the object name. As you’ll have noticed as you typed in the above code, when you type a dot, a dropdown menu appears showing valid keywords; when you type an open bracket, you get a helpful pop-up reminder of the required syntax.
Read more: Write and sell Android apps with Basic 4 Android: Part 1 | Analysis | Features | PC Pro http://www.pcpro.co.uk/features/380845/write-and-sell-android-apps-with-basic-4-android-part-1/2#ixzz2SMPxmtvz