Thursday 8 September 2016

Android Tutorial #2 (Basic components of Android)



Android

This tutorial helps you understand in depth the basic components of an Android application.

Components


1. Java files

Java is a programming language and computing platform first released by Sun Microsystems in 1995.
The Java files contain pieces of code that define how the Android application will react to events at run-time.
The Java files put life into the basic skeleton provided by the layouts allowing the user to interact with the Android application.

App components are the building blocks of an Android application. The system can enter the application from each component, however not all components are user entry points. Each component exists on its own and performs a specific task. Hence basically an Android application is a collection of one or more App components interacting with each other in order to perform specific functions.
There are four type of App components :

A. Activity

The Activity class in Android represents a single screen. Every Java program starts execution from the main function, similarly an Activity starts execution from the onCreate() method. The lifecycle of an Activity is as given below



B. Service 

A service is a component that runs in the background to perform long-running operations or to perform work for remote processes. A service does not provide a user interface.
There are two types of services :
    1. Bounded Service
An application component can bind a service by calling bindService(), such a service offers a client-server interface that allows components to interact with the service, send requests, get results and even supports Inter Process Communication (IPC). The service is bound to the application component that started it and dies when the corresponding application component dies.

    2. Unbounded Service
An unbounded service is started when an application component, such as an activity starts it by calling startService(). Once started a service can run in the background indefinitely, even if the component that started it is destroyed.

Lifecycle of a service is given below.


C. Content Providers

A Content Provider manages a shared set of app data. Data can be stored in the file system, an SQLite database, on the web or any other persistent storage location the application can access. Content providers are useful for reading and writing data that is private to your app and not shared. It is implemented as a subclass of ContentProvider.


D. Broadcast Receivers

It responds to a system-wide broadcast announcements, these include broadcasts announcing that the battery is low, screen is turned off, etc. The application can also initiate custom broadcasts. Broadcast Receivers don't create a user interface, they may create a status bar notification. A broadcast receiver can be thought of as a gateway to other components and is intended to do a minimal amount of work.

2. Resource files

Resource files are present in the resource folder. They form the skeleton of the Android application. This resource folder consists of various subfolders such as

A. drawable


The drawable folder consists of all the drawable files in the Android application.
In order to optimise the UI for different screen sizes the drawables need to be used
according to screen size. To do this we use the following folders
1. drawable-ldpi 
2. drawable-mdpi
3. drawable-hdpi
4. drawable-xhdpi
5. drawable-xxhdpi

LDPI, MDPI, HDPI, XHDPI, XXHDPI refers to screen density, which means how
many pixels can fit in once inch.
The ratio in pixels between them is
1. ldpi - 1:0.75 (low dot per inch)
2. mdpi - 1:1 (medium dot per inch)
3. hdpi - 1:1.5 (high dot per inch)
4. xhdpi - 1:2 (extra high dot per inch)
5. xxhdpi - 1:3 (extra extra high dot per inch.


B. layout

The layout folder contains layout files (.xml extension) that basically define the structure of the UI of the android application. The layouts form the skeleton of the Android application which is given life by the Java files. The layout file consists of views, these views are called the children of the layout. 
The View object is the basic building block of the UI which is created from the View Class and it occupies a rectangular area the screen and is responsible for drawing and event handling. The base class for widgets is View, and these widgets are used to create interactive UI components like buttons, text fields, etc.
There are five standard layouts :
    1. Linear Layout :
Arranges it's children in a single column or row. The orientation of the row can be set using the orientation attribute.

    2. Relative Layout :
Displays the child views in relative positions. The position of each child can be specified as relative to a sibling view.

    3. Frame Layout :
Used to block out an area on the screen to display a single item. Child views are drawn in stack with the most recent views drawn on top. This can be used to draw multiple views overlapping each other.

    4. Absolute Layout :
It allows you to specify exact locations of it's children using x, y co-ordinates.


    5. Table Layout :
Allows you to arrange groups of views in rows and columns.


C. menu

It consists of Menu Resource files. A Menu Resource file is a .xml file which defines an application menu such as an Options menu, Context menu, submenu that can be inflated
using MenuInflator.


D. mipmap

The Mipmap folder is used to place application/ launch icons. Similar to the drawables folder they have different subfolders mipmap-ldpi, mipmap-mdpi, mipmap-hdpi, etc. for different screen resolutions. 


E. values


The values folder contains various constant values that you keep using through the course of the application. For example it may contain constant strings.xml file which stores constant strings that are used throughout the program. It provides a single point of access to values that may be used from different parts of the application avoiding the creation of multiple variables. To retrieve string from strings.xml we can use "R.string.string_name". Similarly we can have a dimens.xml file which allows us to associate names with various dimension values.
The values folder may have different subfolders for different screen resolutions.


2 comments: