Module - 3
App components
• There are four types of app components:
[Link]
[Link]
[Link] receivers
[Link] providers.
Activities
Activities
• An activity is the single screen in android. It is
like window
• By the help of activity, you can place all your UI
components or widgets in a single screen.
• An activity is the entry point for interacting
with the user.
• For example, an email app might have one
activity that shows a list of new emails, another
activity to compose an email,
• For example, a email app might start the new
activity for sharing mails and documents
Services
• They handle background processing associated
with an application.
• Service is a background process that can run
for a long time ().
• A service is a general-purpose entry point for
keeping an app running in the background for
all kinds of reasons
• A service does not provide a user interface
Services
• For example, a service might play music in the
background while the user is in a different app, or it
might fetch data over the network without blocking user
interaction with an activity.
public class MyService extends Service implements
runnable
{
Public void run() {}
}
1. Foreground services
• A) foreground service performs operations that is
noticeable to the user. For example, a foreground
service would be playing audio. A foreground
service continues to run even when the user is not
interacting with your app.
• b) Background A background service performs an
operation that is not directly noticeable by the
user. For example, a service that compacts its
storage or updates a database.
• c) Bound service is bound when an application
component binds to it through the bindService()
function.
•
• Many components can bind to one service at a
time, but once they all unbind, the service will
destroy.
• Bound service offers a client-server interface
that allows an application component to
interact with the service, send requests, receive
requests, and even do so with Inter-process
communication (IPC).
Broadcast receivers
3. Broadcast receivers
• Broadcast Receiver is a component that
responds to broadcast messages from another
application or the same system.
• Broadcast receivers do not show user
interface, they might create status bar
notification to user for events.
• An application wants to receive and respond to a global
event, such as a ringing phone or an incoming text
message, it must register as a BroadcastReceiver.
• A BroadcastReceiver is implemented as a subclass of
BroadcastReceiver. Each broadcast is delivered as an
Intent object.
• A BroadcastReceiver implements the abstract method
onReceive to process incoming Intents
• [Link] - - - -
[Link]
public class MyReceiver extends BroadcastReceiver
{
public void onReceive(context,intent)
{ //code }
}
• the Broadcast-Receiver is listed in the
[Link] file, along with an
appropriate intent-filter tag, as shown in the
following listing
4. Content Provider
• What is Content provider? ‒
• They handle the access to the central
repository and supply data from one
application to another on request.
• If an application component (Activity, Service,
or BroadcastReceiver) needs to access data
from another application, they access the
other application’s ContentProvider.
• A content provider is implemented as a
subclass of ContentProvider.
• public class MyContentProvider extends
ContentProvider
{
public void onCreate()
{ code }
}
• A content provider behaves very much like a
database where you can query it, edit its
content, as well as add or delete content using
insert(), update(), delete(), and query()
methods. In most cases this data is stored in
an SQlite data
Additional components of android
applications
Layouts
Intent of Android Development
• An Intent is basically an intention to do an action.
• It facilitates communication between different component
• It's a way to communicate between Android components to
request an action from a component, by different components
• Searching for a location on the browser and
witnessing a direct jump into Google Maps or
receiving payment links in Messages Application
(SMS) and on clicking jumping to PayPal or GPay
(Google Pay).
• They can be utilized from moving from one
application to another as well
• This process of taking users from one application
to another is achieved by passing the Intent to the
system. Intents, in general, are used for navigating
among various activities within the same
Intents are used to
• – Start an activity
• – Start a service( open a mail, web browser,
calling etc)
• – Pass data in same application or different
application
• – Used to broadcasting a message
Some applications of Intents:
• Dialing Numbers, Sending SMSs, Opening
Settings, redirecting to YouTube, Maps,
•
• – Getting a Result from an Activity
• – Allowing Other Apps to Start Your Activity
• – Notification broadcast
TYPES OF INTENTS
• TYPES OF INTENTS
• [Link] Intents
• [Link] Intents
• Explicit Intents ( communicate between two
activities inside the same application , jump
from one activity to another activity)
Explicit Intent
• When you explicitly define which Android
component should be opened on some user
action, then you use explicit intent
• you can start a new activity in response to a
user action or start a service to download a file
in the background
Implicit Intent ( communicate between two
activities but from two different applications)
Explicit intent
Intent i = new
Intent([Link],
[Link]);
startActivity(i);
Implicit Intent
Intent intent=new Intent(Intent.ACTION_VIEW);
[Link]([Link]("http://
[Link]"));
startActivity(intent);
Intent Methods
• To start an Activity:
syntax: startActivity();
• To start a Service
• syntax: startService();
• .To deliver a Broadcast
• Syntax : sendBroadcast();
Building Blocks for Android Application
Design
• The different types of layouts used to organize
and arrange the controls of an application.
• Learn to use LinearLayout, RelativeLayout,
AbsoluteLayout, FrameLayout, and
TableLayout. And Learn to adapt to the screen
orientation. .
• The chapter shows you how to apply different
attributes in the layouts such as the
Orientation attribute, Height and Width
attribute, Padding attribute, Weight attribute,
and Gravity attribute
Laying Out Controls in Containers.
• What are Containers and layouts?
• A container is a view used to contain other
views.
• Android offers a collection of view classes that
act as containers for views.
• These container classes are called layouts, and
as the name suggests, they decide the
organization, size, and position of their children
views.
• Introduction to layouts?
Layouts are basically containers for other items
known as Views , which are displayed on the
screen.
Layouts help manage and arrange views as well.
Layouts are defined in the form of XML files that
cannot be changed by our code during runtime..
Android Layout Managers
Linear layout
Grid Layout
Constraint layout
Frame layout
Tab Layout
<Button
android:id="@+id/Apple"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:gravity="left"
android:text="Apple" />
<Button
android:id="@+id/Mango"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="Mango"></Button>
<Button
android:id="@+id/Banana"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:gravity="right"
android:text="Banana"></Button>
</LinearLayout>
<Button
android:id="@+id/Apple"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:gravity="left"
android:text="Apple" />
<Button
android:id="@+id/Mango"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="Mango"></Button>
<Button
android:id="@+id/Banana"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:gravity="right"
android:text="Banana"></Button>
</LinearLayout>
<Button
android:id="@+id/Apple"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:text="Apple" />
<Button
android:id="@+id/Mango"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Mango"></Button>
<Button
android:id="@+id/Banana"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Banana"></Button>
<Button
android:id="@+id/Apple"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Apple" />
<Button
android:id="@+id/Mango"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Mango"></Button>
<Button
android:id="@+id/Banana"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Banana"></Button>
</LinearLayout>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1.0"
android:text="Apple" />
<Button
android:id="@+id/Mango"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1.0"
android:text="Mango"></Button>
<Button
android:id="@+id/Banana"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
Relative layout
• The location of a child element is specified in
terms of the desired distance from the existing
children
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="[Link]
com/apk/res/android"
android:orientation="vertical“
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/Apple"
android:text="Apple"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dip"
android:layout_marginLeft="20dip" />
<Button
android:id="@+id/Mango"
android:text="Mango"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="28dip"
android:layout_toRightOf="@id/Apple"
android:layout_marginLeft="15dip"
android:layout_marginRight="10dip"
android:layout_alignParentTop="true" />
<Button
android:id="@+id/Banana"
android:text="Banana"
android:layout_width="200dip"
android:layout_height="50dip"
android:layout_marginTop="15dip"
android:layout_below="@id/Apple"
android:layout_alignParentLeft="true" />
</RelativeLayout>
ABSOLUTE LAYOUT
• Each child in an Absolute Layout is given a
specific location within the bounds of the
container
• The controls in Absolute Layout are laid out by
specifying their exact X and Y positions
<AbsoluteLayout
xmlns:android="[Link]
[Link]/apk/res/android"
xmlns:tools=
[Link]
android:orientation="vertical"
android:layout_width="match_paren
t"
android:layout_height="match_pare
nt“>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Product Form"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Product Code:"
android:layout_x="15dip"
android:layout_y="40dip" />
<EditText
android:id="@+id/product_code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="200dip"
android:layout_x="110dip"
android:layout_y="30dip" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Product Name:"
android:layout_x="5dip“
android:layout_y="30dip" />
<EditText
android:id="@+id/product_name"
android:layout_width="200dip"
android:layout_height="wrap_content"
android:minWidth="300dip"
android:layout_x="110dip"
android:layout_y="80dip"
android:scrollHorizontally="true" />
•< TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Product Price:"
android:layout_x="5dip"
android:layout_y="140dip" />
•
<EditText
android:id="@+id/product_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="200dip"
android:layout_x="110dip"
android:layout_y="130dip" />
•
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/click_btn"
android:text="Add New Product"
android:layout_x="80dip"
android:layout_y="190dip" />
</AbsoluteLayout>
FRAME LAYOUT