Archive for the ‘Android’ Category

Android Developer Challenge Round 2

Thursday, May 28th, 2009

Google has announced a second round of the now-famous Android developer challenge – which awards cash for writing cool apps for the Android platform. Win-win, huh? In 2008, Google did this and you can see the results here. The big surprise – the winningest apps were NOT particularly amazing ($275,000 was given to the writers of the cab4me app which locates a taxi for your location). You can be sure that this round will offer some stiffer competition. Here is the prize breakdown:

For each of the 10 categories:

  • 1st prize: $100,000
  • 2nd prize: $50,000
  • 3rd prize: $25,000

Overall (across all categories)

  • 1st prize: $150,000 (meaning the overall winner will receive $250,000)
  • 2nd prize: $50,000 (meaning the 2nd prize winner will receive up to $150,000)
  • 3rd prize: $25,000 (meaning the 3rd prize winner will receive up to $125,000)

To be eligible, your app can NOT have been published already. Also if you already entered your app in the first contest, forget it. Also, english only, por favor. Full contest details can be found here.

Book Review: The Busy Coder’s Guide to Android Development

Wednesday, April 22nd, 2009

I’ll Only write a review about something I like.  That’s good news for Mark Murphy and his book:  The Busy Coder’s Guide to Android Development.  I’ve found this book invaluable in writing my first Android application since I’m not yet familiar with the sometimes perplexing details of the android API.

Busy Coders Guide

Busy Coder's Guide

The book is available in print or through Murphy’s website:  http://commonsware.com/ .   I Strongly recommend the latter for a few reasons.  Firstly Android is a [rapidly] moving target.  The website purchase allows you to download updates for a year and updates do seem to appear from time to time.  Secondly you also get access to the unfinished advanced book.  While that book is unfinished it just so happened to contain a completed chapter explaining exactly what I wanted to do.  Once your subscription runs out you still get to keep the books in pdf form.

Rather than go through an exhaustive list of the contents I’ll tell you what I’ve found useful so far.

The book has LOTS of details on android widgets and how to describe them in your layout xml’s and access them in your code.  It details some of the fancier things you can do such as dynamic list content (albeit different than my earlier posting) and an options menu api.

The book has some chapters on thread handling and application lifecycle.  I had previously spent weeks combing these details out of various message boards and mailing lists as well as pouring over the then-unclear api documentation.  These chapters would have been a big time-saver for me.

Probably the biggest deal for me is addressed in this book and continued in the advanced book:  Background Processes.  There are all sorts of different ways to set up background processes in android and all sorts of different ways to communicate them.  If you’re struggling with untangling the mystery of persistant programs in android this will be $35 well spent.

There are a couple of things that annoyed me a little about the books.  Code blocks are split up into snippet-description-snippet-description sequences so sometimes you have to put the pieces together a little.  He doesn’t always specify where the code would belong in your directory structure (please fix this one thing if you ever read this Mark).  The unorthodox naming of chapters might make it a little tough to quickly find what you’re looking for but they add a lot of character so I’m actually fine with it.  Just be prepared.

All in all the Busy Coder’s Guides are great resources and well worth the money.  I have it open on my other screen *right* now as I write this and will be using it to help put a few finishing touches on my program this afternoon :-)

- Tyson

Free Graphical Layout Editor for Android

Wednesday, April 15th, 2009

Quick one for you Android coders out there.

Have you ever had trouble getting a layout to look just right for one of your activities? Have you had trouble coming even close? Me to. Check out this tool. It’s a gui layout editor that’s helped me lots of times come up with the right xml for my layouts or tweak what I had come up with already. It’s an applet that will run in your browser to:

http://www.droiddraw.org/

-Tyson

Free Stanford iPhone Dev Course

Tuesday, April 14th, 2009

This is cool. There is a free iPhone development course available for download through iTunes here. This is being put on by Stanford university. I have yet to watch it but will be.

The general “prerequisite” for the course is a familiarity with C:

“we do strongly recommend that students are comfortable with programming in C, especially with regard to using pointers and general C memory management (malloc/free). Familiarity with object oriented designs and principles are definitely helpful but tend to pose less of a stumbling block than wrangling with pointers in C.”

Making Custom Rows in Android ListViews

Sunday, April 12th, 2009

Hi all,

I’ve been spending the last little while writing software for Google’s Android mobile phone platform and I’ve struggled with a few things along the way.  I thought it might be a good idea to write about some of those struggles and their eventual solutions.  This particular one held me up for several days and, try as I might, an example or solution was nowhere to be found.

In Android you can bind a sqlite database curser directly to a ListView on the display using a furnished adapter called SimpleCursorAdapter.  While this is great it makes a pretty boring ListView by default.  Each row is exactly the same.  I wanted to change the text colour on some rows depending on a field from the database that I wasn’t actually going to display.  First lets have a look at the layouts:

/res/layout/pod_view.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent">

	<TextView android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:text="@string/title"
		android:paddingTop="4pt" />

	<TextView android:id="@+id/title"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:textSize="@dimen/font_size_for_pod_row"
		android:paddingBottom="6pt" />

	<ListView android:id="@+id/android:list"
    	android:layout_width="fill_parent"
    	android:layout_height="wrap_content"/>

</LinearLayout>

Nothing to exotic here, just a ListView with a title at the top.  Notice that the layout_width is set to “fill_parent” on both the outer container, LinearLayout, and the ListView itself.  Without this users will only be able to click the part of each row to select it.  If the text doesn’t go all the way across the row it creates an unclickable dead spot which is a bit confusing.

Now each row also has a layout like this one

/res/layout/pod_row.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView android:id="@+id/text1" xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="@dimen/font_size_for_pod_row"
    android:paddingTop="@dimen/vertical_padding_for_pod_row"
    android:paddingBottom="@dimen/vertical_padding_for_pod_row"/>

Again mind the layout_width attributte.  I’ve stored the values for padding and text size in another spot to make adjustments easy.  For the sake of completeness…

/res/values/dimen.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="font_size_for_pod_row">20sp</dimen>
    <dimen name="vertical_padding_for_pod_row">6pt</dimen>
    <dimen name="font_size_for_show_row">14sp</dimen>
    <dimen name="vertical_padding_for_show_row">3pt</dimen>
</resources>

And that’s how you might do that.  This is handy if you have values you want to reuse and tweak in different layouts throughout your project.

Phwew, done with layouts.

Lets dive in and have a look at the modifications you can make to SimpleCursorAdapter.  I’m not going to talk about setting up a basic SimpleCursorAdapter because there are tonnes of examples out there.  One of them is in the official android tutorial. Here is the function that draws my screen within my Activity:

private void populateFields() {

    // This will be used by our SimpleCursorAdapter to bind fields in each row to
    // data from our cursor.  Note that we have an extra field in the cursor that
    // determines a display attribute for the field we display.
    class ShowViewBinder implements SimpleCursorAdapter.ViewBinder {

        // this cursor is built by calling a function that returns a few things.
        // right now I'm interested in the first (title) and the third
        //(downloaded status)

        private static final int DATA_COLUMN = 1;
        private static final int STATUS_COLUMN = 3;
        boolean retval = false;

        //@Override
        public boolean setViewValue(View view, Cursor cursor, int columnIndex) {

            if ( columnIndex == DATA_COLUMN) {
                int status = cursor.getInt(STATUS_COLUMN);
                TextView tv = (TextView) view;

                switch ( status ) {
                    case PodDbAdapter.DOWNLOADED:
                        tv.setTextColor(Color.GREEN);
                        tv.setText(cursor.getString(DATA_COLUMN));
                        retval = true;
                        break;
                    case PodDbAdapter.TO_DOWNLOAD:
                        tv.setTextColor(Color.rgb(0xFF, 0x99, 0));
                        tv.setText(cursor.getString(DATA_COLUMN));
                        retval = true;
                        break;
                    case PodDbAdapter.DOWNLOAD_ERROR:
                        tv.setTextColor(Color.RED);
                        tv.setText(cursor.getString(DATA_COLUMN));
                        retval = true;
                        break;
                    default:
                        tv.setTextColor(Color.WHITE);
                        tv.setText(cursor.getString(DATA_COLUMN));
                        retval = true;
                        break;
                }
            }
            return retval;
        }
    }

    Cursor pod = mDbHelper.fetchPod(mRowId);
    startManagingCursor(pod);
    mTitleText.setText(pod.getString(pod.getColumnIndexOrThrow(PodDbAdapter.KEY_TITLE)));

    Cursor showsCursor = mDbHelper.fetchShows(mRowId);
    startManagingCursor(showsCursor);

    // Create an array to specify the fields we want to send into our ViewBinder
    String[] from = new String[]{PodDbAdapter.SHOW_KEY_TITLE, PodDbAdapter.SHOW_KEY_STATUS};

    // and an array of the fields we want to bind those fields to (in this case just text1)
    int[] to = new int[]{R.id.text1};

    // Now create a simple cursor adapter and set it to display
    SimpleCursorAdapter shows = new SimpleCursorAdapter(this, R.layout.show_row_white, showsCursor, from, to);
    shows.setViewBinder(new ShowViewBinder());
    setListAdapter(shows);

}

There we have it.  Neat huh?

What I’ve done here is implimented a subclass of SimpleCursorAdapter that was furnished for just such an occasion:  SimpleCursorAdapter.ViewBinder.  My Cursor returns an extra field that we won’t display.  We’ll use that to determine the text colour of the data that is displayed by pulling that field out of the Cursor and having a look.  There are two Cursors in play in my example.  One of them is being used to populate the TextView in my pod_view.xml layout but the second is where the magic happens.  You can see where my ViewBinder is attached down near the bottom with shows.setViewBinder(new ShowViewBinder());

Lastly here’s the result.

device

And there we have it.. Looks like I have a couple of podcasts to listen to ;-)

-Tyson



© All rights reserved.