2. App components are the essential building blocks of
an Android app. Each component is an entry point
through which the system or a user can enter your
app. Some components depend on others.
There are four different types of app components:
Activities: They dictate the UI and handle the user
interaction to the smart phone screen.
Services: They handle background processing
associated with an application.
Broadcast receivers: They handle communication
between Android OS and applications.
Content providers: They handle data and database
management issues.
Android - Application Components
3. An activity represents a single screen with a user
interface, in-short Activity performs actions on the
screen.
For example, an email application might have one
activity that shows a list of new emails, another
activity to compose an email, and another activity 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.
An activity is implemented as a subclass
of Activity class as follows −
public class MainActivity extends Activity { }
Activity
4. 4
Android Components: Activities
An Activity corresponds to a single
screen of the Application.
An Application can be composed of
multiples screens (Activities).
The Home Activity is shown when the
user launches an application.
Different activities can exhange
information one with each other.
Hello
World!
Android
HelloWorld
Button1
6. 6
Android Components: Activities
Each activity is composed by a list of graphics
components.
Some of these components (also called Views) can
interact with the user by handling events (e.g. Buttons).
Two ways to build the graphic interface:
8. 8
Android Components: Activities
Android applications typically use both the approaches!
DECLARATIVE APPROACH
PROGRAMMATIC APPROACH
Define the Application layouts
and resources used by the
Application (e.g. labels).
Manages the events, and
handles the interaction with
the user.
XML Code
Java Code
10. As a user navigates through the app, Activity instances in your app transition through
different stages in their life-cycle. The Activity class provides a number of callbacks
that allow the activity to know that a state has changed: that the system is creating,
stopping, or resuming an activity, or destroying the process in which the activity
resides.
The Activity class defines the following call backs i.e. events. User need not
implement all the callbacks methods.
Android Activity Lifecycle is controlled by 7 methods of android.app.Activity class
Android initiates the program within an activity with a call to
onCreate() callback method, called when the activity is first created.
onStart(): This callback method is called when the activity becomes visible to the
user.
onResume(): The activity is in the foreground and the user can interact with it.
onPause(): Activity is partially obscured by another activity. Another activity that’s in
the foreground is semi-transparent. The paused activity does not receive user input
and cannot execute any code.
onStop(): The activity is completely hidden and not visible to the user.
onRestart(): From the Stopped state, the activity either comes back to interact with
the user or the activity is finished running .If the activity comes back, the system
invokes onRestart()
onDestroy(): Activity is destroyed and removed from the memory.
11. When you open the app it will go through
below states:
onCreate() –> onStart() –> onResume()
When you press the back button and exit the app
onPaused() — > onStop() –> onDestory()
When you press the home button
onPaused() –> onStop()
If a phone is ringing and user is using the app
onPause() –> onResume()
12. 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.
There are two types of services local and remote.
Local service is accessed from within the application
whereas remote service is accessed remotely from
other applications running on the same device.
A service is implemented as a subclass of Service class
as follows −
public class MyService extends Service { }
Services
13. 13
Android Components: Services
Services: like Activities, but run in background and do
not provide an user interface.
Used for non-interactive tasks (e.g. networking).
Service life-time composed of 3 states:
Starting Destroyed
Running
(on background)
onCreate()
onStart()
onDestroy()
14.
15. UnBound Service:
1.Basically used for long repetitive task.
2.startService() is the method use to start unbound service.
3.stopService() is the method use to stop explicitly.
4.It is independent of the component from which it starts.
Bound Service:
1.Basically used for long repetitive task but bound with the
component.
2.starts by bindService().
3.unbindService() is the method use to stop explicitly.
4.It is dependent of the component from which it starts
5.bound service offers a client-server interface that allows
components to interact with the service, send requests, get results.
16. 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
broadcaster as an Intent object.
public class MyReceiver extends BroadcastReceiver {
public void onReceive(context,intent){} }
Broadcast Receivers
17. 17
Android Components: Broadcast Receivers
Publish/Subscribe
paradigm
Broadcast
Receivers: An
application can be
signaled of
external events.
Notification types:
Call incoming, SMS
delivery, Wifi
network detected,
etc
18. 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.
public class MyContentProvider extends
ContentProvider { public void onCreate(){} }
Content Providers
19. S.No Components & Description
1
Fragments
Represents a portion of user interface in an Activity.
2
Views
UI elements that are drawn on-screen including buttons, lists forms
etc.
3
Layouts
View hierarchies that control screen format and appearance of the
views.
4
Intents
Messages wiring components together or to invoke components.
5
Resources
External elements, such as strings, constants and drawable pictures.
6
Manifest
Configuration file for the application.
Additional Components
20. 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.
23. start your application development by calling start a
new android studio project. in a new installation
frame should ask Application name, package
information and location of the project.−
24. After entered application name, it going to be called
select the form factors your application runs on, here
need to specify Minimum SDK.
25. The next level of installation should contain selecting
the activity to mobile, it specifies the default layout
for Applications.
29. Java file contains
(1) Activity is a java class that creates and default window on the
screen where we can place different components such as Button,
EditText, TextView, Spinner etc. It is like the Frame of Java AWT.
It provides life cycle methods for activity such as onCreate, onStop,
OnResume etc.
(2) The onCreate method is called when Activity class is first
created.
(3) The setContentView(R.layout.activity_main) gives information
about our layout resource. Here, our layout resources are defined in
activity_main.xml file.
This is the actual application file which ultimately gets converted to
a Dalvik executable and runs your application.
Generated R.java file
It is the auto-generated file that contains IDs for all the resources of
res directory.
It is generated by aapt(Android Asset Packaging Tool). Whenever
you create any component on activity_main, a corresponding ID is
created in the R.java file which can be used in the Java Source file
later.
30. The Main Activity File
The main activity code is a Java file MainActivity.java. This is the
actual application file which ultimately gets converted to a Dalvik
executable and runs your application. Following is the default code
generated by the application wizard for Hello World! Application.
package com.example.helloworld;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Here, R.layout.activity_main refers to the activity_main.xml file
located in the res/layout folder. The onCreate() method is one of
many methods that are figured when an activity is loaded.
31. APK File(android package)
An apk file is created by the framework automatically. If
you want to run the android application on the mobile,
transfer and install it.
Resources
It contains resource files including activity_main, strings,
styles etc.
res/drawable-hdpi: This is a directory for drawable objects
that are designed for high-density screens
res/layout: This is a directory for files that define your app's
user interface.
res/values: This is a directory for other various XML files
that contain a collection of resources, such as strings and
colours definitions.
32. The Manifest File
Whatever component you develop as a part of your
application, you must declare all its components in
a manifest.xml which resides at the root of the application
project directory.
This file works as an interface between Android OS and
your application, so if you do not declare your component
in this file, then it will not be considered by the OS.
Following is the list of tags which you will use in your
manifest file to specify different Android application
components −
<activity>elements for activities
<service> elements for services
<receiver> elements for broadcast receivers
<provider> elements for content providers
34. layout/
XML files that define a user interface layout. They are saved in res/layout/ and accessed
from the R.layout class.
menu/
XML files that define application menus, such as an Options Menu, Context Menu, or Sub
Menu. They are saved in res/menu/ and accessed from the R.menu class.
raw/
Arbitrary files to save in their raw form. You need to call Resources.openRawResource()
with the resource ID, which is R.raw.filename to open such raw files.
values/
XML files that contain simple values, such as strings, integers, and colors. For example, here
are some filename conventions for resources you can create in this directory −
arrays.xml for resource arrays, and accessed from the R.array class.
integers.xml for resource integers, and accessed from the R.integer class.
bools.xml for resource boolean, and accessed from the R.bool class.
colors.xml for color values, and accessed from the R.color class.
dimens.xml for dimension values, and accessed from the R.dimen class.
strings.xml for string values, and accessed from the R.string class.
styles.xml for styles, and accessed from the R.style class.
35. A View is a simple building block of a user interface.
It can be TextView, EditText, or even a button.
It occupies the area on the screen in a rectangular area
and is responsible for drawing and event handling
View is a superclass of all the graphical user interface
components.
The use of a view is to draw content on the screen of the
user’s Android device.
TextView Date Picker
RadioButton CheckBox buttons
Image View EditText
Button Image Button
What is Android View
36. A View Group is a subclass of the View Class and can be
considered as a superclass of Layouts.
It provides an invisible container to hold the views or
layouts.
it can be understood as a special view that can hold other
views that are often known as a child view.
Commonly used subclasses for ViewGroup:
LinearLayout
RelativeLayout
FrameLayout
GridView
ListView
Android View Group
37. layouts which are subclasses of ViewGroup class and a
typical layout defines the visual structure for an
Android user interface .
User can declare your layout using simple XML
file main_layout.xml which is located in the res/layout
folder of your project.
A layout may contain any type of widgets such as
buttons, labels, textboxes, and so on.
Android - UI Layouts
38. android:id: It uniquely identifies the Android Layout.
android:layout_height: It sets the height of the layout.
android:layout_width: It sets the width of the layout.
android:layout_gravity: It sets the position of the child view.
android:layout_marginTop: It sets the margin of the from the
top of the layout.
android:layout_marginBottom: It sets the margin of the from
the bottom of the layout.
android:layout_marginLeft: It sets the margin of the from the
left of the layout.
android:layout_marginRight: It sets the margin of the from the
right of the layout.
android:layout_x: It specifies the x coordinates of the layout.
android:layout_y: It specifies the y coordinates of the layout.
Attributes of Layout in Android
39. Android LinearLayout is a view group that aligns all
children in either vertically or horizontally.
A vertical layout has a column of Views, whereas in a
horizontal layout there is a row of Views.
It supports a weight attribute for each child View that can
control the relative size of each child
View within the available space.
android:orientation:This specifies
the direction of arrangement and
you will use "horizontal" for a row, “
vertical" for a column.
The default is horizontal.
LinearLayout
41. RelativeLayout enables you to specify how child views are positioned
relative to each other. The position of each view can be specified as relative
to sibling elements or relative to the parent.
android:id:This is the ID which uniquely identifies the layout.
android:gravity:This specifies how an object should position its content, on
both the X and Y axes. Possible values are top, bottom, left, right, center,
center_vertical, center_horizontal etc.
Properties:android:layout_alignParentTop
android:layout_above,
android:layout_marginBottom
android:layout_centerHorizontal
android:layout_alignBottom
android:layout_marginTop
android:layout_alignParentBottom
android:layout_centerVertical
android:layout_toRightOf
RelativeLayout
43. Frame Layout is designed to block out an area on the
screen to display a single item.
FrameLayout should be used to hold a single child view.
By default the position is the top-left corner, though the
gravity attribute can be used to alter its locations.
android:foreground:This defines the drawable to draw
over the content and possible values may be a color value,
in the form of "#rgb", "#rrggbb”.
android:foregroundGravity:Defines the gravity to apply to
the foreground drawable. The gravity defaults to fill.
Possible values are top, bottom, left, right, center,
center_vertical, center_horizontal etc.
FrameLayout
46. Android TableLayout going to be arranged groups of
views into rows and columns.
We use the <TableRow> element to build a row in the
table. Each row has zero or more cells;
each cell can hold one View object.
TableLayout containers do not display
border lines for their rows, columns, or cells.
TableLayout
48. An Absolute Layout lets you specify exact locations
(x/y coordinates) of its children.
Absolute layouts are less flexible and harder to
maintain than other types of layouts without absolute
positioning.
android:layout_x:This specifies the x-coordinate of
the view.
android:layout_y:This specifies
the y-coordinate of the view.
Absolute Layout
50. A TextView displays text to the user and optionally allows them
to edit it.
A TextView is a complete text editor, however the basic class is
configured to not allow editing.
TextView code in XML:
<TextView android:id="@+id/simpleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android" />
TextView code in JAVA:
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText("Android"); //set text for text view
TextView
51. 1 android:id
This is the ID which uniquely identifies the control.
2 android:capitalize
If set, specifies that this TextView has a textual input method and should automatically
capitalize what the user types.
•Don't automatically capitalize anything - 0
•Capitalize the first word of each sentence - 1
•Capitalize the first letter of every word - 2
•Capitalize every character - 3
3 android:cursorVisible
Makes the cursor visible (the default) or invisible. Default is false.
4 android:editable
If set to true, specifies that this TextView has an input method.
5 android:fontFamily
Font family (named by string) for the text.
6 android:gravity
Specifies how to align the text by the view's x- and/or y-axis when the text is smaller
than the view.
7 android:hint
Hint text to display when the text is empty.
8 android:inputType
The type of data being placed in a text field. Phone, Date, Time, Number, Password etc.
13 android:password
Whether the characters of the field are displayed as password dots instead of
themselves. Possible value either "true" or "false".
52. 14 android:phoneNumber
If set, specifies that this TextView has a phone number input method. Possible value either
"true" or "false".
15 android:text
Text to display.
16 android:textAllCaps
Present the text in ALL CAPS. Possible value either "true" or "false".
17 android:textColor
Text color. May be a color value, in the form of "#rgb", "#argb", "#rrggbb", or
"#aarrggbb".
21 android:textSize
Size of the text. Recommended dimension type for text is "sp" for scaled-pixels (example:
15sp).
22 android:textStyle
Style (bold, italic, bolditalic) for the text. You can use or more of the following values
separated by '|'.
•normal - 0
•bold - 1
•italic - 2
23 android:typeface
Typeface (normal, sans, serif, monospace) for the text. You can use or more of the
following values separated by '|'.
•normal - 0
•sans - 1
•serif - 2
•monospace - 3
53. A EditText is an overlay over TextView that configures
itself to be editable. It is the predefined subclass of
TextView that includes rich editing capabilities.
EditText
Styles of edit text
54. 1 android:autoText
If set, specifies that this TextView has a textual input method and
automatically corrects some common spelling errors.
2 android:drawableBottom
This is the drawable to be drawn below the text.
3 android:drawableRight
This is the drawable to be drawn to the right of the text.
4 android:editable
If set, specifies that this TextView has an input method.
5 android:text
This is the Text to display.
Inherited from android.widget.TextView Class
55. 1 android:background
This is a drawable to use as the background.
2 android:contentDescription
This defines text that briefly describes content of the view.
3 android:id
This supplies an identifier name for this view.
4 android:onClick
This is the name of the method in this View's context to invoke when
the view is clicked.
5 android:visibility:This controls the initial visibility of the view.
Inherited from android.view.View Class
56. A AutoCompleteTextView is a view that is similar to
EditText, except that it shows a list of completion
suggestions automatically while the user is typing.
The list of suggestions is displayed in drop down menu.
The user can choose an item from there to replace the
content of edit box with.
The Threshold property of AutoCompleteTextView is
used to define the minimum number of characters the
user must type to see the list of suggestions.
AutoCompleteTextView
57. 1
android:completionHint
This defines the hint displayed in the drop down menu.
2
android:completionThreshold
This defines the number of characters that the user must type before
completion suggestions are displayed in a drop down menu.
3
android:dropDownAnchor
This is the View to anchor the auto-complete dropdown to.
4
android:dropDownHeight
This specifies the basic height of the dropdown.
5
android:dropDownHorizontalOffset
The amount of pixels by which the drop down should be offset
horizontally.
6
android:dropDownSelector
This is the selector in a drop down list.
7
android:dropDownVerticalOffset
The amount of pixels by which the drop down should be offset vertically.
8
android:dropDownWidth
This specifies the basic width of the dropdown.
58. A Button is a Push-button which can be pressed, or clicked, by the user to perform an
action.
Buttons in android will contain a text or an icon or both and perform an action when
the user touches it.
There are different type of buttons available to use based on the requirements, those
are ImageButton, ToggleButton, RadioButton.
In html file include
android:onClick="addOperation“
In java file include
/** Called when the user touches the button */
public void addOperation(View view) {
// Do something in response to the button click
}
After oncreate include:
Button btnAdd = (Button)findViewById(R.id.addBtn);
btnAdd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Do something in response to button click
}
});
}
Button
59. 1 android:autoText
If set, specifies that this TextView has a textual input method and
automatically corrects some common spelling errors.
2 android:drawableBottom
This is the drawable to be drawn below the text.
3 android:drawableRight
This is the drawable to be drawn to the right of the text.
4 android:editable
If set, specifies that this TextView has an input method.
5 android:text
This is the Text to display.
Inherited from android.widget.TextView Class
60. 1 android:contentDescription
This defines text that briefly describes content of the view.
2 android:id
This supplies an identifier name for this view.
3 android:onClick
This is the name of the method in this View's context to
invoke when the view is clicked.
4 android:visibility
This controls the initial visibility of the view.
Inherited from android.view.View Class
61. Android Image Button
In android, Image Button is a user interface control that is used to display a button
with an image to perform an action when the user clicks or tap on it.
Generally, the Image button in android looks similar as regular Button and perform the
actions same as regular button but only difference is for image button we will add an
image instead of text.
Android Toggle Button
In android, Toggle Button is a user interface control that is used to display ON
(Checked) or OFF (Unchecked) states as a button with a light indicator.
Android CheckBox
In android, Checkbox is a two-states button that can be either checked or unchecked.
use check-boxes when presenting users with a group of selectable options that are
not mutually exclusive.
Android Radio Button
In android, Radio Button is a two-states button that can be either checked or
unchecked and it cannot be unchecked once it is checked.
62. Android Radio Group
In android, Radio Group is used to group one or more
radio buttons into separate groups based on our
requirements.
In case if we group radio buttons using the radio
group, at a time only one item can be selected from
the group of radio buttons.
Android ProgressBar
In android, ProgressBar is a user interface control
which is used to indicate the progress of an
operation.
63. ` getMax()
This method returns the maximum value of the progress.
2 incrementProgressBy(int diff)
This method increments the progress bar by the difference of value passed
as a parameter.
3 setIndeterminate(boolean indeterminate)
This method sets the progress indicator as determinate or indeterminate.
4 setMax(int max)
This method sets the maximum value of the progress dialog.
5 setProgress(int value)
This method is used to update the progress dialog with some specific value.
64. Android Spinner
In android, Spinner is a drop-down list which allows a
user to select one value from the list.
Android TimePicker
In android, TimePicker is a widget for selecting the
time of day, either in 24-hour or AM/PM mode.
Android DatePicker
In android, DatePicker is a widget for selecting a date
consisting of day, month and year