Monday, November 28, 2011

Tip: Handling API Keys

If you've ever done work against the Google APIs or some other metered web service you've no doubt come into contact with API keys. These keys usually associate some identity to the product/client using the web service.
A way to manage this is to create string resources for each API key in a resource file called res/values/strings_apikeys.xml. Below is an example of a resource file with API keys for Google Maps and Android Backup service.

Benefits:

  1. Keys can be referenced in other resource files or the AndroidManifest.xml as shown below.
  2. You can setup a key per region using the region qualifiers. Or use any resource qualifiers to have different keys against.

res/values/strings_apikeys.xml

    @string/apikey_google_maps_debug_laptop
    01rg6-jslkjdakljdakldakjldaasdkasjdlaj-NeedARealKey
    01rg6-jkljakljdklajdlas-UseAKeyHere
    01rg6-jlkfsdklfjasfkajs-FakeKey
    AEdPqrEAAAAIu-GetARealBackupKey

Example of Google Maps View using the referenced key. You can then use this template view using the tag in other layouts.
res/layout/mapview.xml



Tuesday, November 22, 2011

Android Ice Cream Sandwich Resources

These files are from the Android 4.0.1 source tree.
This is basically a merge and zip of all the res/ directories from the source code.
Useful for making your apps look like Ice Cream Sandwich stock apps. Have fun!

Download

Monday, November 21, 2011

Download Android Source

First install git 1.7, java 1.6, and python 2.7.
mkdir ~/bin
PATH=~/bin:$PATH
curl https://dl-ssl.google.com/dl/googlesource/git-repo/repo > ~/bin/repo
chmod a+x ~/bin/repo
mkdir androidsource
cd androidsource
repo init -u https://android.googlesource.com/platform/manifest
repo sync
For more details visit: Initializing a Build Environment

Saturday, November 19, 2011

Layout Tip: Spacer in LinearLayout

Sometimes you want a layout where you want a spacer view that fills that pushes views to the right of it as far as they can. A common need to do something like this is when you roll your own Action Bar.
The action bar layout with the spacer view highlighted.
XML Layout


    
    
    
    
    
    
    


Explanation
When working with LinearLayout there's an option to use weights of free space rather than a set amount of space. When the layout measures its children it first sets the fixed sized children. For this layout that's everyone except the spacer view. Then it does another pass to measure the children that will fill dimensions based on their android:layout_weight parameter. Since there's only 1 view with this parameter, it fills up the remaining space.

Source Code
actionbar.xml | Project Page

Sunday, November 6, 2011

Tuning Eclipse for Android Development

The standard IDE for Android development is Eclipse. For the most part, Eclipse has all the tools and goodies I need to get my job done. I'd consider it second to Visual Studio which is pretty good in my opinion. Listed below are some of the things I do in my eclipse environment to tune it better for Android development.


1. Download Eclipse Color Themes plugin
Fastest way to get a clean dark color theme for your source code.
http://www.eclipsecolorthemes.org/
Eclipse Color Theme with Obsidian preference selected.
2. Increase the font size
Having big 23" monitors is a must for a developer. Having a big font improves readability, reduces clutter, and is more friendly on the eyes. Especially useful if you don't have 20/20 vision.


3. Associate Android SDK Jars as User Libraries (optional)
Libraries that get updated like the v4 support library will automatically be applied to all projects that include it as a user library instead of a jar within the project itself.
Another advantage is if you're creating a Java (not android) project so that you can export your code as a jar file from eclipse.


4. Associate JAVA_HOME environment variable to a 32-bit JDK
There seems to be subtle problems with Java 64 bit. Rather than dealing with ways to fix them the 32 bit version works fine. Both can co-exist on the same machine without problems.

5. Install Apache ANT (optional)
You might need it right away but having ANT might come in handy later down the road especially if you want to build a library and want a way to automatically package it up or you see a build.xml file somewhere. Download ANT and extract it to the base Android SDK folder.


6. Add Android SDK tools to your PATH environment variable
You'll eventually need access to tools like adb and draw9patch.
[ANDROID SDK DIRECTORY] \tools
[ANDROID SDK DIRECTORY] \platform-tools
[ANDROID SDK DIRECTORY] \apache-ant\bin


Well that's pretty much all the customizations for Eclipse that I use. 

Saturday, November 5, 2011

.hgignore for Android

If you use source control one of the first things you want to do is create an ignore rules file for binaries and auto generated files so they don't pollute check ins with useless changes.
Most developers out there use Mercurial and GIT for source control. Both of these systems are very similar and work fine for any project.

Here's the base .hgignore I've created over the course of making a few Android projects. This version is unique in that it will ignore the directories if your Android projects are placed in the root or in a subdirectory of the repository. It doesn't have that bug where a directory ending in an excluded directory's name also mistakenly get ignored. Like, should remain robin/ but bin/ is ignored.
#Mercurial Ignore Rules for Android
#Save as .hgignore in the repository base directory and add it to source control.
syntax: glob
*.class
*.apk
*.dex
*.ap_
*.suo

syntax: regexp
^(.*[\\/])?gen[\\/].*
^(.*[\\/])?bin[\\/].*
^(.*[\\/])?obj[\\/].*
^(.*[\\/])?log[\\/].*
^(.*[\\/])?obf[\\/].*
^(.*[\\/])?jars[\\/].*
^(.*[\\/])?jar-sources[\\/].*
^(.*[\\/])?javadoc[\\/].*
^(.*[\\/])?\.svn[\\/].*
^(.*[\\/])?\.metadata[\\/].*
^(.*[\\/])?\.settings[\\/].*

Thursday, November 3, 2011

Android Design Templates

Today I was throwing around some app ideas and I always like to draw these ideas out to visualize them. I remembered seeing some templates in the past that you could print out and have the outline of a phone to draw your app in, so I went searching for some Nexus S and Galaxy Tab templates. I didn't really find any, but I did find several other android idea/sketching goodies. In the end, however, I decided to make a custom template with my devices and I am releasing them under a creative commons license for all of you out there to enjoy. If you have suggestions on how to make it better, let me know in the comments.




Tuesday, November 1, 2011

Tip: Naming Resource Files

Android has a lot of resource files. These normally are XML files that have the <resource> root tag. The standard convention is to split resources out by type. The problem with this is these files tend to get very large and become unweildly to edit. A better way to organize them is by adding a category to the file name as well.

For example: strings_preferences.xml would contain all the strings that pertain to preferences while strings_application.xml will pertain to application strings that you might not want to localize.

Layouts sometimes become difficult to determine where they lie in the hierarchy. A good convention is to name layouts that are bound to an activity with the prefix activity_[activity name]. So MainActivity > activity_main.xml. Do this with fragments too: BuddyListFragment > fragment_buddylist.xml.