Android Development by Netero #3 05-15-2014, 02:56 PM
#1
Today we will be going in depth in the anatomy of android application.
Now that you have created your first Hello World Android application, it is time to dissect
the innards of the Android project and examine all the parts that make everything work.
First, note the various files that make up an Android project in the
Package Explorer in Eclipse
![[Image: GwmE9lQ.png]](http://i.imgur.com/GwmE9lQ.png)
The various folders and their files are as follows:
➤➤ src —> Contains the .java source files for your project. In this example, there is one file, MainActivity.java.
The MainActivity.java file is the source file for your activity. You will write the code for your application in this file.
➤➤ Android 4.4.2 —> This item contains one file, android.jar, which contains all the class libraries needed for an Android application.
➤➤ gen —> Contains the R.java file, a compiler-generated file that references all the resources found in your project.
You should not modify this file.
➤➤ assets —> This folder contains all the assets used by your application, such as HTML, text files, databases, etc.
➤➤ res —> This folder contains all the resources used in your application. It also contains a few other
subfolders: drawable-<resolution>, layout, and values. In next tutorials we will talks more about
how you can support devices with different screen resolutions and densities.
➤➤ AndroidManifest.xml —> This is the manifest file for your Android application. Here you specify the permissions
needed by your application, as well as other features (such as intent-filters,receivers, etc.). [More on this in next tutorials]
In Second Tutorial you learned that an Activity is a window that contains the GUI "Graphical User Interface" of the program.
Typically an application can have one or more activity, the soul purpose of an activity is to interact with the user.
Life Cycle: is a number of stages which an activity goes through from the moment of creation.
Intent: is the "Glue" that enables different activities from different applications to work together, as if they belong to same application.
Example: getting access to the message activity from contacts.
Now we got the basic Idea of activity and we defined a few terms now lets start coding
.
To create a new Activity , you need to create a new Java class that extends Activity.
For our code today we don't need to create a new class , we can use our old application
A Class is basically a container of codes used in the application. Classes in Java begin by keyword
public: this means the class or method is accessible by other classes or methods.
Now lets analyze the code we wrote.
package com.hackacademy.FirstApplication; this line of code is to identify the package that we are working on.
import android.app.Activity;
import android.os.Bundle; I said in the second tutorial that we will use codes written by android developers or other developers.
We can call those written codes by using the import keyword, now we use import to get codes from two other pre-defined classes which are
import android.app.Activity; calls the Activity class in package app of android.
import android.os.Bundle; calls the Bundle class in package os.
public class MainActivity extends Activity
this line of code defines the start of our program.
Each class or methods start by putting the codes in a { "code here" } block.
public is a keyword special in Java meaning the codes in this class are accessed by other classes or methods as stated above.
class a keyword used to declare a class.
MainActivity after public class there comes the name. MainActivity is the name of our class.
extends this keyword means that our class uses codes from other resources, which is defined after this keyword.
Activity is the class which MainActivity gets some of its code.
Then we arrive to theWhich is responsible for loading the activity_main.xml file "Activity".
This is called a method you will use a lot of these in your programming.
Methods are a collection of codes which you can refer to them by using the name of the method.[More on this later]
Your activity class loads its UI component using the XML file defined in your res/layout folder. In
this example, you would load the UI from the activity_main.xml
[More on how you can change this first UI to start later]
The stages an Activity goes through are
![[Image: rVnSi.png]](http://i.stack.imgur.com/rVnSi.png)
➤ onCreate() — Called when the activity is fi rst created
➤ onStart() — Called when the activity becomes visible to the user
➤ onResume() — Called when the activity starts interacting with the user
➤ onPause() — Called when the current activity is being paused and the previous activity is being resumed
➤ onStop() — Called when the activity is no longer visible to the user
➤ onDestroy() — Called before the activity is destroyed by the system (either manually or by the system to conserve memory)
➤ onRestart() — Called when the activity has been stopped and is restarting again
Now we will write the codes that represent the stages in activity life cycle.
We create a new String "Set of characters" named tag and stores a value of Events.
String values must be written between a double quote "like so".
the (super.) calls the parent class extended class in this example Activity and calls its onStart(),onRestart() ....etc methods depending on the method which we use it in.
Press F11 to debug the program "if it didn't work, run it as explained in Second Tutorial"
Then click on Debug Prespective and you should be able to see this.
When you click the back button this should be printed
When you click the home button this should be printed
Some additional Info's
Press CRTL + SHIFT + F to format the code in eclipse.
Comments are used to document the program, readable only by human , ignored by computer.
Comments help the readability of your program by other programmers.
We have two type of comments
Single line comment // double slashes is used. can only be used on one line.
Multiple line comment /**/ this can spread over multiple lines.
The comment that you want to write is written between the two /*comment here */.
This part of the tutorial was difficult for me to write because it contains many things that happen
in Android, for that reason please if you have a question or you cannot understand something
post in this thread and I will answer as soon as I can
Resources Google Android Development website & a book on android development.
Regards Netero
Now that you have created your first Hello World Android application, it is time to dissect
the innards of the Android project and examine all the parts that make everything work.
First, note the various files that make up an Android project in the
Package Explorer in Eclipse
Spoiler: Package folders
![[Image: GwmE9lQ.png]](http://i.imgur.com/GwmE9lQ.png)
The various folders and their files are as follows:
➤➤ src —> Contains the .java source files for your project. In this example, there is one file, MainActivity.java.
The MainActivity.java file is the source file for your activity. You will write the code for your application in this file.
➤➤ Android 4.4.2 —> This item contains one file, android.jar, which contains all the class libraries needed for an Android application.
➤➤ gen —> Contains the R.java file, a compiler-generated file that references all the resources found in your project.
You should not modify this file.
➤➤ assets —> This folder contains all the assets used by your application, such as HTML, text files, databases, etc.
➤➤ res —> This folder contains all the resources used in your application. It also contains a few other
subfolders: drawable-<resolution>, layout, and values. In next tutorials we will talks more about
how you can support devices with different screen resolutions and densities.
➤➤ AndroidManifest.xml —> This is the manifest file for your Android application. Here you specify the permissions
needed by your application, as well as other features (such as intent-filters,receivers, etc.). [More on this in next tutorials]
Spoiler: More on Android manifest
The AndroidManifest.xml file contains detailed information about the application:
➤➤ It defines the package name of the application as com.hackcommunity.MainActivity.
➤➤ The version code of the application is 1. This value is used to identify the version number of your application.
It can be used to pro-grammatically determine whether an application needs to be upgraded.
➤➤ The version name of the application is 1.0. This string value is mainly used for display to the user.
You should use the format: <major>.<minor>.<point> for this value.
➤➤ The application uses the image named icon.png located in the drawable folder.
➤➤ The name of this application is the string named app_name defined in the strings.xml file.
➤➤ There is one activity in the application represented by the MainActivity.java file.
The label displayed for this activity is the same as the application name.
➤➤ Within the definition for this activity, there is an element named <intent-filter>.
➤➤ The action for the intent filter is named android.intent.action.MAIN
to indicate that this activity serves as the entry point for the application.
➤➤ The category for the intent-fi lter is named android.intent.category.LAUNCHER to indicate that the application can be launched from the device’s Launcher icon.
➤➤ Finally, the android:minSdkVersion attribute of the <uses-sdk> element specifies the minimum version of the OS on which the application will run.
➤➤ It defines the package name of the application as com.hackcommunity.MainActivity.
➤➤ The version code of the application is 1. This value is used to identify the version number of your application.
It can be used to pro-grammatically determine whether an application needs to be upgraded.
➤➤ The version name of the application is 1.0. This string value is mainly used for display to the user.
You should use the format: <major>.<minor>.<point> for this value.
➤➤ The application uses the image named icon.png located in the drawable folder.
➤➤ The name of this application is the string named app_name defined in the strings.xml file.
➤➤ There is one activity in the application represented by the MainActivity.java file.
The label displayed for this activity is the same as the application name.
➤➤ Within the definition for this activity, there is an element named <intent-filter>.
➤➤ The action for the intent filter is named android.intent.action.MAIN
to indicate that this activity serves as the entry point for the application.
➤➤ The category for the intent-fi lter is named android.intent.category.LAUNCHER to indicate that the application can be launched from the device’s Launcher icon.
➤➤ Finally, the android:minSdkVersion attribute of the <uses-sdk> element specifies the minimum version of the OS on which the application will run.
Spoiler: Code in the manifest file
These codes might be different in your manifest file.
Code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hackacademy.helloworld"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.hackacademy.helloworld.MainActivity"
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>In Second Tutorial you learned that an Activity is a window that contains the GUI "Graphical User Interface" of the program.
Typically an application can have one or more activity, the soul purpose of an activity is to interact with the user.
Life Cycle: is a number of stages which an activity goes through from the moment of creation.
Intent: is the "Glue" that enables different activities from different applications to work together, as if they belong to same application.
Example: getting access to the message activity from contacts.
Now we got the basic Idea of activity and we defined a few terms now lets start coding
.To create a new Activity , you need to create a new Java class that extends Activity.
For our code today we don't need to create a new class , we can use our old application
Spoiler: How to create a new Class
File->New->Class
![[Image: mNbIVoV.png]](http://i.imgur.com/mNbIVoV.png)
Then you will have another window open, just type the name of the class you want to create and click Finish
![[Image: mNbIVoV.png]](http://i.imgur.com/mNbIVoV.png)
Then you will have another window open, just type the name of the class you want to create and click Finish
A Class is basically a container of codes used in the application. Classes in Java begin by keyword
public: this means the class or method is accessible by other classes or methods.
Code:
package com.hackacademy.FirstApplication;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}//end of method onCreate
}//end of classNow lets analyze the code we wrote.
package com.hackacademy.FirstApplication; this line of code is to identify the package that we are working on.
import android.app.Activity;
import android.os.Bundle; I said in the second tutorial that we will use codes written by android developers or other developers.
We can call those written codes by using the import keyword, now we use import to get codes from two other pre-defined classes which are
import android.app.Activity; calls the Activity class in package app of android.
import android.os.Bundle; calls the Bundle class in package os.
public class MainActivity extends Activity
this line of code defines the start of our program.
Each class or methods start by putting the codes in a { "code here" } block.
public is a keyword special in Java meaning the codes in this class are accessed by other classes or methods as stated above.
class a keyword used to declare a class.
MainActivity after public class there comes the name. MainActivity is the name of our class.
extends this keyword means that our class uses codes from other resources, which is defined after this keyword.
Activity is the class which MainActivity gets some of its code.
Then we arrive to the
Code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}//end of method onCreateThis is called a method you will use a lot of these in your programming.
Methods are a collection of codes which you can refer to them by using the name of the method.[More on this later]
Your activity class loads its UI component using the XML file defined in your res/layout folder. In
this example, you would load the UI from the activity_main.xml
[More on how you can change this first UI to start later]
The stages an Activity goes through are
Spoiler: Image Description
![[Image: rVnSi.png]](http://i.stack.imgur.com/rVnSi.png)
➤ onCreate() — Called when the activity is fi rst created
➤ onStart() — Called when the activity becomes visible to the user
➤ onResume() — Called when the activity starts interacting with the user
➤ onPause() — Called when the current activity is being paused and the previous activity is being resumed
➤ onStop() — Called when the activity is no longer visible to the user
➤ onDestroy() — Called before the activity is destroyed by the system (either manually or by the system to conserve memory)
➤ onRestart() — Called when the activity has been stopped and is restarting again
Now we will write the codes that represent the stages in activity life cycle.
Code:
package com.hackacademy.FirstApplication;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
String tag = “Lifecycle”;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(tag, “In the onCreate() event”);
}//end of method onCreate
public void onStart(){
super.onStart();
Log.d(tag, “In the onStart() event”);
}
public void onRestart(){
super.onRestart();
Log.d(tag, “In the onRestart() event”);
}
public void onResume(){
super.onResume();
Log.d(tag, “In the onResume() event”);
}
public void onPause(){
super.onPause();
Log.d(tag, “In the onPause() event”);
}
public void onStop(){
super.onStop();
Log.d(tag, “In the onStop() event”);
}
public void onDestroy(){
super.onDestroy();
Log.d(tag, “In the onDestroy() event”);
}
}//end of classWe create a new String "Set of characters" named tag and stores a value of Events.
String values must be written between a double quote "like so".
the (super.) calls the parent class extended class in this example Activity and calls its onStart(),onRestart() ....etc methods depending on the method which we use it in.
Press F11 to debug the program "if it didn't work, run it as explained in Second Tutorial"
Then click on Debug Prespective and you should be able to see this.
Code:
12-28 13:45:28.115: DEBUG/Events(334): In the onCreate() event
12-28 13:45:28.115: DEBUG/Events(334): In the onStart() event
12-28 13:45:28.115: DEBUG/Events(334): In the onResume() eventWhen you click the back button this should be printed
Code:
12-28 13:59:46.266: DEBUG/Events(334): In the onPause() event
12-28 13:59:46.806: DEBUG/Events(334): In the onStop() event
12-28 13:59:46.806: DEBUG/Events(334): In the onDestroy() eventWhen you click the home button this should be printed
Code:
12-28 14:00:54.115: DEBUG/Events(334): In the onCreate() event
12-28 14:00:54.156: DEBUG/Events(334): In the onStart() event
12-28 14:00:54.156: DEBUG/Events(334): In the onResume() eventQuote:As you can see from this simple experiment, an activity is destroyed when you press the Back button.
This is crucial to know, as whatever state the activity is currently in will be lost; hence,
you need to write additional code in your activity to preserve its state when it is destroyed
"We will study this in later tutorials".At this point, note that the onPause() event is called in
both scenarios — when an activity is sent to the background, as well as when it is killed when the
user presses the Back button. When an activity is started, the onStart() and onResume() events are
always called, regardless of whether the activity is restored from the background or newly created.
Some additional Info's
Press CRTL + SHIFT + F to format the code in eclipse.
Comments are used to document the program, readable only by human , ignored by computer.
Comments help the readability of your program by other programmers.
We have two type of comments
Single line comment // double slashes is used. can only be used on one line.
Multiple line comment /**/ this can spread over multiple lines.
The comment that you want to write is written between the two /*comment here */.
This part of the tutorial was difficult for me to write because it contains many things that happen
in Android, for that reason please if you have a question or you cannot understand something
post in this thread and I will answer as soon as I can
Resources Google Android Development website & a book on android development.
Regards Netero
I'm too busy with Learning new things. Will check as much as I can.




![[+]](https://sinister.li/images/modern/collapse_collapsed.png)