fastread homefastrread library fastread menu

Android : Internal Details of Hello Android Example

Tutorial by:      Date: 2016-05-10 04:49:30

❰ Previous Next ❱

Here, we are going to learn the internal details or working of hello android example.

Android application contains different components such as java source code, string resources, images, manifest file, apk file etc. Let's understand the project structure of android application.

Project Structure of Hello Android example


Java Source Code

Let's see the java source file created by the Eclipse IDE:

File: MainActivity.java
 
  1. package com.example.helloandroid;  
  2. import android.os.Bundle;  
  3. import android.app.Activity;  
  4. import android.view.Menu;  
  5. import android.widget.TextView;  
  6. public class MainActivity extends Activity {//(1)  
  7.     @Override  
  8.     protected void onCreate(Bundle savedInstanceState) {//(2)  
  9.         super.onCreate(savedInstanceState);  
  10.                 
  11.         setContentView(R.layout.activity_main);//(3)  
  12.     }  
  13.     @Override  
  14.     public boolean onCreateOptionsMenu(Menu menu) {//(4)  
  15.         // Inflate the menu; this adds items to the action bar if it is present.  
  16.         getMenuInflater().inflate(R.menu.activity_main, menu);  
  17.         return true;  
  18.     }  
  19. }  

(1) Activity is a java class that creates and default window on the screen where we can place different components such as Button, EditText, TextView, Spinner etc. It is like the Frame of Java AWT.

It provides life cycle methods for activity such as onCreate, onStop, OnResume etc.

(2) The onCreate method is called when Activity class is first created.

(3) The setContentView(R.layout.activity_main) gives information about our layout resource. Here, our layout resources are defined in activity_main.xml file.

File: activity_main.xml
 
  1. <RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity" >  
  6.     <TextView  
  7.         android:layout_width="wrap_content"  
  8.         android:layout_height="wrap_content"  
  9.         android:layout_centerHorizontal="true"  
  10.         android:layout_centerVertical="true"  
  11.         android:text="@string/hello_world" />  
  12. </RelativeLayout>  

As you can see, a textview is created by the framework automatically. But the message for this string is defined in the strings.xml file. The @string/hello_world provides information about the textview message. The value of the attribute hello_world is defined in the strings.xml file.

File: strings.xml
 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string name="app_name">helloandroid</string>  
  4.     <string name="hello_world">Hello world!</string>  
  5.     <string name="menu_settings">Settings</string>  
  6. </resources>  

You can change the value of the hello_world attribute from this file.


Generated R.java file

It is the auto-generated file that contains IDs for all the resources of res directory. It is generated by aapt(Android Asset Packaging Tool). Whenever you create any component on activity_main, a corresponding ID is created in the R.java file which can be used in the Java Source file later.

File: R.java
 
  1. /* AUTO-GENERATED FILE.  DO NOT MODIFY. 
  2.  * 
  3.  * This class was automatically generated by the 
  4.  * aapt tool from the resource data it found.  It 
  5.  * should not be modified by hand. 
  6.  */  
  7. package com.example.helloandroid;  
  8. public final class R {  
  9.     public static final class attr {  
  10.     }  
  11.     public static final class drawable {  
  12.         public static final int ic_launcher=0x7f020000;  
  13.     }  
  14.     public static final class id {  
  15.         public static final int menu_settings=0x7f070000;  
  16.     }  
  17.     public static final class layout {  
  18.         public static final int activity_main=0x7f030000;  
  19.     }  
  20.     public static final class menu {  
  21.         public static final int activity_main=0x7f060000;  
  22.     }  
  23.     public static final class string {  
  24.         public static final int app_name=0x7f040000;  
  25.         public static final int hello_world=0x7f040001;  
  26.         public static final int menu_settings=0x7f040002;  
  27.     }  
  28.     public static final class style {  
  29.         /**  
  30.         Base application theme, dependent on API level. This theme is replaced 
  31.         by AppBaseTheme from res/values-vXX/styles.xml on newer devices. 
  32.             Theme customizations available in newer API levels can go in 
  33.             res/values-vXX/styles.xml, while customizations related to 
  34.             backward-compatibility can go here. 
  35.          Base application theme for API 11+. This theme completely replaces 
  36.         AppBaseTheme from res/values/styles.xml on API 11+ devices. 
  37.   API 11 theme customizations can go here.  
  38.         Base application theme for API 14+. This theme completely replaces 
  39.         AppBaseTheme from BOTH res/values/styles.xml and 
  40.         res/values-v11/styles.xml on API 14+ devices. 
  41.  API 14 theme customizations can go here.  
  42.          */  
  43.         public static final int AppBaseTheme=0x7f050000;  
  44.         /**  Application theme.  
  45.  All customizations that are NOT specific to a particular API-level can go here.  
  46.          */  
  47.         public static final int AppTheme=0x7f050001;  
  48.     }  
  49. }  

APK File

An apk file is created by the framework automatically. If you want to run the android application on the mobile, transfer and install it.


Resources

It contains resource files including activity_main, strings, styles etc.


Manifest file

It contains information about package including components such as activities, services, content providers etc.

❰ Previous Next ❱