Android: Nuts and Bolts I

In my first real “Nuts and Bolts” entry, I’m going to discuss User Interface design. To avoid some redundancy, I ask that readers first work through Google’s Hello Android and a few of the Hello Views tutorials. These will give some familiarity with how to actually get Eclipse talking to Android, as well as teach a bit about the widgets available. What I intend to teach in this tutorial are the things that I didn’t find immediately obvious when going through the Google ones (though in my opinion they are actually pretty good).

A word of caution! I don’t necessarily intend Nuts and Bolts to be a series that anybody keeps up with as I write it. Rather, it is intended to be a cohesive group of entries which cover early Android techniques by topic. The examples linked in this entry will most likely be referenced in several more entries to come, just because there is such a breadth of information in the first few tutorials.

First off, I would reccomend that you don’t just latch on to the first layout method you come across. There are a lot of ways to arrange a user interface in Android and you should choose the right one for the task (though, for reasons I’m still not clear on they told us to use RelativeLayout as often as possible at Google I/O). Additionally, you may be tempted to hide your user interface elements and display a results box, or something of that nature. This seems like a reasonable approach to someone still learning Android (it did to me!), but is not the correct way to display a ‘new screen’ for results.

As you might guess, the proper way to show a ‘new results screen’ is to ask Android for exactly that! The Notepad Tutorial contains some sample code dealing with the creation of a second Activity. An activity in Android can be thought of as any ‘new screen’ that you would go to to perform different actions (or… activities!). Examples of this include viewing results, inputting information into a form, or switching to a new application altogether. Activities are accessed through Intents; these can launch both custom Activities which you create, and activities which others have already coded (though I haven’t learned how to do that last one just yet!). Each Activity can use its own unique xml layout, which is defined in the same place as the main.xml that the SDK creates for you.

You may also want to read about Themes, which can dramatically change the way your Activity presents itself. I use the Dialog theme for an Activity in one of my apps that allow the user to edit information. I chose to do this because I wanted the user to still see the main information display while they edit part of it. To me, that preserves the feeling that the user hasn’t gone away from their main activity – they are just taking a moment to alter their display.

Users who have gone through all of the above tutorials will have already encountered Event Listeners, but in the interest of keeping each Nuts and Bolts focused, I’m going to leave that topic for next time. There is a great deal of information covered in the links provided here, so don’t worry about understanding everything all at once! Go at your own pace, and be sure to browse future Nuts and Bolts entries which will cover other aspects of the code in those links.