Wednesday 3 April 2013

Android project structure

Create new project "AndroidAdvanceHelloWorld"in eclipse.
After you create a new project in eclipse, you will see the following top-level folders in your project.



 Let we explain each step by step.



 (1) "src" folder

This folder will contain the Java source files that you will be creating. In the screenshot you can see the ‘activity’ files that were created for the sample project. The files inside this  folder will be organized according to the package structure. This is similar to the /src folder which is present in any normal Java project.

You can have more than one package in your Android application. Its always a good practice to break the source code of your application into different packages based on its core functionality.  All the source files of your Activities, Services etc. Goes into this folder. In the above screen, you can see the source file of the Activity that we created for our project.


 (2) "gen" folder
The gen directory in an Android project contains generated values. R.java is a generated class which contains references to certain resources of the project.

If you create a new resource, the corresponding reference is automatically created in R.java via the Eclipse ADT tools. These references are static integer values and define IDs for the resources.

The Android system provides methods to access the corresponding resource via these IDs.

For example to access a String with the R.string.yourString ID, you would use the getString(R.string.yourString)) method.

R.java is automatically created by the Eclipse development environment, manual changes are not necessary and will be overridden by the tooling.


 (3) "android-version number"
The android-version number folder, which will contain the libraries (jars/android.jar)  that are need for the project. In the screenshot, you can see that it contains the framework jar file. This is similar to the /lib folder which is present in any normal Java project.


 (4) "res" folder
This directory contains all the external resources (images, data files etc) that are used by the android application. These external resources (content) will be referenced in the android application.

 The "res" folder also contains sub-folders like following.

 -> /res/drawable                        -- Bitmap files / Nine-Patches (re-sizable bitmaps) / State lists / Shapes / Animation drawables / Other drawables
 -> /res/layout                             -- XML files that define a user interface layout.
 -> /res/values                            -- XML files that contain simple values, such as strings, integers, and colors.
                                      
                                                         arrays.xml for resource arrays (typed arrays).
                                                         colors.xml for color values
                                                         dimens.xml for dimension values.
                                                         strings.xml for string values.
                                                         styles.xml for styles.

 -> /res/drawable-hdpi               -- The hdpi qualifier indicates that the resources in that directory are for devices with a high-density screen.

 -> /res/drawable-ldpi                -- The ldpi qualifier indicates that the resources in that directory are for devices with a low-density screen.

 -> /res/drawable-mdpi              -- The mdpi qualifier indicates that the resources in that directory are for devices with a medium-density screen.

 -> /res/drawable-xhdpi            -- The xhdpi qualifier indicates that the resources in that directory are for devices with a extra Large-density screen.

 -> /res/anim                             -- XML files that define tween animations

 -> /res/color                             -- XML files that define a state list of colors.

 -> /res/menu                            -- XML files that define application menus, such as an Options Menu, Context Menu, or Sub Menu.

 -> /res/raw                               -- Arbitrary files to save in their raw form.

 (5) "assets" folder

This folder also contains external resources used in the application like the /res folder. But the main difference is that the resources are stored in raw format and can be read only programmatically.

 (6) AndroidManifest.xml

The components and settings of an Android application are described in the AndroidManifest.xml file. For example all Activities and Services of the application must be declared in this file. It must also contain the required permissions for the application.

Google Play requires that every Android application uses its own unique package. Therefore it is a good habit to use your reverse domain name as package name. This will avoid collisions with other Android applications. It contains information about various activities, views, services etc.

 I hope this tutorial helps you.

No comments:

Post a Comment