Lecture Notes 1 2 3
Lecture Notes 1 2 3
There are several advantages of the Android based system, some of which include:
• The Android SDK (Software Development Kit) is a collection of software tools that
developers use to create Android applications. The SDK includes several components that
provide different functionalities, and they are:
• Android Studio: Android Studio is the primary tool for Android application development. It
is an integrated development environment (IDE) that provides developers with a graphical
user interface for building, debugging, and testing Android apps.
• Android SDK Tools: The Android SDK Tools are a set of command-line tools used for
building and debugging Android applications. These tools include Android Debug Bridge
(ADB), Android Asset Packaging Tool (AAPT), and Android Emulator.
• Android SDK Platform-Tools: The Android SDK Platform-Tools are a set of command-line
tools used for interacting with an Android device or emulator. These tools include ADB,
Fastboot, and Systrace.
• Android SDK Build-Tools: The Android SDK Build-Tools are a set of tools used for building
Android applications. These tools include the Android Asset Packaging Tool (AAPT), the
Android Debugging Bridge (ADB), and the Dalvik Debug Monitor Server (DDMS).
• Android SDK Platforms: The Android SDK Platforms are collections of tools and libraries
that developers use to create Android apps. They include the Android API, which provides
access to the Android framework, and various other components like the Android Support
Library, Google Play Services, and Android System Images.
• Android Virtual Device (AVD) Manager: The AVD Manager is a tool that allows developers
to create and manage Android Virtual Devices (AVDs). An AVD is an emulator instance that
enables developers to test their applications on different Android versions, device
configurations, and screen sizes.
The Android architecture is a layered architecture that is designed to provide a robust and flexible
operating system for mobile devices. It is based on the Linux kernel and includes several layers of
software components. The following is a detailed explanation of the Android architecture:
1. Linux Kernel Layer: This layer is the foundation of the Android architecture. It provides
essential features such as memory management, process management, and security. The
Linux kernel layer also provides support for hardware drivers and network protocols.
2. Hardware Abstraction Layer (HAL): This layer provides a standardized interface between
the hardware and the upper layers of the Android architecture. It abstracts the hardware-
specific details and provides a consistent API for device drivers.
3. Native Libraries: These libraries are written in C/C++ and provide access to the hardware
features of the device. Examples of native libraries include the Media Framework, OpenGL
ES, and SQLite.
4. Android Runtime: The Android Runtime (ART) is a virtual machine that executes the Dalvik
bytecode used by Android applications. It compiles the bytecode into machine code when
an application is installed, which results in improved performance compared to the
previous Dalvik virtual machine.
5. Java API Framework: The Java API framework provides a rich set of APIs for developers to
build Android applications. The APIs include classes for user interface (UI), data storage,
networking, and multimedia.
6. System Applications: These are the pre-installed applications that come with Android
devices. Examples include the phone app, messaging app, and browser.
8. Applications: Finally, the top layer of the Android architecture is the applications layer.
This layer includes the applications that users download from the Google Play Store or
other sources. Applications can be developed in Java or Kotlin and can use the Android
API to access the device’s hardware features and system services.
To navigate transitions between stages of the activity lifecycle, the Activity class provides a core set of six
callbacks:
Method Description
onResume called when activity will start interacting with the user.
An Android Virtual Device (AVD) is a software-based emulation of an Android device that runs on a
computer. It is a tool provided by the Android SDK that allows developers to test their applications on
different Android versions, device configurations, and screen sizes without needing access to a physical
device.
An AVD is created and managed using the Android Virtual Device (AVD) Manager, which is included in the
Android SDK. Developers can create multiple AVDs to test their applications on different versions of
Android, with different screen sizes and resolutions, and with different hardware configurations.
Each AVD includes an emulator, which simulates the device hardware and provides a virtual environment for
running Android applications. The emulator uses the Android SDK tools to create a virtual device image that
includes the Android operating system, system libraries, and pre-installed applications.
Once an AVD is created, developers can launch it using the emulator, which provides a graphical user
interface for interacting with the virtual device. Developers can use the emulator to install, run, and debug
their applications, and to simulate different device configurations and user scenarios.
6. What is the use of Manifest file in android application?
Every application must have an AndroidManifest.xml file in its root directory. The manifest file provides
essential information about your app to the Android system, which the system must have before it can run
any of the app's code.
• It names the Java package for the application. The package name serves as a unique identifier for the
application.
• It describes the components of the application, which include the activities, services, broadcast
receivers, and content providers that compose the application.
• It determines the processes that host the application components.
• It declares the permissions that the application must have to access protected parts of the API and
interact with other applications. It also declares the permissions that others are required to have to
interact with the application's components.
• It lists the Instrumentation classes that provide profiling and other information as the application
runs. These declarations are present in the manifest only while the application is being developed
and are removed before the application is published.
• It declares the minimum level of the Android API that the application requires.
• It lists the libraries that the application must be linked against.
A Fragment is a piece of an activity which enable more modular activity design. It will not be wrong if we say,
a fragment is a kind of sub-activity.
• A fragment has its own layout and its own behaviour with its own life cycle callbacks.
• You can add or remove fragments in an activity while the activity is running.
• You can combine multiple fragments in a single activity to build a multi-pane UI.
• A fragment can be used in multiple activities.
• Fragment life cycle is closely related to the life cycle of its host activity which means when the
activity is paused, all the fragments available in the activity will also be stopped.
• A fragment can implement a behaviour that has no user interface component.
• Fragments were added to the Android API in Honeycomb version of Android which API version 11.
You create fragments by extending Fragment class and You can insert a fragment into your activity layout by
declaring the fragment in the activity's layout file, as a <fragment> element.
The application can embed two fragments in Activity A, when running on a tablet-sized device. However, on
a handset-sized screen, there's not enough room for both fragments, so Activity A includes only the fragment
for the list of articles, and when the user selects an article, it starts Activity B, which includes the second
fragment to read the article.
Android fragments have their own life cycle very similar to an android activity. This section briefs different
stages of its life cycle.
Fragment lifecycle
Here is the list of methods which you can to override in your fragment class −
• onAttach():The fragment instance is associated with an activity instance.The fragment and the
activity is not fully initialized. Typically you get in this method a reference to the activity which
uses the fragment for further initialization work.
• onCreate(): The system calls this method when creating the fragment. You should initialize essential
components of the fragment that you want to retain when the fragment is paused or stopped, then
resumed.
• onCreateView(): The system calls this callback when it's time for the fragment to draw its user
interface for the first time. To draw a UI for your fragment, you must return a View component from
this method that is the root of your fragment's layout. You can return null if the fragment does not
provide a UI.
• onActivityCreated(): The onActivityCreated() is called after the onCreateView() method when the
host activity is created. Activity and fragment instance have been created as well as the view
hierarchy of the activity. At this point, view can be accessed with the findViewById() method.
example. In this method you can instantiate objects which require a Context object
• onStart(): The onStart() method is called once the fragment gets visible.
• onResume(): Fragment becomes active.
• onPause(): The system calls this method as the first indication that the user is leaving the fragment.
This is usually where you should commit any changes that should be persisted beyond the current
user session.
• onStop(): Fragment going to be stopped by calling onStop()
• onDestroyView(): Fragment view will destroy after call this method
• onDestroy(): onDestroy() called to do final clean up of the fragment's state but Not guaranteed to be
called by the Android platform.
Types of Fragments
• Single frame fragments − Single frame fragments are using for hand hold devices like mobiles,
here we can show only one fragment as a view.
• List fragments − fragments having special list view is called as list fragment
• Fragments transaction − Using with fragment transaction. we can move one fragment to
another fragment.
Types of Intents
Explicit intent going to be connected internal world of application, suppose if you wants to connect
one activity to another activity, we can do this quote by explicit intent, below image is connecting first
activity to second activity by clicking button.
These intents designate the target component by its name and they are typically used for application-
internal messages - such as an activity starting a subordinate service or launching a sister activity. For
example –
// Starts TargetActivity
startActivity(i);
Implicit Intents
These intents do not name a target and the field for the component name is left blank. Implicit intents are
often used to activate components in other applications. For example −
read1.setAction(android.content.Intent.ACTION_VIEW);
read1.setData(ContactsContract.Contacts.CONTENT_URI);
startActivity(read1);
Intent Objects
An Intent object is a bundle of information which is used by the component that receives the intent as well
as information used by the Android system.
An Intent object can contain the following components based on what it is communicating or going to
perform −
Action
This is mandatory part of the Intent object and is a string naming the action to be performed — or, in the
case of broadcast intents, the action that took place and is being reported. The action largely determines
how the rest of the intent object is structured . The Intent class defines a number of action constants
corresponding to different intents. Here is a list of Android Intent Standard Actions
The action in an Intent object can be set by the setAction() method and read by getAction().
Data
Adds a data specification to an intent filter. The specification can be just a data type (the mimeType
attribute), just a URI, or both a data type and a URI. A URI is specified by separate attributes for each of its
parts −
These attributes that specify the URL format are optional, but also mutually dependent −
• If a scheme is not specified for the intent filter, all the other URI attributes are ignored.
• If a host is not specified for the filter, the port attribute and all the path attributes are ignored.
The setData() method specifies data only as a URI, setType() specifies it only as a MIME type, and
setDataAndType() specifies it as both a URI and a MIME type. The URI is read by getData() and the type by
getType().
A layout defines the visual structure for a user interface, such as the UI for an activity or app widget. You can
declare a layout in two ways:
• Declare UI elements in XML. Android provides a straightforward XML vocabulary that corresponds to
the View classes and subclasses, such as those for widgets and layouts.
• Instantiate layout elements at runtime. Your application can create View and ViewGroup objects
(and manipulate their properties) programmatically.
Common layouts in Android are:
1) Linear Layout
2) Relative Layout
3) Web View
4) Table Layout
5) Frame Layout
Linear Layout
LinearLayout is a view group that aligns all children in a single direction, vertically or horizontally. You can
specify the layout direction with the android:orientation attribute. All children of a Linear Layout are
stacked one after the other, so a vertical list will only have one child per row, no matter how wide they are,
and a horizontal list will only be one row high (the height of the tallest child, plus
padding). A Linear Layout respects margins between children and the gravity (right, center, or left alignment)
of each child.
Relative Layout
Relative Layout is a view group that displays child views in relative positions. The position of each view can
be specified as relative to sibling elements (such as to the left-of or below another view) or in positions
relative to the parent Relative Layout area (such as aligned to the bottom, left or center).
Relative Layout lets child views specify their position relative to the parent view or to each other (specified
by ID). So you can align two elements by right border, or make one below another, centered in the screen,
centered left, and so on. By default, all child views are drawn at the top-left of the layout, so you must
define the position of each view using the various layout properties available from
RelativeLayout.LayoutParams. Some of the many layout properties available to views in a RelativeLayout
include:
1) android:layout_alignParentTop: If "true", makes the top edge of this view match the top edge of the parent.
3) android:layout_below: Positions the top edge of this view below the view specified with a resource ID.
4) android:layout_toRightOf: Positions the left edge of this view to the right of the view specified with a
resource ID.
</RelativeLayout>
Frame Layout
Frame Layout is designed to block out an area on the screen to display a single item. Generally, Frame
Layout should be used to hold a single child view, because it can be difficult to organize child views in a way
that's scalable to different screen sizes without the children overlapping each other.
You can, however, add multiple children to a Frame Layout and control their position within the Frame
Layout by assigning gravity to each child, using the android:layout_gravity attribute.
Child views are drawn in a stack, with the most recently added child on top. The size of the Frame Layout is
the size of its largest child (plus padding), visible or not (if the Frame Layout's parent permits). Sample code
showing use of FrameLayout has been shown below:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/framelayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:foregroundGravity="fill"
android:foreground="#0f0">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="AndroidSample"/>
</LinearLayout>
</FrameLayout>
Table Layout
Sample code showing use of Table Layout has been shown below:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="1">
<TableRow>
<TextView
android:text="@string/table_layout_4_open"
android:padding="3dip" />
<TextView
android:text="@string/table_layout_4_open_shortcut"
android:gravity="right"
android:padding="3dip" />
</TableRow>
<TableRow>
<TextView
android:text="@string/table_layout_4_save"
android:padding="3dip" />
<TextView
android:text="@string/table_layout_4_save_shortcut"
android:gravity="right"
android:padding="3dip" />
</TableRow>
</TableLayout>
View is a basic building block of UI (User Interface) in android. A view is a small rectangular box that
responds to user inputs. Eg: EditText, Button, CheckBox, etc. ViewGroup is an invisible container of other
views (child views) and other ViewGroup. Eg: LinearLayout is a ViewGroup that can contain other views in it.
View refer to the android.view.View class, which is the base class of all UI classes. android.view.View class is
the root of the UI class hierarchy. So from an object point of view, all UI objects are View objects. Following
are some of the common View subclasses that will be used in android applications.
• TextView
• EditText
• ImageView
• RadioButton
• Button
• ImageButton
• CheckBox
• DatePicker
• Spinner
• ProgressBar
ViewGroup
The ViewGroup class is a subclass of the View class. And also it will act as a base class for layouts and
layouts parameters. The ViewGroup will provide an invisible container to hold other Views or ViewGroups
and to define the layout properties. For example, Linear Layout is the ViewGroup that contains UI controls
like Button, TextView, etc., and other layouts also. ViewGroup Refer to the android.view.ViewGroup class,
which is the base class of some special UI classes that can contain other View objects as children. Since
ViewGroup objects are also View objects, multiple ViewGroup objects and View objects can be organized
into an object tree to build a complex UI structure. Following are the commonly used ViewGroup subclasses
used in android applications.
• FrameLayout
• WebView
• ListView
• GridView
• LinearLayout
• RelativeLayout
• TableLayout
Difference Table
View ViewGroup
View is a simple rectangle box that ViewGroup is the invisible container. It holds View and
responds to the user’s actions. ViewGroup
A View object is a component of the user A ViewGroup object is a layout, that is, a container of
interface (UI) like a button or a text box, and other ViewGroup objects (layouts) and View objects
it’s also called a widget. (widgets)
Examples are EditText, Button, CheckBox, For example, LinearLayout is the ViewGroup that
etc. contains Button(View), and other Layouts also.
View ViewGroup
Mobile apps are basically little, self-contained programs, used to enhance existing functionality, hopefully in
a simple, more user-friendly way.
They all come with powerful web browsers, meaning you can do pretty much anything you can do on a
desktop computer in a phone’s browser. Normally, when people talk about apps they are almost always
referring to programs that run on mobile devices, such as Smartphones or Tablet Computers.
Each app provides limited and isolated functionality such as game, calculator or mobile web browsing.
Although apps may have avoided multitasking because of the limited hardware resources of the early
mobile devices, their specificity is now part of their desirability because they allow consumers to hand-pick
what their device are able to do.
1) Native App
2) Web App
3) Hybrid App
Native App
Native App has been developed for use on a particular platform or device.
A native mobile app is a Smartphone application that is coded in a specific programming language, Java for
Android operating systems.
Native mobile apps provide fast performance and a high degree of reliability.
They also have access to a phone’s various devices, such as its camera and address book.
However, this type of app is expensive to develop because it is tied to one type of operating system, forcing
the company that creates the app to make duplicate versions that work on other platforms.
A Native app can only be “Native” to one type of mobile operating system.
Most video games are native mobile apps.
Web App
Web App stored on a remote server and delivered over the internet through a browser.
Web apps are not real apps; they are really websites that, in many ways, look and feel like native
applications.
Users first access them as they would access any web page: they navigate to a special URL and then have
the option of “installing” them on their home screen by creating a bookmark to that page.
In contrast, a mobile web app is software that uses technologies such as JavaScript or HTML5 to provide
interaction, navigation, or customization capabilities.
This means that they’re delivered wholly on the fly, as needed, via the internet; they are not separate
programs that get stored on the user’s mobile device.
Web apps became really popular when HTML5 came around and people realized that they can obtain
native-like–functionality in the browser.
Hybrid App
This type of application has cross-platform compatibility but can still access a phone’s hardware. It is
developed using platforms such as Sencha, PhoneGap and Mosync. Hybrid Apps are like native apps, run on
the device, and are written with web technologies (HTML5, CSS and JavaScript).
Hybrid apps run inside a native container and leverage the device’s browser engine to render the HTML and
process the JavaScript locally.
A web-to-native abstraction layer enables access to device capabilities that are not accessible in Mobile
Web applications, such as the accelerometer, camera and local storage.
Hybrid apps are also popular because they allow cross-platform development: that is, the same HTML code
components can be reused on different mobile operating systems, reducing significantly the development
costs.
In Android, GridLayout is a layout manager that allows you to create a grid of rows and columns to position
child views. With GridLayout, you can create complex layouts by placing views in a grid pattern.
Here are some of the attributes that you can use with GridLayout:
1. rowCount and columnCount: These attributes specify the number of rows and columns in the grid.
2. layout_row and layout_column: These attributes specify the row and column index of a child view
within the grid.
3. layout_rowSpan and layout_columnSpan: These attributes specify the number of rows and columns
that a child view occupies within the grid.
4. layout_gravity: This attribute specifies how a child view is positioned within its cell. You can use
values like top, bottom, left, right, center, and fill to control the alignment of the view.
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:rowCount="2"
android:columnCount="2">
<TextView
android:text="View 1"
android:layout_row="0"
android:layout_column="0"/>
<TextView
android:text="View 2"
android:layout_row="0"
android:layout_column="1"/>
<TextView
android:text="View 3"
android:layout_row="1"
android:layout_column="0"
android:layout_columnSpan="2"/>
</GridLayout>
Both context menus and system menus are important UI elements in Android that allow users to perform
actions or access additional functionality within an app. Here’s an explanation of each:
1. Context Menu: A context menu is a floating menu that appears when a user long-presses on a view
or item in an Android app. It usually contains options that are relevant to the selected view or item.
For example, in a messaging app, long-pressing on a message might bring up a context menu with
options to reply, delete, or mark as unread. Context menus can be created using
the registerForContextMenu() method in an Activity or Fragment, and can be customized using
the onCreateContextMenu() method.
2. System Menu: The system menu is a menu that appears when a user presses the device’s menu
button (if it has one) or the overflow button (three dots) in the app’s action bar. It typically contains
options that are relevant to the app as a whole, such as settings, help, or sign out. The system menu
is automatically generated by Android based on the options menu resource XML file that you define
in your app. You can create the options menu resource XML file in the res/menu folder of your app,
and then inflate it using the onCreateOptionsMenu() method in your Activity or Fragment.
The Option Menu is a menu that appears when the user presses the menu button on their Android device. It
provides a list of options that the user can choose from to perform an action, such as opening a new screen
or sharing content. Here are some key points about the Option Menu:
• The Option Menu is displayed as a popup menu, typically with an icon in the top-right corner of the
app’s screen.
• The items in the Option Menu are defined in the onCreateOptionsMenu() method of the activity or
fragment. This method is called once when the activity or fragment is created, and is where you can
inflate a menu resource file to create the menu items.
• Each item in the Option Menu is represented by a MenuItem object. You can set the title, icon, and
other properties of the menu item using methods on this object.
• When the user selects a menu item, the onOptionsItemSelected() method is called with the
selected MenuItem object as a parameter. This is where you can perform the action associated with
the selected item.
• The Option Menu can be customized by adding icons, setting text color and size, and changing the
layout using a custom layout file.
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menu_share"
android:icon="@drawable/ic_share"
android:title="Share" />
<item
android:id="@+id/menu_settings"
android:icon="@drawable/ic_settings"
android:title="Settings" />
</menu>