Tuesday, November 27, 2012

Android beginner tutorial: Part 3 - Android project structure

Today we'll explore the contents of the generated project folder.

After creating your Android project in Eclipse, you'll see the project file browser in the left side of the screen. Here is what mine looks like:

The project structure may differ if your API version is different, but it still should be somewhat similar.

Let's start by exploring the res directory. This is a resource folder, which contains the resources that you'll be using in your application, such as images, strings, animations and so on. The ADT plugin generates and adds some folders and files inside the res directory after creating the project.

The res/drawable-hdpi, res/drawable-ldpi, res/drawable-mdpi and res/drawable-xhdpi folders are used to store images, that are used in your application. The reason for splitting the images in multiple folders is the different screen resolutions for devices. You'll find different versions of the launcher icon you've set while creating the project in all of these folders.

The next folder is res/layout - it contains XML files that represent layout of the Activities in your application.

The res/menu folder stores XML files for defining application menus, which are used for different purposes, for example, the options menu.

The res/values folder stores values that are used in your application. These can be strings, arrays, styles and more.

You can see res/values-v11 and res/values-v14 folders in my screenshot above, which contain styles.xml. These are the themes that used instead of the default styles.xml for API version 11 and API version 14.

These are not the only folders that can be contained within the res folder. There are a few more pre-defined directories that, if not generated automatically, can be created manually. It should be noted that Android is rather strict about the resource directory and most of the folder names are pre-defined. For example, Android doesn't support folders inside other folders here.

That's the general information about the items in the resource directory. We will learn their purpose and usage in details as we work on our application.

The next file we should take a look at is the MainActivity.java file inside the package folder inside src. If you open it, the contents look something like this:

This is basically a Java class, which contains the functionality code for the main Activity. So, XML is used to set the layout for the activity, and this Java class is used to add functionality. It's similar to Flex - the application layout is defined in a MXML file, and the code is in AS3 classes.

We won't delve into the code right now, let's instead check out one more item in the project folder - AndroidManifest.xml.

This is basically the configuration file - it declares the general information about the application (version, package...), components of the application, lists the libraries that are used, application permissions, etc. It's comparable to the descriptor.xml file in Adobe AIR projects.

You can edit this file manually as an XML file, or using the Manifest Editor that Eclipse offers. The basic important info is filled out automatically when you create a project. You can edit them here any time.

And those are the essentials that you need to know about the structure of an Android project folder.

We'll learn how to run and debug applications next time.

Thanks for reading!

No comments:

Post a Comment