KEMBAR78
Mobile Application & Development Unit-2 | PDF | Mobile App | Android (Operating System)
0% found this document useful (0 votes)
87 views24 pages

Mobile Application & Development Unit-2

This document provides an overview of Android application design essentials, detailing the key components such as Activities, Services, Content Providers, Intents, and Broadcast Receivers. It explains the role of the Android Manifest file, the importance of application context, and the lifecycle of activities in Android development. Additionally, it covers essential terminologies and concepts necessary for building Android applications.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
87 views24 pages

Mobile Application & Development Unit-2

This document provides an overview of Android application design essentials, detailing the key components such as Activities, Services, Content Providers, Intents, and Broadcast Receivers. It explains the role of the Android Manifest file, the importance of application context, and the lifecycle of activities in Android development. Additionally, it covers essential terminologies and concepts necessary for building Android applications.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Mobile Application & Development Unit II- Android Application

Design Essentials

Unit-II

 Android Application Design Essentials:-


---------------------------------------------------------------------------------------------------------------
Contents:

Android Application Design Essentials: Anatomy of an Android applications, Android


terminologies, Application Context, Activities, Services, Intents, Receiving and Broadcasting
Intents, Android Manifest File and its common settings, Using Intent Filter, Permissions.

---------------------------------------------------------------------------------------------------------------

 Anatomy of an Android applications:

There are some necessary building blocks that an Android application consists of.
These loosely coupled components are bound by the application manifest file which contains
the description of each component and how they interact. The manifest file also contains the
app’s metadata, its hardware configuration, and platform requirements, external libraries,
and required permissions. There are the following main components of an android app

1. Activities
Activities are said to be the presentation layer of our applications. The UI of our
application is built around one or more extensions of the Activity class. By using Fragments
and Views, activities set the layout and display the output and also respond to the user’s
actions. An activity is implemented as a subclass of class Activity. An activity is usually a single
screen in your application. Each activity is implemented as a single class that extends the
Activity base class. Your class will display a user interface composed of Views and respond to
events. Most applications consist of multiple screens.
Syntax : public class MainActivity extends Activity {
}

GFGC Boy’s College, Kolar 1 By: Divakar M.S


Mobile Application & Development Unit II- Android Application
Design Essentials

2. Services
Services are like invisible workers of our app. These components run at the backend,
updating your data sources and Activities, triggering Notification, and also broadcast Intents.
They also perform some tasks when applications are not active. A service can be used as a
subclass of class Service.
A Service is code that is long-lived and runs without a UI. A good example of this is a
media player playing songs from a play list. In a media player application, there would probably
be one or more activities that allow the user to choose songs and start playing them.

Syntax : public class ServiceName extends Service {


}

3. Content Providers
It is used to manage and persist the application data also typically interacts with the
SQL database. They are also responsible for sharing the data beyond the application
boundaries. The Content Providers of a particular application can be configured to allow
access from other applications, and the Content Providers exposed by other applications can
also be configured. A content provider should be a sub-class of the class ContentProvider.

Syntax : public class contentProviderName extends ContentProvider {

public void onCreate() { }

}
4. Intents
It is a powerful inter-application message-passing framework. They are extensively
used throughout Android. Intents can be used to start and stop Activities and Services, to
broadcast messages system-wide or to an explicit Activity, Service or Broadcast Receiver or
to request action be performed on a particular piece of data. Android uses a special class called
Intent to move from screen to screen. Intent describe what an application wants done. The two
most important parts of the intent data structure are the action and the data to act upon. Typical
values for action are MAIN (the front door of the application), VIEW, PICK, EDIT, etc. The data
is expressed as a Uniform Resource Indicator (URI).
Intent Receiver :
You can use an IntentReceiver when you want code in your application to execute in
reaction to an external event, for example, when the phone rings, or when the data network is
available, or when it's midnight. Intent receivers do not display a UI, although they may display
Notifications to alert the user if something interesting has happened. Intent receivers are also
registered in AndroidManifest.xml, but you can also register them from code using
Context.registerReceiver().

GFGC Boy’s College, Kolar 2 By: Divakar M.S


Mobile Application & Development Unit II- Android Application
Design Essentials

5. Broadcast Receivers
They are known to be intent listeners as they enable your application to listen to the
Intents that satisfy the matching criteria specified by us. Broadcast Receivers make our
application react to any received Intent thereby making them perfect for creating event-
driven applications.

6. Notifications
Notifications are the application alerts that are used to draw the user’s attention to
some particular app event without stealing focus or interrupting the current activity of the
user. They are generally used to grab user’s attention when the application is not visible or
active, particularly from within a Service or Broadcast Receiver. Examples: E-mail popups,
Messenger popups, etc.

 Android terminologies :
1. XML
In Android, XML is used for designing the application’s UI like creating layouts, views,
buttons, text fields etc. and also used in parsing data feeds from the internet.

2. View
A view is an UI which occupies rectangular area on the screen to draw and handle user
events.

3. Layout
Layout is the parent of view. It arranges all the views in a proper manner on the screen.

4. Activity
An activity can be referred as your device’s screen which you see. User can place UI
elements in any order in the created window of user’s choice.

5. Emulator
An emulator is an Android virtual device through which you can select the target
Android version or platform to run and test your developed application.

6. Manifest file
Manifest file acts as a metadata for every application. This file contains all the essential
information about the application like app icon, app name, launcher activity, and
required permissions etc.

7. Service
Service is an application component that can be used for long-running background
processes. It is not bounded with any activity as there is no UI. Any other application
component can start a service and this service will continue to run even when the user
switches from one application to another.
GFGC Boy’s College, Kolar 3 By: Divakar M.S
Mobile Application & Development Unit II- Android Application
Design Essentials

8. Intent
Intent is a messaging object which can be used to communicate between two or more
components like activities, services, broadcast receiver etc. Intent can also be used to
start an activity or service or to deliver a broadcast message.

9. Broadcast Receiver

Broadcast Receiver is another building block of Android application development


whichallows you to register for system and application events. It works in such a way
that, when the event triggers for the first time all the registered receivers through this
broadcast receiver will get notified for all the events by Android Runtime. To know
more about the broadcast receivers.

10. Content Providers


Content Providers are used to share data between two applications. This can be
implemented in two ways:
1. When you want to implement the existing content provider in another application.
2. When you want to create a new content provider that can share its data with
otherapplications

 Application Context :

CONTEXT: Android Applications are popular for a long time and it is evolving to a greater
level as users’ expectations are that they need to view the data that they want in an easier
smoother view. Hence, the android developers must know the important terminologies
before developing the app. In Android Programming we generally come across the
word Context.

Looking at this definition we come across two things:


 The Context tells us about the surrounding information.
 It is very important to understand the environment which we want to understand.

Similarly when we talk about Android Programming Context can be understood as


something which gives us the Context of the current state of our application. We can
break the Context and its use into three major points:

 It allows us to access resources.


 It allows us to interact with other Android components by sending messages.
 It gives you information about your app environment.

GFGC Boy’s College, Kolar 4 By: Divakar M.S


Mobile Application & Development Unit II- Android Application
Design Essentials

Types of Context in Android


There are mainly two types of Context that are available in Android.
1. Application Context and
2. Activity Context

The Overall view of the App hierarchy looks like the following:

It can be seen in the above image that in “Sample Application”, the nearest Context is
Application Context. In “Activity1” and “Activity2”, both Activity Context (Here it is
Activity1 Context for Activity1 and Activity2 Context for Activity2) and Application
Context.The nearest Context to both is their Activity Context only.

1. Application Context:-

Mainly it is an instance that is a singleton and can be accessed


via getApplicationContext(). Some use cases of Application Context are:

 If it is necessary to create a singleton object


 During the necessity of a library in an activity

getApplicationContext():
It is used to return the Context which is linked to the Application which holds all
activities running inside it. When we call a method or a constructor, we often have to pass a
Context and often we use “this” to pass the activity Context or “getApplicationContext” to
pass the application Context. This method is generally used for the application level and can
be used to refer to all the activities.
For example, if we want to access a variable throughout the android app, one has to
use it via getApplicationContext().

GFGC Boy’s College, Kolar 5 By: Divakar M.S


Mobile Application & Development Unit II- Android Application
Design Essentials

Syntax:
// Activity 1
public class <your activity1> extends Activity {
........
........
private <yourapplicationname> globarVar;
........
@Override
public void onCreate(Bundle savedInstanceState) {
.......
final GlobalExampleClass globalExampleVariable = (GlobalExampleClass) getApplicationContext();

// In this activity set name and email and can reuse in other activities
globalExampleVariable.setName("getApplicationContext example");
globalExampleVariable.setEmail("xxxxxx@gmail.com");

.......
}

// Activity 2
public class <your activity2> extends Activity {
........
........
private <yourapplicationname> globarVar;
.......
@Override
public void onCreate(Bundle savedInstanceState) {
.......
final GlobalExampleClass globalExampleVariable = (GlobalExampleClass) getApplicationContext();

// As in activity1, name and email is set, we can retrieve it here


final String globalName = globalExampleVariable.getName();
final String globalEmail = globalExampleVariable.getEmail();

.......
}

GFGC Boy’s College, Kolar 6 By: Divakar M.S


Mobile Application & Development Unit II- Android Application
Design Essentials

So, whenever the variable scope is required throughout the application, we can get it
by means of getApplicationContext(). Following is a list of functionalities of Application
Context.
List of functionalities of Application Context:
 Load Resource Values
 Start a Service
 Bind to a Service
 Send a Broadcast
 Register BroadcastReceiver

2. Activity Context:
It is the activity Context meaning each and every screen got an activity. For example,
EnquiryActivity refers to EnquiryActivity only and AddActivity refers to AddActivity only. It
is tied to the life cycle of activity. It is used for the current Context. The method of invoking
the Activity Context is getContext().
Some use cases of Activity Context are:
 The user is creating an object whose lifecycle is attached to an activity.
 Whenever inside an activity for UI related kind of operations like toast, dialogue, etc.,
getContext():
It returns the Context which is linked to the Activity from which it is called. This is useful
when we want to call the Context from only the current running activity.

 Activities in Android Application:


WHAT IS AN ACTIVITY?
An activity is one screen of an app. In that way the activity is very similar to a window in the
Windows operating system.
The most specific block of the user interface is the activity. An Android app contains activities,
meaning one or more screens.
Examples: Login screen, sign up screen, and home screen.

An activity class is per definition a class in Android. Every application which has UI
must inherit it to create a window.

HOW DOES ACTIVITY WORK?

An activity in Android is a specific combination of XML files and JAVA files. It is basically a
container that contains the design as well as coding stuff.XML files provide the design of the
screen and JAVA files deal with all coding stuff like handles, what is happening, design files,
etc. JAVA files and XML files make your activity complete.

GFGC Boy’s College, Kolar 7 By: Divakar M.S


Mobile Application & Development Unit II- Android Application
Design Essentials

 Let’s start by declaring activity. First, open your manifest file then add an element as a
child of the element.

Syntax:

<manifest ... >


<application ... >
<activity android:name=".ExampleActivity" />
...
</application ... >
...
</manifest >
seDebugValue(value)

Example XML file:

Java file:

GFGC Boy’s College, Kolar 8 By: Divakar M.S


Mobile Application & Development Unit II- Android Application
Design Essentials

Activity Lifecycle: Activity is one of the building blocks of Android OS. In simple words Activity
is a screen that user interact with. Every Activity in android has lifecycle like created, started,
resumed, paused, stopped or destroyed. These different states are known as Activity Lifecycle.
In other words we can say Activity is a class pre-written in Java Programming.

Below is Activity Lifecycle Table:


Short description of Activity Lifecycle example:
onCreate() – Called when the activity is first created
onStart() – Called just after it’s creation or by restart method after onStop(). Here
Activity start becoming visible to user
onResume() – Called when Activity is visible to user and user can interact with it
onPause() – Called when Activity content is not visible because user resume
previous activity
onStop() – Called when activity is not visible to user because some other activity
takes place of it
onRestart() – Called when user comes on screen or resume the activity
which was stopped
onDestroy – Called when Activity is not in background

Activity have different states or it’s known as Activity life cycle. All life cycle
methods aren’t required to override but it’s quite important to understand them.
Lifecycles methods can be overridden according to requirements.

GFGC Boy’s College, Kolar 9 By: Divakar M.S


Mobile Application & Development Unit II- Android Application
Design Essentials

LIST OF ACTIVITY LIFECYCLE METHODS OR STATES:


Activity Created: onCreate(Bundle savedInstanceState):
onCreate() method is called when activity gets memory in the OS. To use create state we
need to override onCreate(Bundle savedInstanceState) method. Now there will be
question in mind what is Bundle here, so Bundle is a data repository object that can store
any kind of primitive data and this object will be null until some data isn’t saved in that.

 When an Activity first call or launched then onCreate(Bundle savedInstanceState)


method is responsible to create the activity.
 When ever orientation(i.e. from horizontal to vertical or vertical to horizontal) of
activity gets changed or when an Activity gets forcefully terminated by any
Operating System then savedInstanceState i.e. object of Bundle Class will save the
state of an Activity.
 It is best place to put initialization code.
Activity Started: onStart():
onStart() method is called just after it’s creation. In other case Activity can also be started
by calling restart method i.e after activity stop. So this means onStart() gets called by
Android OS when user switch between applications. For example, if a user was using
Application A and then a notification comes and user clicked on notification and moved to
Application B, in this case Application A will be paused. And again if a user again click on
app icon of Application A then Application A which was stopped will again gets started.
Activity Resumed:.onResume():
Activity resumed is that situation when it is actually visible to user means the data
displayed in the activity is visible to user. In lifecycle it always gets called after activity start
and in most use case after activity paused (onPause).
Activity Paused: onPause():
Activity is called paused when it’s content is not visible to user, in most case onPause()
method called by Android OS when user press Home button (Center Button on Device) to
make hide.
Activity also gets paused before stop called in case user press the back navigation button.
The activity will go in paused state for these reasons also if a notification or some other
dialog is overlaying any part (top or bottom) of the activity (screen). Similarly, if the other
screen or dialog is transparent then user can see the screen but cannot interact with it.
Activity Stopped: onStop():
Activity is called stopped when it’s not visible to user. Any activity gets stopped in case
some other activity takes place of it. For example, if a user was on screen 1 and click on
some button and moves to screen 2. In this case Activity displaying content for screen 1
will be stopped.

GFGC Boy’s College, Kolar 10 By: Divakar M.S


Mobile Application & Development Unit II- Android Application
Design Essentials

Every activity gets stopped before destroy in case of when user press back navigation
button. So Activity will be in stopped state when hidden or replaced by other activities that
have been launched or switched by user. In this case application will not present anything
useful to the user directly as it’s going to stop.
Activity Restarted: onRestart():
Activity is called in restart state after stop state. So activity’s onRestart() function gets
called when user comes on screen or resume the activity which was stopped. In other
words, when Operating System starts the activity for the first time onRestart() never gets
called. It gets called only in case when activity is resumes after stopped state.
Activity Destroyed: onDestroy():
Any activity is known as in destroyed state when it’s not in background. There can
different cases at what time activity get destroyed.
First is if user pressed the back navigation button then activity will be destroyed after
completing the lifecycle of pause and stop.
After the Activity is destroyed if user again click the app icon, in this case activity will be
recreated and follow the same lifecycle again. Another use case is with Splash Screens if
there is call to finish() method from onCreate() of an activity then OS can directly call
onDestroy() with calling onPause() and onStop().

 Android Service

Android service is a component that is used to perform operations on the background


such as playing music, handle network transactions, interacting content providers etc. It
doesn't has any UI (user interface).

The service runs in the background indefinitely even if application is destroyed. Moreover,
service can be bounded by a component to perform interactivity and inter process
communication (IPC).

Features of Service

 Service is an Android Component without an UI


 It is used to perform long running operations in background. Services run
indefinitly unless they are explicitly stopped or destroyed
 It can be started by any other application component. Components can even infact
bind to a service to perform Interprocess- Comminication
 It can still be running even if the application is killed unless it stops itself by calling
stopself() or is stopped by a Android component by calling stopService().
 If not stopped it goes on running unless is terminated by Android due to resource
shortage
 The android.app.Service is subclass of ContextWrapper class.

GFGC Boy’s College, Kolar 11 By: Divakar M.S


Mobile Application & Development Unit II- Android Application
Design Essentials

Android platform service

The Android platform provides and runs predefined system services and every Android
application can use them, given the right permissions. These system services are usually
exposed via a specific Manager class. Access to them can be gained via
the getSystemService() method.

Life Cycle of Android Service

There can be two forms of a service.The lifecycle of service can follow two different paths:
started or bound.

1. Started
2. Bound

1) Started Service
A service is started when component (like activity) calls startService() method, now
it runs in the background indefinitely. It is stopped by stopService() method. The
service can stop itself by calling the stopSelf() method.

2) Bound Service
A service is bound when another component (e.g. client) calls bindService()
method. The client can unbind the service by calling the unbindService() method.
The service cannot be stopped until all clients unbind the service.

 Intent in Android And Types


Android uses Intent for communicating between the components of an Application and
also from one application to another application.
Intent are the objects which is used in android for passing the information among Activities
in an Application and from one app to another also. Intent are used for communicating
between the Application components and it also provides the connectivity between two
apps.
For example: Intent facilitate you to redirect your activity to another activity on occurrence
of any event. By calling, startActivity() you can perform this task.

Intent intent = new Intent(getApplicationContext(),


SecondActivity.class); startActivity(intent);

In the above example, foreground activity is getting redirected to another activity i.e.
SecondActivity.java. getApplicationContext() returns the context for your foreground
activity.

GFGC Boy’s College, Kolar 12 By: Divakar M.S


Mobile Application & Development Unit II- Android Application
Design Essentials

Types of Intents:
Intent are of two types: Explicit Intent and Implicit Intent

Explicit Intent:

 Explicit Intents are used to connect the application internally.


 In Explicit we use the name of component which will be affected by Intent. For
Example: If we know class name then we can navigate the app from One Activity to
another activity using Intent. In the similar way we can start a service to download
a file in background process.

Explicit Intent work internally within an application to perform navigation and data transfer.
The below given code snippet will help you understand the concept of Explicit Intents

Intent intent = new Intent(getApplicationContext(), SecondActivity.class);


startActivity(intent);

Here SecondActivity is the JAVA class name where the activity will now be navigated.
Example with code in the end of this post will make it more clear.

GFGC Boy’s College, Kolar 13 By: Divakar M.S


Mobile Application & Development Unit II- Android Application
Design Essentials

Implicit Intent:

 In Implicit Intents we do need to specify the name of the component. We just specify
the Action which has to be performed and further this action is handled by the
component of another application.
 The basic example of implicit Intent is to open any web page

Let’s take an example to understand Implicit Intents more clearly. We have to open a
website using intent in your application. See the code snippet given below

Intent intentObj = new Intent(Intent.ACTION_VIEW);


intentObj.setData(Uri.parse("https://www.abhiandroid.com"));
startActivity(intentObj);

Unlike Explicit Intent you do not use any class name to pass through Intent(). In this
example we has just specified an action. Now when we will run this code then Android will
automatically start your web browser and it will open AbhiAndroid home page.

In Android:

Let’s implement Intent for a very basic use. In the below example we will Navigate from one
Activity to another and open a web homepage

GFGC Boy’s College, Kolar 14 By: Divakar M.S


Mobile Application & Development Unit II- Android Application
Design Essentials

 Receiving and Broadcast Intent:


In android, we can receive broadcasts by registering in two ways.

One way is by registering a broadcasts using android application manifest file


(AndroidManifest.xml). We need to specify <receiver> element in apps manifest file like as
shown below.
<receiver android:name=".SampleBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>

The above statement will fire the defined system broadcast event whenever the boot process
is completed.

Another way is to register a receiver dynamically via Context.registerReceiver() method. To


register broadcast receiver we need to extend our class using BroadcastReceiver and need to
implement a onReceive(Context, Intent) method like as shown below.

public class MainActivity extends BroadcastReceiver {


@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, log, Toast.LENGTH_LONG).show();
}
}
Suppose if we register for ACTION_BOOT_COMPLETED event, whenever the boot process is
completed the broadcast receiver’s method onReceive() method will be invoked.

Sending Broadcasts

In android, we can send a broadcasts in apps using three different ways, those are

Method Description

sendOrderedBroadcast(Intent, String) This method is used to send broadcasts to one


receiver at a time.

sendBroadcast(Intent) This method is used to send broadcasts to all


receivers in an undefined order.

LoadBroadcastManager.sendBroadcast This method is used to send broadcasts to receivers


that are in the same app as the sender.

GFGC Boy’s College, Kolar 15 By: Divakar M.S


Mobile Application & Development Unit II- Android Application
Design Essentials

Broadcasting Custom Intents


In android we can create our own custom broadcasts using intents. Following is the
simple code snippet of sending a broadcast by creating an intent using sendBroadcast(Intent)
method.

Intent sintent = new Intent();


intent.setAction("com.tutlane.broadcast.MY_NOTIFICATION");
intent.putExtra("data","Welcome to Tutlane");
sendBroadcast(intent);

If you observe above code snippet we create a custom Intent “sintent”. We need to
register our intent action in android manifest file like as shown below

<receiver android:name=".SampleBroadcastReceiver">
<intent-filter>
<action android:name=" com.tutlane.broadcast.MY_NOTIFICATION"/>
</intent-filter>
</receiver>

This is how we can create our own custom broadcasts using Intents in android applications.

System Brodcasting:

In android, several system events are defined as final static fields in the Intent class.
Following are the some of system events available in android applications.

Event Description

android.intent.action.BOOT_COMPLETED It raise an event, once boot completed.

android.intent.action.POWER_CONNECTED It is used to trigger an event when power connected


to the device.

android.intent.action.POWER_DISCONNECTED It is used to trigger an event when power got


disconnected from the device.

android.intent.action.BATTERY_LOW It is used to call an event when battery is low on


device.

android.intent.action.BATTERY_OKAY It is used to call an event when battery is OKAY


again.

android.intent.action.REBOOT It call an event when the device rebooted again.

GFGC Boy’s College, Kolar 16 By: Divakar M.S


Mobile Application & Development Unit II- Android Application
Design Essentials

 Android Manifest File and its common settings:

AndroidManifest.xml:

 Every project in Android includes a manifest file, which is AndroidManifest.xml,


stored in the root directory of its project hierarchy.

 The manifest file is an important part of our app because it defines the structure and
metadata of our application, its components, and its requirements.

 This file includes nodes for each of the Activities, Services, Content Providers and
Broadcast Receiver that make the application and using Intent Filters and Permissions,
determines how they co-ordinate with each other and other applications.

GFGC Boy’s College, Kolar 17 By: Divakar M.S


Mobile Application & Development Unit II- Android Application
Design Essentials

Java:

The Java folder contains the Java source code files. These files are used as a controller for
controlled UI (Layout file). It gets the data from the Layout file and after processing that data
output will be shown in the UI layout. It works on the backend of an Android application.
drawable:

A Drawable folder contains resource type file (something that can be drawn). Drawables
may take a variety of file like Bitmap (PNG, JPEG), Nine Patch, Vector (XML), Shape, Layers,
States, Levels, and Scale.
layout:

A layout defines the visual structure for a user interface, such as the UI for an Android
application. This folder stores Layout files that are written in XML language.
mipmap:

Mipmap folder contains the Image Asset file that can be used in Android Studio application.
You can generate the icon types like Launcher icons, Action bar and tab icons, and
Notification icons.

colors.xml:
colors.xml file contains color resources of the Android application. Different color values are
identified by a unique name that can be used in the Android application program.

Below is a sample colors.xml file:

<?xml version="1.0" encoding="utf-8"?>

<resources>

<color name="colorPrimary">#3F51B5</color>

<color name="colorPrimaryDark">#303F9F</color>

</resources>

strings.xml:

The strings.xml file contains string resources of the Android application. The different string
value is identified by a unique name that can be used in the Android application program. This
file also stores string array by using XML language.

GFGC Boy’s College, Kolar 18 By: Divakar M.S


Mobile Application & Development Unit II- Android Application
Design Essentials

styles.xml:

The styles.xml file contains resources of the theme style in the Android application. This
file is written in XML language. Below is a sample styles.xml file:

<resources>

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

<!-- Customize your theme here. -->

<item name="colorPrimary">@color/colorPrimary</item>

<item name="colorPrimaryDark">@color/colorPrimaryDark</item>

</style>

</resources>

build.gradle(Module: app):
This defines the module-specific build configurations. Here you can add dependencies
what you need in your Android application.

Example of AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tutlane.broadcastreceiver">

<application android:allowBackup="true" android:icon="@mipmap/ic_launcher"


android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true"
android:theme="@style/AppTheme">

<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>
</activity>
<receiver android:name="MyBroadcastReceiver">
<intent-filter>
<action android:name="com.tutlane.CUSTOM_INTENT">
</action>
</intent-filter>
</receiver>
</application>
</manifest>

GFGC Boy’s College, Kolar 19 By: Divakar M.S


Mobile Application & Development Unit II- Android Application
Design Essentials

 Intent Filter in Android :-


Intent Filter are the components which decide the behavior of an intent. As we have read
in our previous tutorial of Intent about the navigation of one activity to another, that can
be achieve by declaring intent filter. We can declare an Intent Filter for an Activity in
manifest file.
Intent filters specify the type of intents that an Activity, service or Broadcast receiver can
respond to. It declares the functionality of its parent component (i.e. activity, services or
broadcast receiver). It declares the capability of any activity or services or a broadcast
receiver.
Intent Filter Code Inside Android Manifest:
The code of Intent Filter is used inside AndroidManifest.xml file for an activity. You can see
it: open manifests folder >> open AndroidManifest.xml file

Syntax of Intent Filters:


Intent filter is declared inside Manifest file for an Activity.

<!--Here Name of Activity is .MainActivity, image of icon name stored in drawable folder, label
present inside string name is label-->
<!--If anything is different please customize the code according to your app-->
<activity android:name=".MainActivity">
<intent-filter android:icon="@drawable/icon"
android:label="@string/label">

<action android:name="android.intent.action.MAIN" />


<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

GFGC Boy’s College, Kolar 20 By: Divakar M.S


Mobile Application & Development Unit II- Android Application
Design Essentials

Attributes of Intent Filter:

Below are the attributes of Intent filter:


1. android:icon
An icon represents the activity, service or broadcast receiver when a user interact with it
or when it appears to user in an application. To set an icon you need to give reference of
drawable resource as declared android:icon=”@drawable/icon”.
How to add custom icon in your App:
Step 1: Open your android project folder in computer / system
Step 2: Open app>> src >> main >> res >> drawable >> here save custom icon image with
name
Step 3: Replace @drawable/icon with @drawable/your_image name in the below code

android:icon="@drawable/icon"

Important Note: If you have not declared any icon for your activity then android sets the icon
of parent component by default. And if no icon is defined by parent component then icon is set
as the default icon by <application> element (declared in Manifest file).
2.android:label
A label represents the title of an activity on the toolbar. You can have different Labels for
different Activities as per your requirement or choice. The label should be set as a reference
to a string resource. However, you can also use a raw string to set a label as declared in the
below given code snippet

android:label = "@string/label"
or
android:label = "New Activity"

How To Create Custom Label in App:


Step 1: Click on values values
Step 2: Open Strings.xml file present inside values
Step 3: Edit String value to your custom name. For example,

GFGC Boy’s College, Kolar 21 By: Divakar M.S


Mobile Application & Development Unit II- Android Application
Design Essentials

Elements In Intent Filter:

There are following three elements in an intent filter:

1. Action
2. Data
3. Category

Important Note: Every intent filter must contain action element in it. Data and category
element is optional for it.
1. Action:
It represent an activities action, what an activity is going to do. It is declared with the
name attribute as given below

<action android:name = "string" />

An Intent Filter element must contain one or more action element. Action is a string that
specifies the action to perform. You can declare your own action as given below. But we usually
use action constants defined by Intent class.

<intent-filter>
<action android:name="com.example.android.intentfilters.Main2Activity"/>
</intent-filter>

There are few common actions for starting an activity like ACTION_VIEW
ACTION_VIEW: This is used in an Intent with startActivity(). This helps when you redirect to
see any website, photos in gallery app or an address to view in a map app.
For example: As we have done in previous topic of Intent, we started a website using implicit
intent in that Intent we used ACTION_VIEW element to view website in the web browser.

Intent intentObj = new Intent(Intent.ACTION_VIEW);


intentObj.setData(Uri.parse("https://www.abhiandroid.com"));
startActivity(intentObj);

There are more actions similar to above like, ACTION_SEND, ACTION_MAIN,


ACTION_WEB_SEARCH and many more.
2. Data:
There are two forms in which you can pass the data, using URI(Uniform Resource
Identifiers) or MIME type of data. For understanding the concept of URI in better manner
check the link.

GFGC Boy’s College, Kolar 22 By: Divakar M.S


Mobile Application & Development Unit II- Android Application
Design Essentials

The syntax of data attribute is as follows:

<data android:scheme="string"
android:host="string"
android:port="string"
android:path="string"
android:pathPattern="string"
android:pathPrefix="string"
android:mimeType="string" />

This specifies the format of data associated with an Intent which you use with
component. As explained in above code snippet, data passed through Intent is a type of URI.
Check the below given table for better clarification

ACTION DATA MEANING


Opens phone application and calls
Intent.ACTION_CALL tel:phone_number
phone number
Opens phone application
Intent.ACTION_DIAL tel:phone_number and dials (but doesn’t call)
phone_number
Opens phone application
Intent.ACTION_DIAL voicemail: and dials (but doesn’t call)
the voice mail number.
Opens the maps
Intent.ACTION_VIEW geo:lat,long Application centered on (lat, long).

Opens the maps


Intent.ACTION_VIEW geo:0,0?q=address
application centered on the
specified address.
http://url https://url Opens the browser application to the
Intent.ACTION_VIEW specified address.

Opens the browser application and


uses Google search for given string
Intent.ACTION_WEB_SEARCH plain_text

3.Category:
This attribute of Intent filter dictates the behavior or nature of an Intent. There is a string
which contains some additional information about the intent which will be handled by a
component. The syntax of category is as follows:

<category android:name="string" />

Most of Intents do not require a category for example: CATEGORY_BROWSABLE,


CATEGORY_LAUNCHER.

GFGC Boy’s College, Kolar 23 By: Divakar M.S


Mobile Application & Development Unit II- Android Application
Design Essentials

BROWSABLE – Browsable category, activity allows itself to be opened with web browser to
open the reference link provided in data.
LAUNCHER – Launcher category puts an activity on the top of stack, whenever
application will start, the activity containing this category will be opened first.

<intent-filter>
<!--Code here-->
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<!--Code here-->
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>

 Permissions:-
The purpose of a permission is to protect the privacy of an Android user. Android apps must
request permission to access sensitive user data (such as contacts and SMS), as well as certain
system features (such as camera and internet). Depending on the feature, the system might
grant the permission automatically or might prompt the user to approve the request.
Add permissions to the manifest
On all versions of Android, to declare that your app needs a permission, put a <uses-
permission> element in your app manifest, as a child of the top-level <manifest> element. For
example, an app that needs to access the internet would have this line in the manifest:
Example
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.snazzyapp">

<uses-permission android:name="android.permission.INTERNET"/>
<!-- other permissions go here -->

<application ...>
...
</application>
</manifest>

GFGC Boy’s College, Kolar 24 By: Divakar M.S

You might also like