You are on page 1of 114

SPCF MIS Department@2014

Basic4Android
Training Workshop
Mr. Marlon I. Tayag, CCNA,CCAI,Apple Cert. Trainer
MIS Director
SPCF MIS Department@2014
Android Programming
The Android operating system is
widely used in:
Cell phones
Tablets
Other mobile devices
Developed by Google
A subset of the Linux operating
system.
SPCF MIS Department@2014
Android Programming
Capabilities of Android powered
devices (varies by device):
GPS
Internet (wi-fi & cell)
Camera
Voice/Sound
Movement/Orientation
Touch Screen
SPCF MIS Department@2014
Traditionally to program Android
powered devices you would use such
tools as Eclipse, a Java IDE or App
Inventor a web based development
tool.
App inventor has been dropped by
Google
Each of these tools are free and use
open source licensing
Android Programming
SPCF MIS Department@2014
Android Programming
Problem is five fold:
Learning new development tools
Learning new paradigms & vocabulary
Developing user interface without visual
tools
Learning new programming language
Pain getting developed software to actual
device
SPCF MIS Department@2014
Android Programming
An easy to learn, and easy to use
new programming tool for Android
powered devices is Basic4Android
Uses a Visual Basic ( Microsoft) like
language
IDE with visual design of GUI in both
virtual device, emulator & actual
device
Costs $49 USD (Standard), $99 USD
for Enterprise version
SPCF MIS Department@2014
Android Programming
Basic4Android
Free evaluation version can be used
for 30 days
Very robust downloadable manual &
user support forum
RAD tool of choice
SPCF MIS Department@2014
Android Programming -
General Features:
Simple and powerful tool
IDE and programming language 100% focused
on Android development
Compiles to native byte-code. No runtime
libraries are required. Created APK files are
exactly the same as APK files created with
Java / Eclipse
Performance is similar to applications written
with Java
Event driven programming language similar
to Visual Basic with support for objects and
code modules
SPCF MIS Department@2014
Android Programming -
General Features:
Event driven programming language similar
to Visual Basic with support for objects and
code modules
No need to write any XML files
Highly extensible with support for custom
Java libraries
Large, active and friendly community of
developers. Over 18,000 registered
developers!
The only available true WYSIWYG visual editor
for Android. The visual editor supports
multiple screens and resolutions
SPCF MIS Department@2014
Android Programming -
General Features:
All Android phones and tablets starting with
Android 1.6 and up to 4.0 are supported
Modern IDE with auto-complete, built-in
documentation, internal index New! and other
advanced features
Powerful step-by-step debugger
Screen Capture Tool
SPCF MIS Department@2014
Supports all Android core features including:
SQL databases
GPS
Home screen widgets and live wallpapers
Background services and broadcast listeners
Bluetooth (serial ports)
Phone features: phone calls, sms, contacts,
call logs and others
USB host and accessory modes
Web services
Camera
XML, JSON and CSV
Android Programming -
SPCF MIS Department@2014
Supports all Android core features including:
Views animations
Multi-touch gestures
Networking (TCP, UDP, FTP, SMTP and POP3)
Push notifications (C2DM) New!
Text to speech and voice recognition
AdMob ads and other ads platforms
OpenGL
Graphics and charts
Android Programming -
SPCF MIS Department@2014
Supports all Android core features including:
Sensors
Files I/O
Integrate with services such as: Parse, Google
services, Twitter, Facebook and others
Objects serialization
Sip / Voip
Android Programming -
SPCF MIS Department@2014
Version:
Basic4Android v3.80
http://www.basic4ppc.com/
http://www.basic4ppc.com/android/download
s.html
SPCF MIS Department@2014
View of the Basic4Android Tools
SPCF MIS Department@2014
Basic4Android
IDE
SPCF MIS Department@2014
SPCF MIS Department@2014
ANDROID EMULATOR
SPCF MIS Department@2014
Problem With Built-in Emulator
Emulator is slow!!
SPCF MIS Department@2014
GenyMotion
Genymotion is a fast third-party
emulator that can be used instead of
the default Android emulator. In
some cases it's as good as or better
than developing on actual devices!
http://www.genymotion.com/
SPCF MIS Department@2014
SPCF MIS Department@2014
GenyMotion
SPCF MIS Department@2014
Adding Android Virtual Machines
In order to add a new android virtual
machines. You must register at
genymotion.com
Use your registered account to login
and download an android virtual
machine
SPCF MIS Department@2014
Adding Android Virtual Machines
SPCF MIS Department@2014
SPCF MIS Department@2014
Note: Developing applications and using
the emulator to test is fine, but you must
have access to a real Android device(s)
and test. You should always test against
the type of devices that your software is
targeted.
SPCF MIS Department@2014
Android Tablet
SPCF MIS Department@2014
USB Cable
SPCF MIS Department@2014
Micro SD memory Card
SPCF MIS Department@2014
Android Programming
Challenges (to name a few):
Orientation Landscape vs. Portrait
Screen Size Various sizes
Device Capabilities GPS and camera
SPCF MIS Department@2014
Day 1
Introduction
to B4Android
SPCF MIS Department@2014
Integrated Development
Environment - IDE
SPCF MIS Department@2014
Code Area
Module List
Subroutine List
Tabs
SPCF MIS Department@2014
IDE Description
Code area The code editor
Module list List containing all
module names.
Clicking on one of the
names jumps directly to
the selected module.
Subroutine list List of the subroutines
in the current module.
Clicking on one of the
names jumps directly to
the selected routine.
Tabs Tabs for different
settings.
SPCF MIS Department@2014
Menu and Toolbar
SPCF MIS Department@2014
Toolbar
SPCF MIS Department@2014
File Menu
SPCF MIS Department@2014
Edit Menu
SPCF MIS Department@2014
Project Menu
SPCF MIS Department@2014
Code Area
Management
SPCF MIS Department@2014
Collapse a subroutine
In the IDE a subroutine can be
collapsed to minimize the number of
lines displayed
SPCF MIS Department@2014
SPCF MIS Department@2014
Hovering with the mouse over the
collapsed routine name shows its
content.
SPCF MIS Department@2014
Collapse a Region
You can define Regions in the code,
which can be collapsed.
SPCF MIS Department@2014
Commenting and uncommenting
code
A selected part of the code can be set
to comment lines or set to normal
SPCF MIS Department@2014
Splitting Long Lines
Long lines of code are difficult to
read:
The underscore character can be
used to split long lines. For example:
SPCF MIS Department@2014
Creating Your
First App
SPCF MIS Department@2014
Your First App
When you create a new
Basic4Android app, a sample project
is already loaded, allowing you to
run this simple app without any
additional code.
SPCF MIS Department@2014
C
O
D
E

T
E
M
P
L
A
T
E
SPCF MIS Department@2014
Save the program
Use the [File > Save] menu
SPCF MIS Department@2014
Run the program
To run the program press F5 or click
the
SPCF MIS Department@2014
Using GenyMotion As Emulator
You can use GenyMotion to act as an
emulator. To do this GenyMotion
must be running with a selected
Android VM
SPCF MIS Department@2014
SPCF MIS Department@2014
Running app inside
GenyMotion
SPCF MIS Department@2014
The Designer
The Designer allows generating
layouts with either the Emulator or a
real device
SPCF MIS Department@2014
The View and Layout Concepts
First a few key concepts. In Basic4Android, a
page displayed to a user is called an Activity
and a control which can be added to the
Activity is called a View. The details of Views
are collected in a file called a Layout.
The code which controls the Layout is called
an Activity Module. To be visible to the user,
the Layout must be loaded into the Activity.
This normally happens within the
Activity_Create sub.
SPCF MIS Department@2014
Running the Designer
Run the Designer by clicking
Designer in the main menu. The
Designer and Abstract.
Designer Windows appear:
SPCF MIS Department@2014
This is where you add views
(controls) and configure their
properties.
SPCF MIS Department@2014
Connect the Designer to your
device
To see the Layout in WYSIWYG, you
need to connect Basic4Android to
either a device or an emulator.
SPCF MIS Department@2014
Connecting To GenyMotion
Be sure that GenyMotion
Double click the icon to connect to
Android VM
SPCF MIS Department@2014
Add a button
In the Designer Window click [Add
View > Button]
SPCF MIS Department@2014
SPCF MIS Department@2014
SPCF MIS Department@2014
Configure The Button
SPCF MIS Department@2014
Saving The Layout
Save your layout by clicking [File >
Save] in the Designer. You can
choose any name, but it makes sense
to use the same name as the Activity
Module, in this case Main. This will
create a file called main.bal within
the Files folder of your project.
SPCF MIS Department@2014
Generate Members / Events
In order to control the button in
code, we need to declare it in the
relevant Activity Module.
The best way to make sure you do
this correctly is to ask the either
Designer or the Abstract Designer to
generate this code for you.
First make sure the correct Activity
tab is selected in the IDE, since any
code you generate will be added to
the current activity.
SPCF MIS Department@2014
Using the Designer Tools
Select your new button and use the
Designers [Tools > Generate
Members] option. This will display
the following (you might need to
expand the btnTest list by clicking
the + sign:
SPCF MIS Department@2014
Using the Designer Tools
Check btnTest and Click, as shown.
Click Generate members and Close.
SPCF MIS Department@2014
Using the Abstract Designer
An alternative way to generate these
two pieces of code is to use the
Abstract Designers context (or
popup) menu. If you right-click on
btnTest in the Abstract Designer, the
following menu appears:
SPCF MIS Department@2014
Add code to button
Now we are going to write the code
to handle this event. Move the
existing Msgbox code from the Sub
Activity_Create into the new sub. It
should now read
SPCF MIS Department@2014
Load the Layout
To make this work we need to load
this new layout when the app starts.
Edit Activity_Create to read:
SPCF MIS Department@2014
Your Third App
Now lets see if you can create an app on your
own! You are going to create an app which will
show the time when the user clicks a button. To
do this, you need to add a label to your view. A
label is an object (a view in Android jargon)
which can display text. Call it Label1.
Use the designer to declare the label (that is, add
a Dim statement to your code). Now change your
app so that your btnText will run the following
code when the user clicks it:
This defines the message which Label1 will show.
DateTime is a Basic4Android object which
provides a wide range of time-related and date-
related functions.
SPCF MIS Department@2014
Activity 1: Creating A Calculator
Create a basic app calculator that
will show the sum of two values
Create a calculator app that will do
all basic mathematical function
SPCF MIS Department@2014
Variables
A variable is a symbolic name given
to some known or unknown quantity
or information, for the purpose of
allowing the name to be used
independently of the information it
represents. A variable name in
computer source code usually
associated with a data storage
location and thus also its contents,
and these may change during the
course of program execution. (source
Wikipedia)
SPCF MIS Department@2014
Variable Types
SPCF MIS Department@2014
Declaring Variables
Variables are declared with the Dim
keyword followed by the variable
name and the As keyword and
followed by the variable type.
Examples:
SPCF MIS Department@2014
Declaring Variables
The same variables can also be
declared in a short way.
The names of the variables separated
by commas and followed by the type
declaration.
SPCF MIS Department@2014
Type variables
The Type keyword is used to create
your own types or structures. You
can use such types to create simple
structures that group some values.
However, you can also use it to
create more complex collections.
Define a type with the Type keyword:
SPCF MIS Department@2014
We can declare either single
variables or arrays of this type:
To access a particular item, we use
the variable name and its data item,
separated by a period:
SPCF MIS Department@2014
Array Variables
Arrays are collections of data or
objects that can be selected by
indices. Arrays can have multiple
dimensions.
The declaration contains the Dim
keyword followed by the variable
name LastName, the number of items
between brackets (50), the keyword
As and the variable type String.
SPCF MIS Department@2014
The first index of each dimension in an array is
0.
SPCF MIS Department@2014
Visibility and Lifetime of
Variables and Subs
There are two access modifiers which
determine the visibility of variables
and subroutines between modules:
Public and Private. Public means that
the object can be accessed from other
modules as well as the one in which
it is declared. Private means it is
hidden from other modules. You can
declare them with:
SPCF MIS Department@2014
Conditional Statements
SPCF MIS Department@2014
If Then Else End If
The If-Then-Else-End If structure
allows you to operate conditional
tests and execute different code
sections according to the test result.
General case:
SPCF MIS Department@2014
Select Case
The Select - Case structure allows you
to compare a test expression with
other expressions and to execute
different code sections according to
the matches with the test expression:
SPCF MIS Department@2014
Activity 2: Grading System
Create an app that will compute the
average grade of a student based on
three quizzes
Show a remark whether the student
PASSED or FAILED
Set the color of the label / remark to
RED if PASSED and BLUE if FAILED
SPCF MIS Department@2014
SPCF MIS Department@2014
For Next
In a ForNext loop, the same code will be
executed a number of times controlled by a
variable called an iterator. For example:
In this case i is the iterator. This is how the
code is executed:
Iterator i set to the first value 1 and your code
will be executed.
When execution reaches Next, execution will
return to the For statement and i will be
incremented by the Step value 2 to 1+2 or 3.
If i is less than or equal to the upper value 10,
then your code will be executed again.
This will be repeated until i is greater than the
upper value
SPCF MIS Department@2014
Exit
It is possible to exit a ForNext loop with the Exit
keyword. When code execution meets the Exit
keyword, it continues on the line after Next. The
following will log 1-4:
Continue
If you want to stop executing the current iteration
but continue with the next one, use Continue. The
following will log 1-4 and 6-10, but not 5:
Differences between Basic4Android and Visual Basic
B4A uses Next, whereas VB uses Next i
B4A uses Exit, VB uses Exit For
SPCF MIS Department@2014
Do-While
You can loop while a certain
condition is True. For example, this
will randomly decrease a number
starting with 10000 and log the
result while it is greater than 0:
SPCF MIS Department@2014
Do-Until
Sometimes, we do not know the
initial value which we want to use.
We only know when we want to stop
the loop. In this case, we use the Do
Until loop:
SPCF MIS Department@2014
Exit a Loop
It is possible to exit either of these
Do-Loop structures by using the Exit
keyword.
SPCF MIS Department@2014
Modularizing Code
SPCF MIS Department@2014
Subs
A Subroutine (Sub) is a piece of
code. It has a distinctive name and a
defined visibility. In Basic4Android, a
subroutine is called Sub, and is
equivalent to procedures, functions,
methods and subs in other
programming languages. Example:
SPCF MIS Department@2014
Calling a Sub
When you want to execute a Sub in
the same module, you simply use the
Subs name.
SPCF MIS Department@2014
A subroutine declared in a code
module can be accessed from any
other module by prefixing the name
of the module where the Sub was
declared to the name of the sub,
joined by a dot:
Calling a Sub from another
module
SPCF MIS Department@2014
Parameters
Input parameters can be transmitted
to the Sub. This allows you to make
the sub do different things
depending on its inputs. The
parameter list is enclosed in
parentheses, and their types are
required:
To invoke a sub which needs
parameters, add the parameters to
the call:
SPCF MIS Department@2014
Activity 3:
It is a common belief that 1 human year
is equal to 7 dog years. That is not very
accurate, since dogs reach adulthood
within the first couple of years. To
calculate the proper age of a dog you
use the formula:
10.5 dog years per human year
for the first 2 years, then 4 dog
years per human year for each
year after.
Create an app that will calculate the
dogs age based on the given formula
above.
SPCF MIS Department@2014
Communicating with your User
As your program runs, you will need
to send messages to your user from
time to time. We deal here with the
methods of doing this. Of course,
there are many Views (such as
buttons) which allow the user to take
actions during an Activity, but here
we are thinking about how you can
take specific actions to gain your
users attention.
SPCF MIS Department@2014
Modal Dialogs
There are several ways to show your
user a message in a dialog box which
remains visible until the user clicks
(sometimes called modal or
blocking dialogs). The program will
not continue and timers will be
suspended until the user responds.
SPCF MIS Department@2014
Msgbox
Use the Msgbox keyword to show a
simple message without any options.
You can specify the message and the
box title:
SPCF MIS Department@2014
Msgbox2
If you want to show more options,
Msgbox2 allows you to include any
combination of the following: a
positive button, a negative button, a
cancel button and/or an icon. It will
return one of the DialogResponse
constants, and you can detect the
users response and act accordingly:
SPCF MIS Department@2014
InputList
InputList shows the user a modal
dialog with a list of options. It ends
when the user clicks on an option
and returns either the index of the
selected item, or
DialogResponse.Cancel if the user
presses the back key.
SPCF MIS Department@2014
InputList
SPCF MIS Department@2014
InputMultiList
InputMultiList lets you show a list
from which the user can select
multiple items before returning.
SPCF MIS Department@2014
SPCF MIS Department@2014
SPCF MIS Department@2014
SPCF MIS Department@2014
SPCF MIS Department@2014
SPCF MIS Department@2014
PUBLISHING YOUR ANDROID APP
Icon Image
Screen Shot(s)
Also Required:
APK file
XML manifest file
SPCF MIS Department@2014
PUBLISHING YOUR ANDROID APP
Market Places:
Market.android.com
Getjar.com
AndAppOnline.com
Mobihand.com
Aproov.com
Barnes & Noble
Kindle/Amazon
SPCF MIS Department@2014
Additional Resources:
www.basic4ppc.com
Android Application Development
for Dummies; Donn Felker
App Inventor for Android: Build Your
Own Apps No Experience Required;
Jason Tyler
Beginning Android; Mark L. Murphy
SPCF MIS Department@2014
Additional Resources:
Android Application Development;
Rick Rogers, John Lombardo, Zigurd
Mednieks & Blake Meike
Hello, Android; Ed Burnette
Android Programming Tutorials;
Mark L. Murphy

You might also like