Coding Android applications Part 3

Vuyiswamb
Posted by in Android category on for Beginner level | Points: 250 | Views : 1034 red flag

Now that we understand the basics of the IDE Eclipse, we should understand the file structure that is presented to us. A .NET Developer will notice that the file structure is somehow complex as compared to what one is used to.
Recommendation
Read Developing Android applications Part 2 before this article.

Introduction

Now that we understand the basics of the IDE Eclipse, we should understand the file structure that is presented to us. A .NET Developer will notice that the file structure is somehow complex as compared to what one is used to. 

Objective
The main objective is to get you familiar with the file structure and how the code is coupled together.  Let us get started quickly. 

General View  

The file and directory structure in Eclipse for an Android application might look complex, but it’s not. The names of files and directories are named differently as compared to what a Visual Studio does. Figure depicts the File structure 
Figure 1.1  

As you can see at the Top is the name of our Project “HelloWorld” and there are files which are inside our project. This is not different from what a visual studio developer see. I will explain the directories and files that are found here and even some important sub directories that are found in this example project.  
gen 
This directory contains Java generated code and libraries. This are used by Java. In .NET or Visual studio we do come across generated code after creating a project which is not empty.   

Assets 
This directory is usually empty, you can store files that you might want to access in your app and when your projects create an apk, this folder will be one of the folders inside the apk  
Bin 
This is the same bin directory that we have in Visual studio that gets regenerated every time you compile your Project by building it. This bin folder is the one that contains the setup file for Android which is a file with an extension of .apk 
Libs 
This directory have external libraries that you want to use in your project. Sometimes you might want to reuse a functionality that you don’t want to redo, this is the place where you can store the libraries. The libraries in .NET comes in a form of dll’s and in Android development they come in a form of .Jar extension. 
 
Res 
The res directory contains UI, images and other important files that will be explained later. The res directory has other directories inside it. Below is the explanation of each directory 

Drawable 

This folder contains images, you will notice that there are five different types of Drawables. These folders contains the same image with different sizes. When creating the Project in Part 2 of this article, in the wizard there was a part where we had to upload an image. That image was sliced into smaller images of different sizes to accommodate different devices with different sizes. We don’t have this kind of functionality in visual studio.  

Layout 

This is where the UI are stored. In Android development, the UI and the code behind are stored in different places. The UI comes in form of XML, the same way HTML or the asp.net is. The difference is that the Android UI is .XML and the ASP.NET page is .aspx 

Below is an example code of the UI or Activity


There are different kinds of layouts and that is beyond the scope of this article. If you have developed a Silverlight or a WPF application you will note as similarities. There is namespace declarations, the height and width of the screen or activity and we also have a TextView which is similar to a Label in Visual studio development.  

Menu 

This directory contains xml description for menus. This is rarely used especially if you have your own layout that is more flexible and attractive.  

Values 

This folder contains constants, this that you want to use throughout the project that will not change. E.g. Application name on the title bar of each window or Activity, a Timer Value for a Clock etc... 

Android manifest 
The Android manifest is similar to a Web config in ASP.NET, it perform exactly the same functions. Below is an example of an Android manifest file



In the manifest file, you can see I can tell the app what is minimum version and the target version of the Android sdk that I want, I can also tell it to start an Activity and I can also tell what the name of my apk is, I can also tell it when to find an icon to launch the application. This is self-explanatory I will not go deeper on it.  
Src 
NB: I placed this at the end so I explain the connection between XML and Java

This directory contains the .Java files. This Files can either be Classes or Activity classes. Now when I say Activity Classes I mean the Java Files that are coupled with an Activity. We go deeper on that subject later on. In asp.net this could be our code behind files. You know for each asp.net page there is a .cs file which is coupled with the asp.net page. These is similar to that , these are cs files , the difference here is that they are not stored in the same place as their UI file (XML) asp.net Page. Below is an example of the code 




This is where some minor differences, but I will not try to teach you the languages as it will be beyond the scope of the article. On the code above, you will note that there is a class named “MainActivity” this class extends Activity. As you know Java and C# don’t support multiple inheritance, they implement or extend an interface.  So here we see that part in action. If a Class extends to Activity interface this means that this is the code behind for the UI. When we move down to the code there is a method named “onCreate”, this is the same as PageLoad in asp.net, though ASP.Net has other events that gets fired before Page load, “onCreate” in Android is close to what “Page Load” is doing in asp.net.   

One can ask how the code behind is connected to the XML because there is nothing in the XML UI that connects the two.


Conclusion 

I would like to conclude part 3 here, so that I can give you time to digest what I just explained. You saw the code you did not write and you are close to coding yourself. In the next article Part 4, we will write code.

Page copy protected against web site content infringement by Copyscape

About the Author

Vuyiswamb
Full Name: Vuyiswa Maseko
Member Level: NotApplicable
Member Status: Member,MVP,Administrator
Member Since: 7/6/2008 11:50:44 PM
Country: South Africa
Thank you for posting at Dotnetfunda [Administrator]
http://www.Dotnetfunda.com
Vuyiswa Junius Maseko is a Founder of Vimalsoft (Pty) Ltd (http://www.vimalsoft.com/) and a forum moderator at www.DotnetFunda. Vuyiswa has been developing for 16 years now. his major strength are C# 1.1,2.0,3.0,3.5,4.0,4.5 and vb.net and sql and his interest were in asp.net, c#, Silverlight,wpf,wcf, wwf and now his interests are in Kinect for Windows,Unity 3D. He has been using .net since the beta version of it. Vuyiswa believes that Kinect and Hololen is the next generation of computing.Thanks to people like Chris Maunder (codeproject), Colin Angus Mackay (codeproject), Dave Kreskowiak (Codeproject), Sheo Narayan (.Netfunda),Rajesh Kumar(Microsoft) They have made vuyiswa what he is today.

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)