You are on page 1of 13

CHAPTER 2

APP COMPONENTS & DEVELOPMENT

2.1. Application Building Blocks


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:
2.1.1. Activities
An activity represents a single screen with a user interface. For example, an
email application might have one activity that shows a list of new emails,
another activity to compose an email, and one 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 {

2.1.2. 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:

8
public class MyService extends Service {

2.1.3. 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 broadcasted as an Intent object.

public class MyReceiver extends BroadcastReceiver {

2.1.4. 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 {

9
2.1.5. Additional Components
There are additional components which will be used in the construction of
above mentioned entities, their logic, and wiring between them. These
components are:
Table 2.1.5

Components Description
Fragments Represent a behavior or a portion of
user interface in an Activity.
Views UI elements that are drawn onscreen
including buttons, lists forms etc.
Layouts View hierarchies that control screen
format and appearance of the views.
Intents Messages wiring components
together.
Resources External elements, such as strings,
constants and drawable pictures. It is
an automatically generated file and
you should not modify the content of
the R.java file.
Manifest Configurations file for the application.
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.

10
2.2. ENVIRONMENT SETUP

2.2.1. System Requirements


Android application development on either of the following operating systems

Microsoft Windows 10/8/7/Vista/2003 (32 or 64-bit)


Mac OS X 10.8.5 or higher, up to 10.9 (Mavericks)
GNOME or KDE desktop
Second point is that all the required tools to develop Android applications are
open source and can be downloaded from the Web. Following is the list of
software's you will need before you start your Android application
programming.

Java JDK5 or later version


Java Runtime Environment (JRE) 6
Android Studio
Minimum RAM 2GB
Minimum Hard Disk 50 GB
Processors i3 or above
2.2.2. Setup Android Studio
Overview
Android Studio is the official IDE for android application development. It
works based on IntelliJ IDEA, You can download the latest version of
android studio from Android Studio 2.2 Download, If you are new to
installing Android Studio on windows, you will find a file, which is named
as android-studio-bundle-143.3101438-windows.exe.So just download and
run on windows machine according to android studio wizard guideline. If you
are installing Android Studio on Mac or Linux, You can download the latest
version from Android Studio Mac Download, or Android Studio Linux
Download, check the instructions provided along with the downloaded file for
Mac OS and Linux. This tutorial will consider that you are going to setup
your environment on Windows machine having Windows 8.1 operating
system.

11
Installation
So let's launch Android Studio.exe, Make sure before launch Android Studio,
Our Machine should require install0ed Java JDK.

Figure 2.2.2(a)

Once you launched Android Studio, its time to mention JDK path in installer.

Figure 2.2.2(b)

12
Below the image initiating JDK to android SDK

Figure 2.2.2(c)

Need to check components required to create applications, below image has


selected Android Studio, Android SDK, Android Virtual
Machine and performance (Intel chip).

13
Need to specify the location of local machine path for Android studio and
Android SDK, below the image has taken default location of windows 8.1 x64
bit architecture.

Figure 2.2.2(d)

Need to specify the ram space for Android emulator by default it would take
512MB of local machine RAM.

14
At final stage, it would extract SDK packages into our local machine, it would
take a while time to finish the task and would take 2626MB of Hard disk
space.

Figure 2.2.2(e)
After done above all, click finish button & it going to be open android studio
project with Welcome to android studio message as shown below

15
You can start your application development by calling start a new android
studio project. in a new installation frame should ask Application name,
package information and location of the project.

After entered application name, it going to be called select the form factors
your application runs on, here need to specify Minimum SDK, in our tutorial,
I have declared as API23: Android 6.0(Mashmallow).

16
The next level of installation should contain selecting the activity to mobile;
it specifies the default layout for Applications.

At the final stage, it going to be open development tool to write the


application code.

17
2.2.3. Create Android Virtual Device
To test your Android applications, you will need a virtual Android device. So
before we start writing our code, let us create an Android virtual device.
Launch Android AVD Manager Clicking AVD_Manager icon as shown below

Figure 2.2.3
After Click on a virtual device icon, it going to be shown by default virtual
devices which are present on your SDK, or else need to create a virtual device
by clicking Create new Virtual device button

If your AVD is created successfully it means your environment is ready for


Android application development. If you like, you can close this window using
top-right cross button. Better you re-start your machine and once you are done

18
with this last step, you are ready to proceed for your first Android example but
before that we will see few more important concepts related to Android
Application Development.

2.2.4. Hello World App Example


Before Writing a Hello world code, you must know about XML tags.To write
hello word code, you should redirect to App>res>layout>Activity_main.xml

To show hello word, we need to call text view with layout ( about text view
and layout, you must take references at Relative Layout and Text View ).
<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"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">

<TextView android:text="@string/hello_world"
android:layout_width="550dp"
android:layout_height="wrap_content" />
</RelativeLayout>

Need to run the program by clicking Run>Run App or shift+f10 key. Finally,
result should be placed at Virtual devices as shown below

19
Figure 2.2.4

20

You might also like