Chapter 2
Chapter 2
Chapter 2
By
FAHAD ASLAM
Chapter 2
Create a New Project
Anatomy of Android Application
Layout
◦ Write the XML
◦ Attributes
◦ Load the XML Resource
◦ ID
◦ Layout Parameters
◦ Layout Position
◦ Size, Padding and Margins
◦ Common Layouts
Create Android Application
Anatomy of Android Application
Sr.No. Folder, File & Description
1 Java
This contains the .java source files for your project. By
default, it includes an MainActivity.java source file having an
activity class that runs when your app is launched using the app
icon.
2 res/drawable-hdpi
This is a directory for drawable objects that are designed for
high-density screens.
3 res/layout
This is a directory for files that define your app's user
interface.
Sr.No. Folder, File & Description
4 res/values
This is a directory for other various XML files that contain a
collection of resources, such as strings and colours definitions.
5 AndroidManifest.xml
This is the manifest file which describes the fundamental
characteristics of the app and defines each of its components.
6 Build.gradle
This is an auto generated file which contains
compileSdkVersion, buildToolsVersion, applicationId,
minSdkVersion, targetSdkVersion, versionCode and versionName
Intent
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.
Types of intent
Implicit
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
Explicit
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.
Android Activity life Cycle
Activity
An Android activity is one screen of the Android app's
user interface. In that way an Android activity is very
similar to windows in a desktop application. An Android
app may contain one or more activities, meaning one or
more screens. The Android app starts by showing the
main activity, and from there the app may make it
possible to open additional activities
Android Activity life Cycle
If you have worked with C, C++ or Java programming
language then you must have seen that your program
starts from main() function.
Android system initiates its program with in
an Activity starting with a call on onCreate() callback
method.
Android Activity Lifecycle is controlled by 7 methods
of android.app.Activity class.
Fragment
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.
Following are important points about fragment −
A fragment has its own layout and its own behavior 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.
Fragment Life Cycle
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.
Basic XML Layouts - Containers
Designing Complex Uis
Arguably, LinearLayout is the most common modeling
tool. It offers a "box" model similar to the Java-Swing
Box-Layout. • Generally, complex UI designs result
from the combination of simpler nested boxes that show
their inner pieces using a horizontal or vertical
orientation
Basic XML Layouts - Containers
Summary of Commonly-used Android
containers
1. LinearLayout (the box model),
2. RelativeLayout (a rule-based model), and
3. TableLayout (the grid model), along with
4. ScrollView, a container designed to assist
with implementing scrolling containers.
5. Other (ListView, GridView, WebView,
MapView,…) discussed later
Basic XML Layouts - Containers
1. Linear Layout
LinearLayout is a box model – widgets or child
containers are lined up in a column or row, one after
the next. To configure a LinearLayout, you have five
main areas of control besides the container's contents:
• orientation,
• fill model,
• weight,
• gravity,
• padding ,
• margin
Basic XML Layouts - Containers
1. Linear Layout
Orientation