-
Android Programming Resources
Original Version: 7/19/2012
Last Updated: 7/19/2012
Applies to: Android 1.0+
The primary place to find info about Android programming is the official
site:
where you'll find excellent tutorials, sample apps, guides, API references,
design tips, developer tools, etc.
See the Android Development row of my links page:
for deep links into useful portions of that site, as well as other resources,
including blogs, mailing lists, tip sites, videos, sample code, where to
report bugs, etc.
--Fred
-
Getting Started with Android Programming
Original Version: 7/19/2012
Last Updated: 7/23/2012
Applies to: Android 1.0+
If you are already a Java programmer using Eclipse (or STS
-- SpringSource Tool Suite), it is easy to get started writing Android
apps. Download
the free Android SDK and the free ADT plugin for Eclipse. Use
the wizard to create a sample Android app. Drag/drop
the main UI components into place in the "Graphical
Layout" tab
of the XML editor. Wire up the behavior in familiar Java code. Run or debug the app in
Eclipse, and it connects to your Android device via USB or fires up an
emulator so you can test it, step through it, etc. Nice!
For more info, see:
Once you get something simple working, you'll want to learn more about
the Android architecture (see next tip) and begin exploring Android's extensive
Java class libraries.
--Fred
-
Android Architecture Basics
Original Version: 7/19/2012
Last Updated: 7/24/2012
Applies to: Android 1.0+
Android apps use a message-driven architecture with various type
of components:
Intent |
A message (string of text) typically specifying an action
to be performed or an event that has occurred.
Can cause an Activity to be displayed,
a Service to execute, etc. |
Activity |
A UI screen to be displayed to the user.
Typically launched at app startup or displayed by sending it an Intent.
Fills the entire screen -- the user cannot see 2 Activities at the same
time. |
Fragment |
A reusable portion of an Activity --
a region of a UI screen.
Added in Android version 3.0. |
View |
A single UI "control", like a checkbox, edit
field, etc.
Resides in an Activity or a Fragment. |
Service |
A behind-the-scenes block of code with no
UI.
Typically invoked by sending it an Intent.
Can be long-running on a separate thread from the UI, or bound temporarily
to an Activity in the UI. |
Broadcast Receiver |
Listens for Intents that are broadcast
by the system or by the enclosing app or any other app.
Typically does little work other than to dispatch the Intent to
an Activity or a Service. |
Content Provider |
Provides access to local or remote data (files, databases,
Internet resources, etc.)
Typically called directly (actually via a ContentResolver), not via an Intent. |
Manifest File |
XML file that registers the various components of
an app with the system, specifying IntentFilters that
control which Intents are routed to which components.
It
is common for one app to send an Intent as a way of
displaying an Activity of another app, or invoking a Service of
another app.
Also lists permissions needed by the app, hardware features used by the
app, etc. |
For more info, see:
--Fred
-
Publishing Your First App to Google Play
Original Version: 7/20/2012
Last Updated: 7/23/2012
Applies to: Android 1.0+
There are very complete and detailed instructions on how to publish an
app to "Google Play" (the new name of the "Android Market") at:
but that may be more than you want to read right now, especially if you
start following all of the links from there, and reading all of the style
guidelines, etc.
There is more concise info at:
starting in the section entitled "Publishing an Application - An
Overview". However, it glosses over some issues that I think are
important and some gotcha's that were not obvious to me, while giving more
detail than I needed for other parts.
My instructions are focused on getting the first version
of your app published ASAP, skipping any steps that can wait till later
releases, and warning you of any gotcha's I found.
- (Optional) Create a business name
You can skip this step and associate your apps with your personal name.
According to the docs, you can change the name later. However,
creating a business name can be as simple as making up a name that no
one is already using it, and perhaps opening a "Doing Business As" bank
account in that name. In my case, I already own "Bristle Software,
Inc.",
so I used it.
- (Optional) Register a domain name
If you register a domain name for your self or for your business,
you have exclusive rights to it, so you can rely on it being unique,
and can safely use it as a package name in your app. If you skip
this step, you'll be choosing a package name that may someday conflict
with other Android apps. Keep in mind that if you change the package
name after users start downloading and using your app, they will never
see updates because Google Play doesn't know it's the same app. In
my case, I already own the domain name bristle.com, so I used it.
- (Optional) Create a new e-mail account for Google Play
You can skip this step and just use your existing e-mail address when
registering at Google Play, if you don't want to
keep your Google Play stuff separate from other e-mail. According
to the docs, you can change the address later. Since I
own the bristle.com domain, it was easy for me to create a new "android-apps"
e-mail address there and forward it to my regular address. I can
filter incoming mail based on the address, or can change the forwarding
in the future if I like.
- (Optional) Create a new e-mail account for the app
You can skip this step and just advertise your existing e-mail
address to the users of your app, if you don't want to keep their e-mail
separate from other e-mail. Since
it was easy, I created
a new "android-apps-lacalc" address that I advertise
only with my "LACalc" app, and forwarded it to my regular address. I
can filter it or change the forwarding in the future.
- (Optional) Create a marketing Web site
You can skip this step, but I found it useful to have a place other than
Google Play to advertise and document my app. Since I already had
a web site at bristle.com, it was easy to create http://bristle.com/AndroidApps.
I can keep a list of my apps there, with the following for each
app:
- More extensive release notes than the 500-char limit that Google
Play allows
- More extensive description than the 4000-char
limit that Google Play allows
- A link to the app at Google Play
- Links to the app at Amazon Appstore for Android and any other markets
where I may offer it
- The APK file of the app, available for direct download
- Screen captures, videos, and other marketing, training, and support
materials
- Register at Google Play
Register for a "Google Play Publisher Account" at
the "Google Play Android Developer Console":
Specify your new business name, e-mail address ("android-apps" in my
example) and Web site, if you created them above.
Notes:
- When paying the one-time $25 fee, you can ignore Google Checkout
and just click Continue if you want to pay via credit card.
- Write the first version of your app
This can be a very simplistic first cut. Doesn't have to be pretty
or full-featured. You can start doing incremental new releases
immediately after publishing this first version, every 5 minutes if you
like. Use
a well-considered package name. As mentioned above, you cannot
change the package name later. I put all of my apps in packages
com.bristle.android.*,
to avoid conflicts with my open source Java apps (com.bristle.javaapps.*),
Java library (com.bristle.javalib.*), Web apps (com.bristle.webapps.*),
etc.
- Decide on the price to charge for your app
If you
make it free at first, you cannot charge for it later. However, it
is possible to use "In-App Billing" to charge for new features
in the future. You can also specify multiple prices in the future
if you expand to allow sales in multiple
countries and currencies.
- Create a launcher icon
Your app's APK file will contain a "launcher icon" in
various PNG sizes and resolutions for different devices. If you're
not feeling creative, you can temporarily use the default Android robot
icon (ic_launcher.png) that is created by Eclipse in all the necessary
sizes when you create your first
Android app. You can update the icon later. There's lots of good
design advice at the Android web site.
- Create a 512x512 pixel version of
the launcher icon
When you publish to Google Play, you'll be required to supply
a 512x512 pixel "PNG with Alpha" version
of the launcher icon. This large version of the icon
will be shown for your app at the Google Play Web site. You
can temporarily use the default Android robot icon here also. I
used the Mac built-in program "Preview" to load the largest
of the launcher icons, adjust its size to 512x512 via the Tools menu,
and save it as a PNG file with the Alpha option checked. You can
probably do the same on Windows via Microsoft Paint. Or you can
download and use GIMP, buy and use Photoshop, etc.
- Create 2-8 screenshots of the app
When you publish to Google Play, you'll be required to supply
at least 2 (up to 8) screenshots of the app. The easiest
way to create these is via Eclipse:
- Eclipse | Window | Show View | Other... | Android | Devices
- Select the hardware device or the emulator that is currently
showing the screen you want to capture.
Note: If you don't select one, the next step hangs,
showing "Capturing" until you cancel, select
one, and retry.
- Click the camera icon at the top right of the toolbar
- Decide the title of the app
When you publish to Google Play, you'll be required to supply
an app title of 30
chars or less. It does not have to exactly match the android:label
of the application in the manifest file, which can be longer. This
is the name that users will see when searching for apps, deciding
whether to download, etc.
- Write a brief textual description of the app
When you publish to Google Play, you'll be required to supply an
app description of 4000 chars or less. This
is the description that users will see when searching for apps, deciding
whether to download, etc. If you want a longer description, you can put
it at your marketing Web site.
- Create a "release ready" APK file
The easiest way to create a "release ready" APK file from
your app is via the Eclipse "Export Wizard" as:
- Eclipse | File | Export... | Android | Export Android Application
This takes care of generating a private key if you don't already have
one, using the key to sign the APK, optimizing the APK, etc.
Notes:
- See detailed step-by-step instructions at:
- Make the key valid for a long time, like 100 years. When
it expires, you'll no longer be able to update apps that were signed
with it.
- Do not put a comma in the "Organization" name of the
key. For example, "Bristle Software Inc." is fine,
but "Bristle Software, Inc." causes the error:
java.io.IOException: Incorrect AVA format
See details at:
- Upload and activate the APK
Upload and activate the APK at:
Notes:
- Activate the APK file before filling in all the details. Otherwise,
there will be no "Supported Devices" listed, and you'll
have to stop and activate it before continuing.
- Don't specify any "Promo Text" unless you have
specified a "Promotional Graphic". You can't have
it without the graphic.
- Specify the app-specific e-mail address you created above ("android-apps-lacalc" in
my example),
otherwise the Google Play e-mail address you created above ("android-apps" in
my example), otherwise,
your regular e-mail address. This address will be shown
to users and potential users who want to contact you.
- You may have to refresh the browser page after clicking "Publish",
to see your app listing.
- Wait 0-60 minutes for it to appear at the Google Play store for
download onto devices.
--Fred
-
Updating an Existing App at Google Play
Original Version: 7/21/2012
Last Updated: 7/21/2012
Applies to: Android 1.0+
To update an existing app at Google Play:
- Make source code changes, build, test, review, etc.
Do all the usual stuff you would do with any software before doing a
release.
- Update the version
Update the versionName and versionCode in the manifest file. If
you don't increase the versionCode, it's not a new release and Google
Play will object that you already uploaded it.
- Write release notes
Since "Recent Changes" at Google Play is limited to 500 chars,
I keep my full history of release notes in a text file at the marketing
Web site described in the previous tip (Publishing
Your First App to Google Play) and a smaller subset at Google Play.
For example, see:
- Create a "release ready" APK file
Same as in the previous tip (Publishing
Your First App to Google Play).
- Re-create screenshots as necessary
Same as in the previous tip (Publishing
Your First App to Google Play), but only for screens that have changed.
- Update version control system
You always want to know exactly
which source code went into each version.
- Update the marketing Web site
Update the release notes, APK, screenshots, and all other materials
at the marketing Web site.
- Upload and activate the new APK
Upload and activate the new APK at:
Notes:
- Click on existing app | APK files | New | Upload APK
Otherwise, you may get the error:
"You have another application on Google Play with the same
package name. Go to that other application, and click upgrade."
- Since "Recent Changes" is limited to 500 chars, I cut/paste
the top of the release notes from my Web site here, and include a
link to my site. That typically allows me to show all changes
for the most recent couple of releases. Users
who are upgrading from an older release can follow the link to the
Web site for more history. For example, see the "What's
New"
tab at:
- Update any screenshots and other Product Details as needed
- Deactivate old APK, and activate new one
- Save
- Wait 0-60 minutes for it to appear at the Google Play store for
download onto devices.
--Fred
-
Beware specifying versionCode as a resource
Original Version: 7/23/2012
Last Updated: 7/24/2012
Applies to: Android 1.0+
Beware factoring versionCode out of the manifest file and into a resource
file. Works fine, but Google Play refuses to publish the app.
Details:
Android allows (and the tools encourage) you to factor displayable strings
out of your Java source files into "resource
files" like strings.xml. This is very useful for internationalization
and for any situation where you want to separate the display strings from
the code. You can factor them out of your Java source files, and
also out of other resource files and even the manifest file.
I decided to do the same for the versionName string, which
is ordinarily specified directly in the AndroidManifest.xml file. This
allowed me to easily refer to the string in my Java classes and in my other
resource files, which made it easy to display to the user. I added
it to strings.xml as:
<string name="strVersionName">1.7</string>
and referred to it in the manifest file as:
android:versionName="@string/strVersionName"
and in
resource files as:
and in Java files as:
getString(R.string.strVersionName)
This worked fine, so I decided to do it also for versionCode,
which would allow me to keep the 2 related values together in the same file.
This would be especially handy if I want to automate the process of updating
these values as part of my build process. At first, I tried to do exactly
the same thing, adding it to strings.xml as:
<string name="strVersionCode">8</string>
and referred to it in the manifest file as:
android:versionCode="@string/strVersionCode"
and in resource files as:
and in Java files as:
getString(R.string.strVersionCode)
However, versionCode in the manifest file is required to be an integer, not a
string, so I switched to this format in strings.xml:
<integer name="intVersionCode">8</integer>
and referred to it in the manifest file as:
android:versionCode="@integer/intVersionCode"
and in resource files as:
"@integer/intVersionCode"
and in Java files as:
getResources().getInteger(R.integer.intVersionCode)
This worked fine, until I tried to publish to Google Play. I was able to
build and run the app in the emulator and on my physical Android phone, but when
I tried to upload the APK to Google Play, I got the error:
The file is invalid:
ERROR getting 'android:versionCode' attribute:
attribute is not an integer value
Googling, I found others complaining of the same problem, and this link
with an interesting workaround:
--Fred
-
Beware too many Toast messages
Original Version: 7/23/2012
Last Updated: 7/23/2012
Applies to: Android 1.0+
The easiest way to show a popup message in Android is via the Toast class,
which shows a message for a couple seconds, then clear itself. For
example:
Toast.makeText(this, "Show this message", Toast.LENGTH_SHORT).show();
This is similar to an alert() call in JavaScript, but does not
require the user to click OK. It clears itself.
Beware using too many Toast messages because they can get queued up and
be annoying. For example, while debugging some keystroke-handling
code, I chose to show a Toast message in response to each
keystroke, but when I typed faster than the Toast delay, I piled up lots
of Toast messages. Toast
only shows one message at a time, and it shows each message for its full
duration, so it took a long time for them all to get displayed
and cleared. Worse, I made the mistake of holding down an auto-repeating
key for a couple seconds, and queued up enough Toast messages
to display for several minutes. Even when I switched to a different
app, each Toast message remained on the screen for its full
duration, and the queued messages continued to pop up. Very annoying...
If you are just debugging, it is usually better to
use the built-in Android Log class instead, as:
Log.i("MyClass", "Show this message");
Log messages are written to the Android log file, which is shown dynamically
in Eclipse's LogCat view.
For more info on Toast and Log, see the Android Javadocs:
--Fred
-
KeyEvents may ignore
soft keys
Original Version: 7/24/2012
Last Updated: 7/24/2012
Applies to: Android 1.0+
If you want to respond to every keystroke a user types during data entry,
it is tempting to respond to events like key down, key up, etc. However,
beware that such events may be generated only for hardware keys,
not by the on-screen keyboard.
Details:
You can catch KeyEvents via "Event Handlers" by subclassing an
EditText and overriding methods like:
onKeyDown(int, KeyEvent)
onKeyUp(int, KeyEvent)
etc.
or more easily, via "Event Listeners" by calling EditText methods
like:
setOnKeyListener(EditText.OnKeyListener)
etc.
but when the user touches keys in the on-screen keyboard, these events may
not be generated.
I found it worked for these cases...
But not for these cases...
I used a TextChangedListener instead, which is a better idea anyhow because...
For more info see:
--Fred
-
Emulator keys
Original Version: 10/16/2012
Last Updated: 10/16/2012
Applies to: Android 1.0+
Want to switch the Android emulator from portrait to landscape layout? Hit
Ctrl-F11.
To see the complete list of laptop/desktop keys for the emulator, use the
command:
On
my Mac, this shows:
When running the emulator, use the following keypresses:
HOME Home button
F2, PAGEUP Menu (Soft-Left) button
Shift-F2, PAGEDOWN Star (Soft-Right) button
ESCAPE Back button
F3 Call/Dial button
F4 Hangup/EndCall button
F7 Power button
F5 Search button
KEYPAD_PLUS, Ctrl-F5 Volume up button
KEYPAD_MINUS, Ctrl-F6 Volume down button
Ctrl-KEYPAD_5, Ctrl-F3 Camera button
KEYPAD_7, Ctrl-F11 Switch to previous layout
KEYPAD_9, Ctrl-F12 Switch to next layout
F8 Toggle cell network on/off
F9 Toggle code profiling
Alt-ENTER Toggle fullscreen mode
F6 Toggle trackball mode
DELETE Show trackball
KEYPAD_5 DPad center
KEYPAD_4 DPad left
KEYPAD_6 DPad right
KEYPAD_8 DPad up
KEYPAD_2 DPad down
KEYPAD_MULTIPLY Increase onion alpha
KEYPAD_DIVIDE Decrease onion alpha
note that NumLock must be deactivated for keypad keys to work
--Fred