Bristle Software Android Programmer Tips

This page is offered as a service of Bristle Software, Inc.  New tips are sent to an associated mailing list when they are posted here.  Please send comments, corrections, any tips you'd like to contribute, or requests to be added to the mailing list, to tips@bristle.com.

Table of Contents:
  1. Android Programming Resources
  2. Getting Started with Android Programming
  3. Android Architecture Basics
  4. Publishing Your First App to Google Play
  5. Updating an Existing App at Google Play
  6. Beware specifying versionCode as a resource
  7. Beware too many Toast messages
  8. KeyEvents may ignore soft keys
  9. Emulator keys
  10. Bristle BrowserWidth tool
  11. Bristle JavaScriptEnabled tool
Details of Tips:
  1. 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

  2. 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

  3. 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:

    For more info, see:

    --Fred

  4. 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.

    1. (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.

    2. (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.

    3. (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.

    4. (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.

    5. (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:
      1. More extensive release notes than the 500-char limit that Google Play allows
      2. More extensive description than the 4000-char limit that Google Play allows
      3. A link to the app at Google Play
      4. Links to the app at Amazon Appstore for Android and any other markets where I may offer it
      5. The APK file of the app, available for direct download
      6. Screen captures, videos, and other marketing, training, and support materials

    6. 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.

    7. 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.

    8. 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.

    9. 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.

    10. 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.

    11. 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

    12. 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.

    13. 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.

    14. 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:
    15. 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

  5. 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:

    1. Make source code changes, build, test, review, etc.

      Do all the usual stuff you would do with any software before doing a release.

    2. 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.

    3. 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:
    4. Create a "release ready" APK file

      Same as in the previous tip (Publishing Your First App to Google Play).

    5. 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.

    6. Update version control system

      You always want to know exactly which source code went into each version.

    7. Update the marketing Web site

      Update the release notes, APK, screenshots, and all other materials at the marketing Web site.

    8. 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

  6. 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:


    and referred to it in the manifest file as:


    and in resource files as:


    and in Java files as:


    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:


    and referred to it in the manifest file as:


    and in resource files as:


    and in Java files as:


    However, versionCode in the manifest file is required to be an integer, not a string, so I switched to this format in strings.xml:


    and referred to it in the manifest file as:


    and in resource files as:


    and in Java files as:


    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:


    Googling, I found others complaining of the same problem, and this link with an interesting workaround:


    --Fred

  7. 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:


    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 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

  8. 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:


    or more easily, via "Event Listeners" by calling EditText methods like:


    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

  9. 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:

    --Fred