Understanding Anatomy/ Internal Details of an Application
×
Home Courses Community Support
Gray Matter

First Step Towards Android: Introduction »

Understanding Activities and Intents »

Hands On With UI Designing In Android (Controls-Views,Widgets and Layouts) »

Updating... »

Understanding Anatomy/ Internal Details of an Appl..

Android application contains different components such as java source code, string resources, images, manifest file, apk file etc.

 

The Main Activity File (Java Source Code)

The code in the main activity file is a Java file MainActivity.java. This is the actual application file which ultimately gets converted to a Dalvik executable and runs your application. Following is the default code generated by the application wizard for Hello World! application −


Let's see the java source file:

File name: MainActivity.java
package com.example.helloworld;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Textview;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;

public class MainActivity extends Activity {//…………………………………………………(1)

   @Override
   public void onCreate(Bundle savedInstanceState) {//……………………………(2)
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main); //……………………………………………(3)
   }
   
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {//…………………………………(4)
        // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.activity_main, menu);
      return true;
   }
}

 


(1) Activity is a java class that creates a 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, onStart, onPause 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.


 

The Layout File (xml code)

The activity_main.xml is a layout file available in res/layout directory, that is referenced by your application when building its interface. You will modify this file very frequently to change the layout of your application. For your "Hello World!" application, this file will have following content related to default layout –


File name: activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent" >
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerHorizontal="true"
      android:layout_centerVertical="true"
      android:padding="@dimen/padding_medium"
      android:text="@string/hello_world"
      tools:context=".MainActivity" />
</RelativeLayout>

 

 

The Strings File (xml code)

The strings.xml file is located in the res/values folder and it contains all the text that your application uses. For example, the names of buttons, labels, default text, and similar types of strings go into this file. This file is responsible for their textual content.The TextView is an Android control which is created by framework used to build the GUI and it have various attributes like android:layout_width, android:layout_height etc which are being used to set its width and height etc.. The @string refers to the strings.xml file located in the res/values folder. Hence, @string/hello_world  provides the information about the textview message.refers to the hello string defined in the strings.xml file, which is "Hello World!".

File name: strings.xml

<resources>
   <string name="app_name">HelloWorld</string>
   <string name="hello_world">Hello world!</string>
   <string name="menu_settings">Settings</string>
   <string name="title_activity_main">MainActivity</string>
</resources>


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

 

The R file (Generated R.java)

The gen/com.example.helloworld/R.java file is the glue between the activity Java files like MainActivity.java and the resources like strings.xml. It is an automatically generated file and you should not modify the content of the R.java file.


This file 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 name: R.java

/* AUTO-GENERATED FILE.  DO NOT MODIFY.
 *
 * This class was automatically generated by the
 * aapt tool from the resource data it found.  It
 * should not be modified by hand.
 */

package com.example.helloworld;

public final class R {
   public static final class attr {
   }
   
   public static final class dimen {
      public static final int padding_large=0x7f040002;
      public static final int padding_medium=0x7f040001;
      public static final int padding_small=0x7f040000;
   }
   
   public static final class drawable {
      public static final int ic_action_search=0x7f020000;
      public static final int ic_launcher=0x7f020001;
   }
   
   public static final class id {
      public static final int menu_settings=0x7f080000;
   }
   
   public static final class layout {
      public static final int activity_main=0x7f030000;
   }
   
   public static final class menu {
      public static final int activity_main=0x7f070000;
   }
   
   public static final class string {
      public static final int app_name=0x7f050000;
      public static final int hello_world=0x7f050001;
      public static final int menu_settings=0x7f050002;
      public static final int title_activity_main=0x7f050003;
   }
   
   public static final class style {
      public static final int AppTheme=0x7f060000;
   }
}

 

 

The Manifest File (manifest.xml)

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

Whatever component you develop as a part of your application, you must declare all its components in a manifest.xml which resides at the root of the application project directory. This file works as an interface between Android OS and your application, so if you do not declare your component in this file, then it will not be considered by the OS. For example, a default manifest file will look like as following file −
 

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.helloworld"
   android:versionCode="1"
   android:versionName="1.0" >
   
   <uses-sdk
      android:minSdkVersion="8"
      android:targetSdkVersion="22" />
   
   <application
       android:icon="@drawable/ic_launcher"
       android:label="@string/app_name"
       android:theme="@style/AppTheme" >
       
       <activity
          android:name=".MainActivity"
          android:label="@string/title_activity_main" >
       
          <intent-filter>
             <action android:name="android.intent.action.MAIN" />
             <category android:name="android.intent.category.LAUNCHER"/>
          </intent-filter>
       
       </activity>
       
   </application>
</manifest>

 


Here <application>...</application> tags enclosed the components related to the application. Attribute android:icon will point to the application icon available under res/drawable-hdpi. The application uses the image named ic_launcher.png located in the drawable folders.
 

The <activity> tag is used to specify an activity and android:name attribute specifies the fully qualified class name of the Activity subclass and theandroid:label attributes specifies a string to use as the label for the activity. You can specify multiple activities using <activity> tags.
 

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-filter is named android.intent.category.LAUNCHER to indicate that the application can be launched from the device's launcher icon.
 

The @string refers to the strings.xml file explained below. Hence,@string/app_name refers to the app_name string defined in the strings.xml file, which is "HelloWorld". Similar way, other strings get populated in the application.
 

Following is the list of tags which you will use in your manifest file to specify different Android application components:

  • <activity>elements for activities
     
  • <service> elements for services
     
  • <receiver> elements for broadcast receivers
     
  • <provider> elements for content providers

 

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.

 

  • SRC - It contains package where java resource files are stored. It is the folder where the project's source files are located. For example, if you are working on a Java project, src folder and its sub folders would hold the .java files.
     
  • gen - It is an auto-generated file by aapt  (Android Asset Packaging Tool) that contains resource IDs for all the resources of res/ directory.
     
  • assets - An asset is a file that may be referenced in a Web page. It can be a CSS file, a JavaScript file, an image or video file, etc. Assets are located in Web-accessible directories and are directly served by Web servers.
     
  • bin - This folder is usually where the compiled files are copied to. For example, this folder will have the .class, .jar etc files which are compiled.
    It also contains AndroidManifest.xml file.
    • Difference between src and bin - "src" folder contains the human readable code and "bin" contains the binary that the java virtual machine executes.
       
  • libs - It contains directories ,modules, library files, device drivers etc.
     
  • res - It contains classes for accessing application resources, such as raw asset files, colors, drawable, media or other  files in the package, plus important device configuration details (orientation, input types, etc.) that affect how the application may behave.
    It contains following-
    • Layout - It is a directory that defines app’s user interface like activity_main.xml.
       
    • Menu -  It is a directory that defines app’s menu items.
       
    • Values - It is a Directory for other XML files that contain a collection of resources, such as string and color definitions. The strings.xml file defines the "Hello world!" string that displays when you run the default app.
       
  • AndroidManifest.xml - It contains information of your package, including components of the application such as activities, services, broadcast receivers, content providers etc.

 

Elements of manifest.xml

The elements used in the xml file are described below.

<manifest>

manifest is the root element of the AndroidManifest.xml file. It has package attribute that describes the package name of the activity class.

<application>

application is the sub element of the manifest. It includes the namespace declaration. This element contains several sub elements that declares the application component such as activity etc.
The commonly used attributes are of this element are icon, label, theme etc.
android:icon represents the icon for all the android application components.
android:label works as the default label for all the application components. 
android:theme represents a common theme for all the android activities.
 

<activity>

activity is the sub element of application and represents an activity that must be defined in the AndroidManifest.xml file. It has many attributes such as label, name, theme, launch Mode etc.

android:label represents a label i.e. displayed on the screen.

android:name represents a name for the activity class. It is required attribute.
 

<intent-filter>

It is the sub-element of activity that describes the type of intent to which activity, service or broadcast receiver can respond to.
 

<action>

It adds an action for the intent-filter. The intent-filter must have at least one action element.
 

<category>

It adds a category name to an intent-filter.

 

← Preview

Say Hello to Android (Creating First Android App)

Next →

Activity And Activity Lifecycle

NOTES

Our mission is to provide highly effective and quality education via innovative solutions. Geeksdemy look forward to bridge the gap between in-demand technology and academics in order to deliver innovative, easy, interesting and affordable learning across the Globe.

Courses

  • Python
  • Game Development with Unity 3D
  • Arduino
  • PCB & Circuit Designing
  • Robotics and Embedded C
  • 8051/8052 Embedded Systems
  • QuadCopter & UAV
  • MATLAB with Robotics
  • Raspberry Pi
Learn On the Go!
Get the Android App
Get the iOS App