0% found this document useful (0 votes)
33 views27 pages

User Interfaces, Intent and Fragments

User Interfaces, Intent and Fragments

Uploaded by

nandishbs12234
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views27 pages

User Interfaces, Intent and Fragments

User Interfaces, Intent and Fragments

Uploaded by

nandishbs12234
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Course Details

Course Code : CSE3075

Course Name: Mobile Application Development [MAD]


Credit Structure: 3 Credits (L=1, P=4, C=3)

Instructor In-Charge: Dr. Bhavana A and Ms. Ramabhai

1
Module 2
User Interfaces, Intent and Fragments

2
Introduction to Activity
What is Activity?
• An Activity is an application component that allows a user to perform an
operation.
• Eg. Making call, capturing photos and sending messages etc.
• An Application is an colection of activities
• Main activity provides interface to the user and further invokes the other activity

3
Creating an Activity
• You can create an activity by creating a java class that extends the Activity class.
• Each activity needs to create a window in which its user interface components can
be loaded.
• setContentView(View) method is inoked
• onCreate method is called when Activity class is first created.

4
Structure of a typical Android
Application (Android Studio)

5
View, View Groups and Layouts
• A layout defines the structure for a user interface in your app, such as in an activity.
• All elements in the layout are built using a hierarchy of View and View
Group objects.
• A View usually draws something the user can see and interact with.
• Whereas a View Group is an invisible container that defines the layout structure
for View and other View Group objects.
• The View objects are usually called "widgets" and can be one of many subclasses,
such as Button or TextView.
• The ViewGroup objects are usually called "layouts" can be one of many types that
provide a different layout structure, such as LinearLayout or ConstraintLayout .

6
View, View Groups and Layouts
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. You can also use Android Studio's Layout Editor to build your XML layout using
a drag-and-drop interface.
• Instantiate layout elements at runtime. Your app can create View and View Group
objects (and manipulate their properties) programmatically.

7
Common Layouts

8
Linear Layout
• Linear Layout 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.

9
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).

• A Relative Layout is a very powerful utility for designing a user interface because it
can eliminate nested view groups and keep your layout hierarchy flat, which improves
performance. If you find yourself using several nested Linear Layout groups, you may
be able to replace them with a single Relative Layout.

10
Constraint Layout
• Constraint Layout allows you to create large and complex layouts with a flat view
hierarchy (no nested view groups). It's similar to Relative Layout in that all views are
laid out according to relationships between sibling views and the parent layout, but
it's more flexible than Relative Layout and easier to use with Android Studio's Layout
Editor.
• All the power of Constraint Layout is available directly from the Layout Editor's visual
tools, because the layout API and the Layout Editor were specially built for each other.
So you can build your layout with Constraint Layout entirely by drag-and-dropping
instead of editing the XML.

11
Building Layouts with an Adapter
• When the content for your layout is dynamic or not pre-determined, you can use a
layout that subclasses Adapter View to populate the layout with views at runtime.
A subclass of the Adapter View class uses an Adapter to bind data to its layout.

12
Recycler View
• The Recycler View widget is a more advanced and flexible version of List View.
• If your app needs to display a scrolling list of elements based on large data sets (or
data that frequently changes), you should use Recycler View.
• Several different components work together to display your data.

13
Intent
• An Intent is a messaging object you can use to request an action from another app
component.
• Although intents facilitate communication between components in several ways,
• There are three fundamental use cases:
• Starting an activity
• Starting a service
• Delivering a broadcast

14
Starting an activity: Cont…
• An Activity represents a single screen in an app.
• You can start a new instance of an Activity by passing an Intent to startActivity().
The Intent describes the activity to start and carries any necessary data.
• If you want to receive a result from the activity when it finishes,
call startActivityForResult().
• Your activity receives the result as a separate Intent object in your
activity's onActivityResult() callback.

15
Intent Types Cont…
• Explicit intents specify which application will satisfy the intent, by supplying
either the target app's package name or a fully-qualified component class name.
You'll typically use an explicit intent to start a component in your own app,
because you know the class name of the activity or service you want to start.
• For example, you might start a new activity within your app in response to a
user action, or start a service to download a file in the background.
• Implicit intents do not name a specific component, but instead declare a general
action to perform, which allows a component from another app to handle it.
• For example, if you want to show the user a location on a map, you can use
an implicit intent to request that another capable app show a specified
location on a map.

16
The two main components of an Intent are:
• Action
• The built-in action to be performed, such as ACTION_VIEW, ACTION_EDIT, ACTION_CALL,
ACTION_SENDTO,... or a user-created-activity
• Data
• Basic argument needed by the intent to work.
For instance: a phone number to be called , a picture to be shown, a message to be sent, etc.

Activity- Intent: { action + data }


Activity-
1 2
Optional results

17
Components of an Intent : Android Actions
List of common actions that Intents can use for launching built-in activities [usually
through startActivity(Intent) ]

ACTION_MAIN ACTION_ANSWER
ACTION_VIEW ACTION_INSERT
ACTION_ATTACH_DATA ACTION_DELETE
ACTION_EDIT ACTION_RUN
ACTION_PICK ACTION_SYNC
ACTION_CHOOSER ACTION_PICK_ACTIVITY
ACTION_GET_CONTENT ACTION_SEARCH
ACTION_DIAL ACTION_WEB_SEARCH
ACTION_CALL ACTION_FACTORY_TEST
ACTION_SEND
ACTION_SENDTO

18
Components of an Intent : Data
• Data Data is supplied as an URI, i.e. a string whose prefix indicates the composition of the data item.
For instance:
• tel://,
• http://,
• mailto://,
• file://,
• content://,
• geo:,
• audio/,
• media/,
• vnd.android.cursor.dir

19
Fragments
• A Fragment represents a behavior or a portion of user interface in
a FragmentActivity.
• You can combine multiple fragments in a single activity to build a multi-pane UI and
reuse a fragment in multiple activities.
• A fragment must always be hosted in an ACTIVITY (Main Host Container)
activity and the fragment's lifecycle is
directly affected by the host activity's Fragment1
lifecycle. (View 1)
Fragment3
• you can manipulate each fragment
(View 3)
independently, such as add or remove them.
Fragment2
(View 2)

20
Fragments
• For example, when the activity is paused, so are all fragments in it, and when the
activity is destroyed, so are all fragments. However, while an activity is running
(it is in the resumed lifecycle state)
• When you add a fragment as a part of your activity layout, it lives in
a ViewGroup inside the activity's view hierarchy and the fragment defines its
own view layout.
• You can insert a fragment into your activity layout by declaring the fragment in
the activity's layout file, as a <fragment> element, or from your application code
by adding it to an existing ViewGroup.

21
Fragments

Reference: https://developer.android.
com/guide/components/fragments

22
Fragment’s Lifecycle
• onAttach() Invoked when the fragment has been connected to the
host activity.
• onCreate() Used for initializing non-visual components needed
by the fragment.
• onCreateView() Most of the work is done here. Called to create
the view hierarchy representing the fragment. Usually inflates a
layout, defines listeners, and populates the widgets in the inflated
layout.

23
Fragment’s Lifecycle
• onPause() The session is about to finish. Here you should
commit state data changes that are needed in case the fragment is
re-executed.

• onDetach() Called when the inactive fragment is disconnected


from the activity.

24
END OF MODULE 2

25
Learn more …
• https://developer.android.com/reference/android/view/View
• https://www.geeksforgeeks.org/difference-between-view-and-viewgroup-in-android/
• https://developer.android.com/develop/ui/views/layout/declaring-layout
• https://data-flair.training/blogs/android-layout-and-views/
• https://developer.android.com/develop/ui/views/components/menus
• https://www.geeksforgeeks.org/android-menus/
• https://developer.android.com/guide/components/intents-filters
• https://www.geeksforgeeks.org/what-is-intent-in-android/
• https://www.geeksforgeeks.org/implicit-and-explicit-intents-in-android-with-examples/
• https://developer.android.com/guide/fragments#:~:text=A%20Fragment%20represents%20a%20
reusable,an%20activity%20or%20another%20fragment
.
• https://www.geeksforgeeks.org/fragment-lifecycle-in-android/

26
Thank You

27

You might also like