Writing Your First Android Application: A Hello World Example – Part 2 – Understand the Application File Structure

Now that you have a working application open in Eclipse, there are all these crazy-ass files under Package Explorer that were created by the plugin. So, what do they do and which ones will you be dealing on a daily basis with?

Under the /src folder you will find what is called “Activities”. The easiest way to think about activities is that an activity represents what you see on the screen and which implements logic for user actions on that screen. The /src folder does not contain the presentation, or layout, of the screen and it’s elements and only contains the Java code to handle events pertaining to the activity.

The layout of your screen and the components residing on it are stored in an XML file in the /res/layout folder. Each XML file is just that – a layout so two or more activities can be bound to the same layout but implement different logic for it. For instance, if your layout contains a textbox and a button, you can create two activities that will show that same layout but perform different actions when you click the button.

The glue that ties activities in /src and layouts in /res/layout is the AndroidManifest.xml file in the root of your project. This XML file lists all the activities in the project and sets the layouts they are using. You’ll need to do this manually in Eclipse.

In Part 3 (to be posted soon), we’ll go further into details of these files and building a simple Android application.

A short recap:
1. Activity Classes (java) = Handling logic – what happens when you tap, swipe your finger across the screen, change orientation, etc. If you are familiar with MVC, this is your Controller.
2. Layouts (xml) – These are your definitions of what is on the screen – layout, docking, components, etc. If you are familiar with MVC, this is your View.
3. Manifest – Eclipse does not automate things, so for each activity you need to manually add it to the manifest file and specify the layout it uses.

This entry was posted in Android and tagged , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *