Lecture 3
Lecture 3
Lecture 3
Hassan Ayub
Application Components
Application components are the essential building blocks of an Android application. These
components are loosely coupled by the application manifest file AndroidManifest.xml that
describes each component of the application and how they interact.
There are following four main components that can be used within an Android application
Activities
An activity represents a single screen with a user interface , in-short Activity performs actions on the
screen. For example, an email application might have one activity that shows a list of new emails,
another activity to compose an email, and another activity for reading emails. If an application has more
than one activity, then one of them should be marked as the activity that is presented when the
application is launched.
An activity is implemented as a subclass of Activity class as follows −
2 Views
UI elements that are drawn on-screen including buttons, lists forms etc
3 Layouts
View hierarchies that control screen format and appearance of the views.
4 Intents
Messages wiring components together.
5 Resources
External elements, such as strings, constants and drawable pictures.
6 Manifest
Configuration file for the application.
Create Android Application
DEMO
Anatomy of Android Application
1 Java
This contains the .java source files for your project. By default, it includes an MainActivity.java source file having
an activity class that runs when your app is launched using the app icon.
2 res/drawable-hdpi
This is a directory for drawable objects that are designed for high-density screens.
3 res/layout
This is a directory for files that define your app's user interface.
4 res/values
This is a directory for other various XML files that contain a collection of resources, such as strings and colours
definitions.
5 AndroidManifest.xml
This is the manifest file which describes the fundamental characteristics of the app and defines each of its
components.
6 Build.gradle
This is an auto generated file which contains compileSdkVersion, buildToolsVersion, applicationId,
minSdkVersion, targetSdkVersion, versionCode and versionName
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 your application. Following is the default code generated by
the application wizard for Hello World! application
Here, 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 figured when an activity is loaded.
The Manifest File
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 −
There are many more items which you use to build a good Android application. Apart from coding for the
application, you take care of various other resources like static content that your code uses, such as bitmaps,
colors, layout definitions, user interface strings, animation instructions, and more. These resources are always
maintained separately in various sub-directories under res/ directory of the project.
Sr No Directory & Resource Type
1 anim/
XML files that define property animations. They are saved in res/anim/ folder and accessed from the R.anim class.
2 color/
XML files that define a state list of colors. They are saved in res/color/ and accessed from the R.color class
3 drawable/
Image files like .png, .jpg, .gif or XML files that are compiled into bitmaps, state lists, shapes, animation drawable. They are saved in res/drawable/
and accessed from the R.drawable class.
4
layout/
XML files that define a user interface layout. They are saved in res/layout/ and accessed from the R.layout class
5 menu/
XML files that define application menus, such as an Options Menu, Context Menu, or Sub Menu. They are saved in res/menu/ and accessed from
the R.menu class.
6 raw/
Arbitrary files to save in their raw form. You need to call Resources.openRawResource() with the resource ID, which is R.raw.filename to open
such raw files.
7 Values/
colors.xml for color values, and accessed from the R.color class.
dimens.xml for dimension values, and accessed from the R.dimen class.
strings.xml for string values, and accessed from the R.string class.
styles.xml for styles, and accessed from the R.style class.
8 xml/
Arbitrary XML files that can be read at runtime by calling Resources.getXML(). You can save various configuration files here which will be used at
Alternative Resources
Your application should provide alternative resources to support specific device configurations.
For example, you should include alternative drawable resources ( i.e.images ) for different
screen resolution and alternative string resources for different languages. At runtime, Android
detects the current device configuration and loads the appropriate resources for your
application.
Accessing Resources
During your application development you will need to access defined resources either in your
code, or in your layout XML files. Following section explains how to access your resources in
both the scenarios −
Accessing Resources in Code
When your Android application is compiled, a R class gets generated, which contains resource
IDs for all the resources available in your res/ directory. You can use R class to access that
resource using sub-directory and resource name or directly resource ID.
Example
To access res/drawable/myimage.png and set an ImageView you will use following code −
ImageView imageView = (ImageView) findViewById(R.id.myimageview);
imageView.setImageResource(R.drawable.myimage);
2 onStart()
This callback is called when the activity becomes visible to the user.
3 onResume()
This is called when the user starts interacting with the application.
4 onPause()
The paused activity does not receive user input and cannot execute any code and called when the current
activity is being paused and the previous activity is being resumed.
5 onStop()
This callback is called when the activity is no longer visible.
6 onDestroy()
This callback is called before the activity is destroyed by the system.
7 onRestart()
This callback is called when the activity restarts after stopping it.