What Is Android
What Is Android
Android is an open source and Linux-based Operating System for mobile devices such as
smartphones and tablet computers. Android was developed by the Open Handset Alliance,
led by Google, and other companies.
Android offers a unified approach to application development for mobile devices which
means developers need to develop only for Android, and their applications should be able to
run on different devices powered by Android.
The first beta version of the Android Software Development Kit (SDK) was released by
Google in 2007, whereas the first commercial version, Android 1.0, was released in
September 2008.
On June 27, 2012, at the Google I/O conference, Google announced the next Android
version, 4.1 Jelly Bean. Jelly Bean is an incremental update, with the primary aim of
improving the user interface, both in terms of functionality and performance.
The source code for Android is available under free and open source software licenses.
Google publishes most of the code under the Apache License version 2.0 and the rest, Linux
kernel changes, under the GNU General Public License version 2.
Feature Description
Multi-tasking User can jump from one task to another and same time
various application can run simultaneously.
Resizable widgets Widgets are resizable, so users can expand them to
show more content or shrink them to save space
Wi-Fi Direct A technology that let apps discover and pair directly,
over a high-bandwidth peer-to-peer connection.
History of Android
The code names of android ranges from A to N currently, such as Aestro,
Blender, Cupcake, Donut, Eclair, Froyo, Gingerbread, Honeycomb, Ice Cream
Sandwitch, Jelly Bean, KitKat, Lollipop and Marshmallow. Let's understand
the android history in a sequence.
7.1 Nougat
8.1 Oreo
You will be glad to know that you can start your Android application
development on either of the following operating systems −
Second point is that all the required tools to develop Android applications are
freely available and can be downloaded from the Web. Following is the list of
software's you will need before you start your Android application
programming.
Android Studio
Here last two components are optional and if you are working on Windows
machine then these components make your life easy while doing Java based
application development. So let us have a look how to proceed to set required
environment.
If you are running Windows and installed the JDK in C:\jdk1.8.0_102, you
would have to put the following line in your C:\autoexec.bat file.
set PATH=C:\jdk1.8.0_102\bin;%PATH%
set JAVA_HOME=C:\jdk1.8.0_102
Alternatively, if you use Android studio, then it will know automatically where
you have installed your Java.
Android IDEs
There are so many sophisticated Technologies are available to develop
android applications, the familiar technologies, which are predominantly
using tools as follows
Android Studio
Eclipse IDE(Deprecated)
Android Applications
Android applications are usually developed in the Java language using the Android
Software Development Kit.
Once developed, Android applications can be packaged easily and sold out either
through a store such as Google Play or the Amazon Appstore.
Android powers hundreds of millions of mobile devices in more than 190 countries
around the world. It's the largest installed base of any mobile platform and is growing
fast. Every day more than 1 million new Android devices are activated worldwide.
This tutorial has been written with an aim to teach you how to develop and package
Android application. We will start from environment setup for Android application
programming and then drill down to look into various aspects of Android
applications.
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.
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.
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.
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.
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:
Components Description
1 This contains the .java source files for your project. By default, it
includes anMainActivity.java source file having an activity class that
runs when your app is launched using the app icon.
2 gen
This contains the .R file, a compiler-generated file that references all the
resources found in your project. You should not modify this file.
3 bin
This folder contains the Android package files .apk built by the ADT
during the build process and everything else needed to run an Android
application.
4 res/drawable-hdpi
This is a directory for drawable objects that are designed for high-
density screens.
5 res/layout
This is a directory for files that define your app's user interface.
6 res/values
This is a directory for other various XML files that contain a collection
of resources, such as strings and colors definitions.
7 AndroidManifest.xml
This is the manifest file which describes the fundamental characteristics
of the app and defines each of its components.
1 Linear Layout
2 Relative Layout
RelativeLayout is a view group that displays child views in relative
positions.
3 Table Layout
TableLayout is a view that groups views into rows and columns.
4 Absolute Layout
AbsoluteLayout enables you to specify the exact location of its children.
5 Frame Layout
The FrameLayout is a placeholder on screen that you can use to display a
single view.
6 List View
ListView is a view group that displays a list of scrollable items.
7 Grid View
GridView is a ViewGroup that displays items in a two-dimensional,
scrollable grid.
Layout Attributes
Each layout has a set of attributes which define the visual properties of that
layout. There are few common attributes among all the layouts and their are
other attributes which are specific to that layout. Following are common
attributes and will be applied to all the layouts:
1 android:id
2 android:layout_width
3 android:layout_height
4 android:layout_marginTop
5 android:layout_marginBottom
7 android:layout_marginRight
8 android:layout_gravity
9 android:layout_weight
This specifies how much of the extra space in the layout should be allocated
to the View.
10 android:layout_x
11 android:layout_y
12 android:layout_width
13 android:layout_width
14 android:paddingLeft
15 android:paddingRight
This is the right padding filled for the layout.
16 android:paddingTop
17 android:paddingBottom
Here width and height are the dimension of the layout/view which can be
specified in terms of dp (Density-independent Pixels), sp ( Scale-independent
Pixels), pt ( Points which is 1/72 of an inch), px( Pixels), mm ( Millimeters)
and finally in (inches).
You can specify width and height with exact measurements but more often,
you will use one of these constants to set the width or height −
Gravity attribute plays important role in positioning the view object and it can
take one or more (separated by '|') of the following constant values.
clip_vertical 0x80 Additional option that can be set to have the top
and/or bottom edges of the child clipped to its
container's bounds. The clip will be based on the
vertical gravity: a top gravity will clip the
bottom edge, a bottom gravity will clip the top
edge, and neither will clip both edges.
clip_horizontal 0x08 Additional option that can be set to have the left
and/or right edges of the child clipped to its
container's bounds. The clip will be based on the
horizontal gravity: a left gravity will clip the
right edge, a right gravity will clip the left edge,
and neither will clip both edges.
start 0x00800003 Push object to the beginning of its container,
not changing its size.
Android UI Controls
There are number of UI controls provided by Android that allow you to build
the graphical user interface for your app.
1 TextView
2 EditText
3 AutoCompleteTextView
4 Button
5 ImageButton
An ImageButton is an AbsoluteLayout which enables you to specify the
exact location of its children. This shows a button with an image (instead
of text) that can be pressed or clicked by the user.
6 CheckBox
An on/off switch that can be toggled by the user. You should use check
box when presenting users with a group of selectable options that are not
mutually exclusive.
7 ToggleButton
8 RadioButton
9 RadioGroup
10 ProgressBar
The ProgressBar view provides visual feedback about some ongoing tasks,
such as when you are performing a task in the background.
11 Spinner
A drop-down list that allows users to select one value from a set.
12 TimePicker
The TimePicker view enables users to select a time of the day, in either
24-hour mode or AM/PM mode.
13 DatePicker
Events are a useful way to collect data about a user's interaction with
interactive components of Applications. Like button presses or screen touch etc.
The Android framework maintains an event queue as first-in, first-out (FIFO) basis. You
can capture these events in your program and take appropriate action as per
requirements.
Event Listeners − An event listener is an interface in the View class that contains
a single callback method. These methods will be called by the Android framework
when the View to which the listener has been registered is triggered by user
interaction with the item in the UI.
OnClickListener()
OnLongClickListener()
onLongClick()
This is called when the user either clicks or touches or
focuses upon any widget like button, text, image etc. for
one or more seconds. You will use onLongClick() event
handler to handle such event.
OnFocusChangeListener()
onFocusChange() This is called when the widget looses its focus ie. user
goes away from the view item. You will use
onFocusChange() event handler to handle such event.
OnFocusChangeListener()
onKey() This is called when the user is focused on the item and
presses or releases a hardware key on the device. You
will use onKey() event handler to handle such event.
OnTouchListener()
onTouch() This is called when the user presses the key, releases
the key, or any movement gesture on the screen. You
will use onTouch() event handler to handle such event.
OnMenuItemClickListener()
onMenuItemClick() This is called when the user selects a menu item. You
will use onMenuItemClick() event handler to handle such
event.
onCreateContextMenuItemListener()
onCreateContextMenu()
This is called when the context menu is being built(as
the result of a sustained "long click)
There are many more event listeners available as a part of View class like
OnHoverListener, OnDragListener etc which may be needed for your
application. So I recommend to refer official documentation for Android
application development in case you are going to develop a sophisticated
apps.
Android - SQLite Database
SQLite supports all the relational database features. In order to access this
database, you don't need to establish any kind of connections for it like
JDBC,ODBC e.t.c
Database - Package
The main package is android.database.sqlite that contains the classes to
manage your own databases
Database - Creation
In order to create a database you just need to call this method
openOrCreateDatabase with your database name and mode as a parameter.
It returns an instance of SQLite database which you have to receive in your
own object.Its syntax is given below
Apart from this , there are other functions available in the database package
, that does this job. They are listed below
This method only opens the existing database with the appropriate flag
mode. The common flags mode could be OPEN_READWRITE
OPEN_READONLY
2
openDatabase(String path, SQLiteDatabase.CursorFactory factory,
int flags)
It is similar to the above method as it also opens the existing database but
it does not define any handler to handle the errors of databases
3
openOrCreateDatabase(String path,
SQLiteDatabase.CursorFactory factory)
It not only opens but create the database if it not exists. This method is
equivalent to openDatabase method.
4
openOrCreateDatabase(File file, SQLiteDatabase.CursorFactory
factory)
This method is similar to above method but it takes the File object as a
path rather then a string. It is equivalent to file.getPath()
Database - Insertion
we can create table or insert data into table using execSQL method defined
in SQLiteDatabase class. Its syntax is given below
This will insert some values into our table in our database. Another method
that also does the same job but take some additional parameter is given
below
This method not only insert data , but also used to update or modify
already existing data in database using bind arguments
Database - Fetching
We can retrieve anything from database using an object of the Cursor class.
We will call a method of this class called rawQuery and it will return a resultset
with the cursor pointing to the table. We can move the cursor forward and
retrieve the data.
1 getColumnCount()
2 getColumnIndex(String columnName)
This method returns the index number of a column by specifying the name
of the column
3 getColumnName(int columnIndex)
This method returns the name of the column by specifying the index of the
column
4 getColumnNames()
This method returns the array of all the column names of the table.
5 getCount()
6 getPosition()
This method returns the current position of the cursor in the table
7 isClosed()
This method returns true if the cursor is closed and return false otherwise
public DBHelper(){
super(context,DATABASE_NAME,null,1);