Lecture 3

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 23

Android - Application Components

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 −

public class MainActivity extends Activity { }


Services

 A service is a component that runs in the background to perform


long-running operations. For example, a service might play music in
the background while the user is in a different application, or it
might fetch data over the network without blocking user interaction
with an activity.
 A service is implemented as a subclass of Service class as follows −

public class MyService extends Service { }


Broadcast Receivers

 Broadcast Receivers simply respond to broadcast messages from other


applications or from the system. For example, applications can also initiate
broadcasts to let other applications know that some data has been downloaded to
the device and is available for them to use, so this is broadcast receiver who will
intercept this communication and will initiate appropriate action.
 A broadcast receiver is implemented as a subclass of BroadcastReceiver class
and each message is broadcaster as an Intent object.

public class MyReceiver extends BroadcastReceiver { public void onReceive(context,intent){} }


Content Providers

 A content provider component supplies data from one application to others on


request. Such requests are handled by the methods of the ContentResolver class.
The data may be stored in the file system, the database or somewhere else
entirely.
 A content provider is implemented as a subclass of ContentProvider class and
must implement a standard set of APIs that enable other applications to perform
transactions.
public class MyContentProvider extends ContentProvider { public void onCreate(){} }
Additional Components
Sr No. Components & Description
1 Fragments
Represents a portion of user interface in an Activity.

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

 Before you run your app, you should


be aware of a few directories and
files in the Android project −
Sr No Folder, File & Description

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 −

<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 the android: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
The Strings File

 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. For example, a default
strings file will look like as following file −

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 your
"Hello World!" application, this file will have following content
related to default layout −
Android Resources Organizing & Accessing

 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);

Here first line of the code make use of R.id.myimageview to get


ImageView defined with id myimageview in a Layout file.
Second line of code makes use of R.drawable.myimage to get
an image with name myimage available in drawable sub-
directory under /res.
Example
Activities

 An activity represents a single screen with a user interface


just like window or frame of Java.Android activity is the
subclass of ContextThemeWrapper class.

 If you have worked with C, C++ or Java programming


language then you must have seen that your program starts
from main() function. Very similar way, Android system
initiates its program with in an Activity starting with a call
on onCreate() callback method. There is a sequence of
callback methods that start up an activity and a sequence of
callback methods that tear down an activity as shown in the
below Activity life cycle diagram: (image courtesy :
android.com )
Sr. No. Callback & Description
1 onCreate()
This is the first callback and called when the activity is first created.

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.

You might also like