Click here to Skip to main content
15,884,739 members
Articles / Mobile Apps / Android
Article

Basic structure of an Android project

Rate me:
Please Sign up or sign in to vote.
4.80/5 (4 votes)
31 May 2012CPOL4 min read 140.8K   1.7K   33   5
The basic structure of an Android project and its major files and classes.

Introduction

In this article I will explain about the basic structure of an Android project and its major files and classes.

Background

I assume that you have some knowledge in Java programming and you know how to develop in Eclipse. In order to develop an Android project, you have to install the Android plug in for Eclipse. In order to do that, choose (in the top menu) Help -> Install new software, and in the window that opens, insert the address: "https://dl-ssl.google.com/android/eclipse/". The installation wizard will guide you to download the full Android SDK if it is necessary.

Basic Android project structure

After you have installed all the plug-ins necessary for the development of an Android file, you can now begin to develop an Android application. From the top menu, choose File -> Project, and from the "New Project window", choose "Android Project". Follow the project setup wizard and after finishing the wizard, you will have a basic Android application. Every Android project contains several folders:

  • src: This folder contains the Java source files.
  • gen: Generated Java library, this library is for Android internal use only.
  • Res: Here we can store resource files such as pictures, XML files for defining layouts, and so forth. Within this folder there are additional folders such as Drawable, Layout, and Values.
  • Drawable: Here we store the various graphic files. We can see three types of drawable folders. This is because there are many Android devices with different screen resolutions. By default, there are several versions of this folder such as: Drawable-mdpi, drawable-hdpi, and so forth. This is required in order to adapt to different screen resolutions.
  • Layout: This is the place for XML layout files. Layout files are XML files which define how various Android objects (such as textboxes, buttons, etc.) are organized on the screen.
  • Values: XML files which store various string values (titles, labels, etc.).

Major files in the Android project:

  • AndroidManifest.xml: This is the Android definition file. It contains information about the Android application such as minimum Android version, permission to access Android device capabilities such as internet access permission, ability to use phone permission, etc.
  • MainLayout.xml: This file describes the layout of the page. This means the placement of every component (such as textboxes, labels, radio buttons, user defined components, etc.) on the app screen.
  • Activity class: Every application that occupies the entire device screen needs at least one class which inherits from the Activity class. One major method is called OnCreate. This method initiates the app and loads the layout page.

The Android manifest file

A typical Android.Manifest file looks like this:

XML
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.firstproject"
    android:versionCode="1"
    android:versionName="1.0" >
		
    <uses-sdk android:minSdkVersion="7" />
	
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".MynewprojectActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
		<category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

Some important definitions in this file:

  • Android:versionCode: This is a numerical value. This is an important attribute because that is how the Android device knows when to alert the user to upgrade the application to a newer version.
  • Android:sdkMinVersion: Defines what is the earliest version of the Android operating system available to this application.
  • Activity element: Defines what the activities available in this application are and which activity has to be loaded at startup.

Layout file

A typical layout file looks like this:

XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

</LinearLayout>

This layout defines one object, a TextView. This is a text label and it is used when a programmer wants to place a text title in your application.

Activity class

One major class is the Activity class. We already defined this class in the android.manifest file as the main activity class. Which is the first class to be loaded and executed right after the Android application will be launched:

Java
package com.firstproject;

import android.app.Activity;
import android.os.Bundle;

public class MynewprojectActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

We can see here one major method called "onCreate" which is called when the Activity class is activated. In our specific example, we define a layout file by the line setContentView(R.layout.main).

Let's have some code

We want to change the text in the label from "Hello World" to Hello "New Application". First of all we have to link the label in the layout to a Java object. Before that we have to give an ID to our label object. This is done by opening our layout file, which is called in our specific project "main.xml" and is located in the "res/layout" folder.

In the "TextView" element, we have to add the following attribute:

android:id="@+id/myLabel"

After that, let's open out main activity class, which in our case is called "MyfirstandroidprojectActivity.java" and add the following line in the "OnCreate" method:

Java
TextView mTextView = (TextView)this.findViewById(R.id.myLabel);
mTextView.setText("Hello my New Project");

The first line defines the linkage between the "TextView" object on the screen and the Java TextView object, and the second line defines the text string in the "TextView" object.

Summary

This article explains only the fundamentals of programming for Android devices. If you want to see a full featured Android application, you are welcome to see my chess application at http://code.google.com/p/pocket-chess-for-android/. My chess application is also downloadable to Android devices through this address at Google Play: https://play.google.com/store/apps/details?id=kobi.chess&feature=search_result.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Israel Israel
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
SuggestionDemo project is not working. Pin
Member 1095206016-Jul-14 19:23
Member 1095206016-Jul-14 19:23 
QuestionDirectory Structure of an Android Project Pin
boss prabu10-Mar-13 8:25
boss prabu10-Mar-13 8:25 
QuestionThanks Pin
su_penguin7-Jun-12 13:46
su_penguin7-Jun-12 13:46 
SuggestionWhere's the beef? Pin
Slacker00731-May-12 23:55
professionalSlacker00731-May-12 23:55 
GeneralRe: Where's the beef? Pin
wob200019-Aug-12 14:55
wob200019-Aug-12 14:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.