Anatomy of Android Application
The following is a list of directories and files that constitute an Android project:
Serial Number Folder, File & Description
1 AndroidManifest.xml
This is the manifest file which describes the fundamental characteristics of the app
and defines each of its components.
2 java
This contains the .java source files for the project. By default, it includes
MainActivity.java source file having an activity class that runs when the app is
launched using the app icon.
3 res/drawable
This is a directory for drawable objects that is used for putting static icons or
images files like .png, .jpg, or XML files that are compiled into bitmaps, state lists,
shapes, animation drawable. .
4 res/layout
This is a directory for files that define your app's user interface.
5 res/values
This is a directory for other various XML files that contain a collection of
resources, such as strings and colors definitions.
Mobile Application Development Page 1 of 5
The following is a brief overview of the important application files.
The Main Activity File
The main activity code is a Java file MainActivity.java. This is the actual application file which ultimately gets
converted to a Dalvik executable and runs the application. The following is the default code generated by the
application wizard for Hello World application:
package com.example.helloandroid;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
R.layout.activity_main refers to the activity_main.xml file located in the res/layout folder. The onCreate()
method is one of many methods that are fired when an activity is loaded. R file is the glue between the activity
Java files like MainActivity.java and the resources like strings.xml. Generally, the R class is a special Java class
which enables Android to utilize the resources in a simpler way compared to accessing via file paths. The
argument of the setContentView() method is R.layout.activity_main which means “set the content of the activity
to be the layout residing in activity_main.xml”.
The Manifest File
Whatever component you develop as a part of your application, you must declare all its components in a
manifest file called AndroidManifest.xml which resides at the root of the application project directory. This file
works as an interface between Android OS and the application, so if you do not declare your component in this
file, then it will not be considered by the OS. E.g., a default manifest file will look like as in the following file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.helloandroid">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
Mobile Application Development Page 2 of 5
The root element needs to declare the Android XML namespace
xmlns:android=http://schemas.android.com/apk/res/android
All other elements will be children of the root and will inherit that namespace declaration.
The <application>...</application> tags enclosed the components related to the application. Attribute
android:icon will point to the application icon available under res/mipmap. The application uses the image
named ic_launcher.
The <activity> tag is used to specify an activity and android:name attribute specifies the fully qualified class
name of the Activity subclass. 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 intent filters must be
declared to be used by all the Android components that wish to be notified via intents, so that Android knows
which intents should go to that component.
The app theme
The android:theme attribute sets the app's theme, which defines the appearance of user interface elements
such as text:
android:theme="@style/AppTheme">
The @string refers to the strings.xml file. Hence, @string/app_name refers to the app_name string defined in the
strings.xml file. Similar way, other strings get populated in the application.
The following is a list of tags that will be used in the 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
Resources
This are all the needed files except the source code. For example, if we want to include an mp3 file in our
project, we place this file inside the “res” folder. The media, image and layout files residing in the resources
folder are accessed via Java code written in MainActivity.java
The Layout File
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 the "HelloAndroid" application, this file will have following content related to the layout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
Mobile Application Development Page 3 of 5
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
TheTextView is an Android view (control) used to build the GUI and it has various attributes like
android:layout_width, android:layout_height, etc., which are 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 refers to the hello string
defined in the strings.xml file, which is "Hello World!"
Values
Defines how the application views look like
Color State List Resource
String Resources
Style Resource
The Strings File: It contains all the text that the 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. For
example, a default string file will look like as follows:
<resources>
<string name="app_name">Hello Android</string>
<string name="hello_world">Hello World</string>
</resources>
Screen sizes
Android runs on a variety of devices that offer different screen sizes and densities i.e. Android can handle
applications that run on small mobile phone devices, as well as applications that run on large tablet densities.
This feature gives Android a great advantage. Generally Android system provides APIs that allow you to control
your application’s UI for specific screen sizes and densities, so as to optimize the UI design for different screen
configurations.
The following is a summary of the screen configurations and an overview of the API and underlying screen-
compatibility features:
i). Screen size: Actual physical size, measured as the screen’s diagonal. Android groups all actual screen sizes
into four generalized sizes: small, normal, large, and extra-large.
ii). Screen density: The quantity of pixels within a physical area of the screen; usually referred to as dpi (dots
per inch). E.g., a "low" density screen has fewer pixels within a given physical area, compared to a "normal"
or "high" density screen. Android groups all actual screen densities into six generalized densities: low,
medium, high, extra-high, extra-extra-high, and extra-extra-extra-high.
iii). Orientation: The orientation of the screen from the user’s point of view. This is either landscape or portrait,
Notice that the orientation can change at runtime when the user rotates the device.
iv). Resolution: The total number of physical pixels on a screen. When adding support for multiple screens,
applications do not work directly with resolution; applications should be concerned only with screen size and
density, as specified by the generalized size and density groups.
Mobile Application Development Page 4 of 5
v). Density-independent pixel (dp): A virtual pixel unit that should be used when defining UI layout, to express
layout dimensions or position in a density-independent way. The density-independent pixel is equivalent to
one physical pixel on a 160 dpi screen, which is the baseline density assumed by the system for a "medium"
density screen. At runtime, the system handles any scaling of the dp units, based on the actual density of the
screen in use. The conversion of dp units to screen pixels is: px = dp * (dpi / 160). You should always use dp
units when defining your application’s UI, to ensure proper display of your UI on screens with different
densities i.e. it ensures that your application not only renders properly, but also provides the best user
experience possible on each screen. The following is a set of six generalized densities:
• ldpi (low) ~120dpi
• mdpi (medium) ~160dpi
• hdpi (high) ~240dpi
• xhdpi (extra-high) ~320dpi
• xxhdpi (extra-extra-high) ~480dpi
• xxxhdpi (extra-extra-extra-high) ~640dpi
Mobile Application Development Page 5 of 5