Pages

Sunday, August 19, 2012

Let's have a look at code...

In previous articles you learned how to create a project in Android. We have seen that, how to create a Hello World program in Android but, we haven't look for the code of the project. Let's understand each file which play important role in making of an Android Project.

Any Android project can be created in three simple steps.

1) Design your screens with controls.
2) Add or create resources in your project.
3) Write class files.

We'll understand Hello World program in same manner.

1) Design screen : To design your screen you have to go to /res/layout/activity_hello_world.xml which represent you screen layout. Name of this XML file inherit from activity name you have given at the time of project creation.

Graphics layout window of activity_hello_world
This is layout of Hello World application design. On the screen there is only one TextView control having caption with "Hello World!". What ever you design on this screen, the XML code for the design will generate and store in XML file. Here in this example it will store in activity_hello_world.xml.

In the bottom side of the above screen there are two tab given. One is Graphical Layout and another is activity_hello_world.xml.


Above screen is XML window of Hello World project.

Everything in the design should be in layout. Here I have used RelativeLayout. In this layout I have put a TextView control which displays some text.

Each control in design have some attribute to set like, ID, TEXT, WIDTH, HEIGHT as you can see in screen shot. Now, I am finished with design and I have to move to next step which is creating some resources.


2) Create Resources : In this step I'll create some resources but, before that let me explain what exactly resource in Android project.
In small window you can see list of resources you can create. Here in Hello World program I have created some String resources. String resources can be created in strings.xml file with <String></String> tag. Take a look at below screen.
You can see in above screen that there are some strings created with <String> tag and given some Name and Values. You can access these strings anywhere in your Android project in many ways, like in layout file you can use @string/id and you can use these in Java file with use of R class.

Now, resources are ready. Time to go for last step.

3) Write class files : This is the part where main logic of your project exists.All Listener events, Database actions and Control events you'll write here.
You can see HelloWorld.java file code in above screen. Class name is Hello World which extends Activity class. I'll explain in detail about Activity in near future. You just need to understand that for each screen in your application you'll need one class which extends Activity class.

As you can see there are two methods named onCreate and onCreateOptionsMenu. We have to concern with onCreate method for now. This method will call when your Activity runs on the screen. Means, when you run your project or run application in your mobile this method will call. Each Activity has its own onCreate method. You can write code here that you want to run when Activity starts.

OnCreate method has two line already coded.
1. super.onCreate(savedInstanceState) which calls Activity class' onCreate method.
2. setContentView(R.layout.activity_hello_world) which sets activity_hello_world.xml as layout of your Hello World activity.

Thats it, you are done with code of this project!!!

3 comments: