Unit 2

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 66

UNIT-2- ANDROID UI DESIGN

Android Activity Lifecycle


Android Activity Lifecycle is controlled by 7
methods of android.app.Activity class. The android
Activity is the subclass of ContextThemeWrapper
class.
An activity is the single screen in android. It is like
window or frame of Java.
By the help of activity, you can place all your UI
components or widgets in a single screen.
The 7 lifecycle method of Activity describes how
activity will behave at different states.
Android Activity Lifecycle methods
Android Activity Lifecycle methods
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns
:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-
auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context="example.javatpoint.com.activitylifecyc
le.MainActivity">
activity_main.xml
 <TextView
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Hello World!"
 app:layout_constraintBottom_toBottomOf="pare
nt"
 app:layout_constraintLeft_toLeftOf="parent"
 app:layout_constraintRight_toRightOf="parent"
 app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
File: MainActivity.java
 package example.javatpoint.com.activitylifecycle;
 import android.app.Activity;
 import android.os.Bundle;
 import android.util.Log;
 public class MainActivity extends Activity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 Log.d("lifecycle","onCreate invoked");
 }
File: MainActivity.java
 @Override
 protected void onStart() {
 super.onStart();
 Log.d("lifecycle","onStart invoked");
 }
 @Override
 protected void onResume() {
 super.onResume();
 Log.d("lifecycle","onResume invoked");
 }
File: MainActivity.java
 @Override
 protected void onPause() {
 super.onPause();
 Log.d("lifecycle","onPause invoked");
 }
 @Override
 protected void onStop() {
 super.onStop();
 Log.d("lifecycle","onStop invoked");
 }
File: MainActivity.java
 @Override
 protected void onRestart() {
 super.onRestart();
 Log.d("lifecycle","onRestart invoked");
 }
 @Override
 protected void onDestroy() {
 super.onDestroy();
 Log.d("lifecycle","onDestroy invoked");
 }
}
Uses for the Support Libraries
There are a few distinct uses for the support libraries.
Here is a more complete list of ways you can use the
support libraries in your app:
Backward Compatibility for newer APIs - A large
amount of the support libraries provide backward
compatibility for newer framework classes and methods.
For example, the Fragment support class provides
support for fragments on devices running versions earlier
than Android 3.0 (API level 11).
Uses for the Support Libraries
Convenience and Helper Classes - The support libraries
provides a number of helper classes, particularly for user
interface development.
For example the Recycler View class provides an user
interface widget for displaying and managing very long
lists, useable on versions of Android from API level 7 and
up.
Debugging and Utilities - There are a number of features
that provide utility beyond code you incorporate into your
app, including the support-annotations library for improved
code lint checks on method inputs and Multidex support for
configuring and distributing apps with over 65,536
methods.
Using Support versus Framework APIs
Support Libraries provide classes and methods that
closely resemble APIs in the Android Framework.
Here are the guidelines for when you should use
support library classes in place of Framework APIs:
Compatibility for a Specific Feature - If you want to
support a recent platform feature on devices that a
running earlier versions of the platform, use the
equivalent classes and methods from the support library.
Using Support versus Framework APIs
Compatibility for Related Library Features - More
sophisticated support library classes may depend on one
or more additional support library classes, so you should
use support library classes for those dependencies.
For example, the ViewPager support class should be
used with FragmentPagerAdapter or
the FragmentStatePagerAdapter support classes.
General Device Compatibility - If you do not have a
specific platform feature you intend to use with your app
in a backward compatible way, it is still a good idea to
use support library classes in your app.
Using Support versus Framework
APIs
For example, you may want to use ActivityCompat in
place of the framework Activity class, so you can take
advantage of newer features later on, such as incorporating
the new permissions model introduced in Android 6.0 (API
level 23).
Android 7.1 Nougat (API 25)
 Rearranged notification shade
 Touch/display performance improvements
 Developer features:
Shortcut manager APIs.
Circular app icons support.
Keyboard image insertion.
Fingerprint sensor gesture to open/close notification shade.
Manual storage manager Intent for apps.
Improved VR thread scheduling.
Enhanced wallpaper metadata.
Multi-endpoint call support.
Battery usage alerts.
API Level: 21
Android 5.0 (LOLLIPOP) offers new features for users
and app developers.
Support for 64-bit CPUs
Support for print previews
Vector drawables, which scale without losing definition.
Material design, bringing a restyled user interface.
Refreshed lock screen, no longer supporting widgets.
Refreshed notification tray and quick settings pull-down
Project Volta, for battery life improvements
API Level: 21
Updated emoji.
Smart lock feature.
Audio input and output through USB devices
Lock screen provides shortcuts to application and
notification settings.
Intent
public class Intent
extends Object implements Parcelable, Cloneable
java.lang.Object
android.content.Intent

An Intent provides a facility for performing late


runtime binding between the code in different
applications.
Its most significant use is in the launching of activities
Intent
It can be used with startActivity to launch
an Activity, broadcastIntent to send it to any
interested BroadcastReceiver components,
and Context.startService(Intent) or Context.bindServic
e(Intent, ServiceConnection, int) to communicate with
a background Service.
Intent Structure
Action - The general action to be performed, such
as ACTION_VIEW, ACTION_EDIT, ACTION_MAIN, etc.
Data - The data to operate on, such as a person record in
the contacts database, expressed as a Uri.
Intent
Some examples of action/data pairs are:
ACTION_VIEW - content://contacts/people/1 --
Display information about the person whose identifier
is "1".
ACTION_DIAL - content://contacts/people/1 --
Display the phone dialer with the person filled in.
ACTION_VIEW - tel:123 -- Display the phone dialer
with the given number filled in. Note how the VIEW
action does what is considered the most reasonable
thing for a particular URI.
Intent
Intent Resolution
Explicit Intents have specified a component
(via setComponent(ComponentName) or setClass(Conte
xt, Class)), which provides the exact class to be run.
Often these will not include any other information,
simply being a way for an application to launch various
internal activities it has as the user interacts with the
application.
Implicit Intents have not specified a component; instead,
they must include enough information for the system to
determine which of the available components is best to
run for that intent.
Intent
inter filter
syntax:
<intent-filter android:icon="drawable resource"
 android:label="string resource"
 android:priority="integer" >
 ...
</intent-filter>
inter filter
contained in:
<activity>
<activity-alias>
<service>
<receiver>
must contain:
<action>
can contain:
<category>
<data>
inter filter
Specifies the types of intents that an activity, service,
or broadcast receiver can respond to.
An intent filter declares the capabilities of its parent
component — what an activity or service can do and
what types of broadcasts a receiver can handle.
It opens the component to receiving intents of the
advertised type, while filtering out those that are not
meaningful for the component.
Most of the contents of the filter are described by its
<action>, <category>, and <data> subelements.
Intent attributes
android:icon
An icon that represents the parent activity, service, or
broadcast receiver when that component is presented to
the user as having the capability described by the filter.
This attribute must be set as a reference to a drawable
resource containing the image definition.
The default value is the icon set by the parent
component's icon attribute.
If the parent does not specify an icon, the default is the
icon set by the <application> element.
Intent attributes
android:label
A user-readable label for the parent component.
This label, rather than the one set by the parent
component, is used when the component is presented to
the user as having the capability described by the filter.
The label should be set as a reference to a string
resource, so that it can be localized like other strings in
the user interface.
Intent attributes
android:priority
It provides information about how able an activity is to
respond to an intent that matches the filter, relative to
other activities that could also respond to the intent.
When an intent could be handled by multiple activities
with different priorities, Android will consider only those
with higher priority values as potential targets for the
intent.

introduced in: API Level 1


Intent adding category
A string containing additional information about the kind of
component that should handle the intent.
Any number of category descriptions can be placed in an
intent, but most intents do not require a category.
Here are some common categories:
CATEGORY_BROWSABLE
The target activity allows itself to be started by a web browser
to display data referenced by a link, such as an image or an e-
mail message.
CATEGORY_LAUNCHER
The activity is the initial activity of a task and is listed in the
system's application launcher.
linking activities
Add a method to the MainActivity class that's called
by the button as follows:
In the file
app>java>com.example.myfirstapp>MainActivity, add
the sendMessage() method stub as shown below:
linking activities
1. Create new project with name “LinkingActivity”
2. Open res ➪ layout ➪ activity_main.xml , add button
simple drag drop or follow this step’s how to add
button in Android Button example , after adding new
button change its button name to “Go To Next “
linking activities
Now see acitivty_main.xml file code
linking activities
Now you need second UI for second activity : Right
Click on layout folder ➪ New ➪ Android XML file
(pop up box open see figure)

4. Now click add text in


second_activity.xml
➪ “This is second Activity”
linking activities
Code of second_activity.xml
linking activities
user interface design components
 Basic Layouts
 Input Controls
 Toast, Dialog and Snackbar
 Navigation, ActionBar and Menus
 Design Support Library
 Image View
 Digital Clock
 Analog Clock
 Text Clock
 SeekBar Example
 Calendar View
 Drop Down List
user interface design components
A typical user interface of an android application
consists of action bar and the application content area.
Main Action Bar
View Control
Content Area
Split Action Bar
Views and View Groups
View Class are the basic building block for user
interface components.
A View occupies a 2-dimensional area (say: rectangle)
on the screen, which is responsible for framing and
handling different type of events.
Views are used to create input and output fields in the
an Android App.
It can be input text field, radio field, image field etc.
They are same as, input text field, image tag to show
images, radio field in HTML.
View Group Class
The View-Group is the base class for layouts, which
holds other Views and define their properties.
Actually an application comprises combination and
nesting of Views-Group and Views Classes.
Basic View
Button: Button is pushed or pressed or clicked.
Every view should have a unique identifier which
identifies the element in the project. Make sure, two
elements should not share their unique ID. We
implement certain methods and interfaces which
listens to user input and act accordingly.
TextView: This view is basically meant for displaying
the text of application. For example the label of
application is displayed here.
EditText: This is a modification of Text View. It is
editable. User can give input dynamically.
Check Box: This widget is a two-state button. It can
be either checked or unchecked.
Radio Button: This is also a two-state button. It can
be checked or unchecked. Unlike checkbox if it is
checked once, cannot be unchecked. It can be checked
dynamically.
Radio Group: This houses radio buttons together.
Checking any one of the radio buttons in the group
unchecks rest of the buttons.
Image Button: This a button with image. User can
press or click this button. Image on the button is
referenced from the src folder of our project.
Picker View
Android provides controls for the user to pick a time or
pick a date as ready-to-use dialogs.
Each picker provides controls for selecting each part of
the time (hour, minute, AM/PM) or date (month, day,
year).
Using these pickers helps ensure that your users can
pick a time or date that is valid, formatted correctly,
and adjusted to the user's locale.
Picker View
We recommend that you use DialogFragment to host each
time or date picker.
The DialogFragment manages the dialog lifecycle for you
and allows you to display the pickers in different layout
configurations, such as in a basic dialog on handsets or as
an embedded part of the layout on large screens.
Although DialogFragment was first added to the platform
in Android 3.0 (API level 11), if your app supports
versions of Android older than 3.0—even as low as
Android 1.6—you can use the DialogFragment class that's
available in the supportlibrary for backward compatibility.
Adapter View
Adapter and Adapter View are so popular, that every
time you see any app with a List of items or Grid of
items, you can say for sure that it is using Adapter and
Adapter View.
Adapter View
Different Layouts
Types of layout
Linear Layout
Absolute Layout
Table Layout
Frame Layout
Relative Layout
Different Layouts
Linear Layout
Linear layout is further divided into horizontal and
vertical layout. It means it can arrange views in a single
column or in a single row. Here is the code of linear
layout(vertical) that includes a text view.
Different Layouts
AbsoluteLayout
The AbsoluteLayout enables you to specify the exact
location of its children. It can be declared like this.
Different Layouts
TableLayout
The TableLayout groups views into rows and columns. It
can be declared like this.
Different Layouts
RelativeLayout
The RelativeLayout enables you to specify how child
views are positioned relative to each other.It can be
declared like this.
Different Layouts
FrameLayout
The FrameLayout is a placeholder on screen that you can
use to display a single view. It can be declared like this.
App Widgets
App widgets can be thought of as a small window or
controller for an Android app that can be embedded in
another application (like the homescreen).
They can be very useful, allowing users to view or
control an app without actually launching it.
Lollipop Material design
Welcome to Android 5.0 Lollipop—the largest and
most ambitious release for Android yet!
This release is packed with new features for users and
thousands of new APIs for developers.
It extends Android even further, from phones, tablets,
and wearable, to TVs and cars.
Android 5.0 brings Material design to Android and
gives you an expanded UI toolkit for integrating the
new design patterns easily in your apps.
New 3D views let you set a z-level to raise elements
off of the view hierarchy and cast realtime shadows,
even as they move.
Lollipop Material design
A new system-managed processing thread
called RenderThread keeps animations smooth even when
there are delays in the main UI thread.
ART runtime, built from the ground up to support a mix of
ahead-of-time (AOT), just-in-time (JIT), and interpreted
code.
It’s supported on ARM, x86, and MIPS architectures and is
fully 64-bit compatible.
ART improves app performance and responsiveness.
Efficient garbage collection reduces the number and
duration of pauses for GC events, which fit comfortably
within the v-sync window so your app doesn’t skip frames.
Lollipop Material design
ART also dynamically moves memory to optimize
performance for foreground uses.
Notifications
Notifications in Android 5.0 are more visible, accessible,
and configurable.
Varying notification details may appear on the lock
screen if desired by the user. Users may elect to allow
none, some, or all notification content to be shown on a
secure lock screen.
RecyclerView
The RecyclerView class supports the display of a
collection of data.
It is a modernized version of the ListView and
the GridView classes provided by the Android
framework.
Recycler view addresses several issues that the
existing widgets have.
It enforced a programming style that results in good
performance.
It also comes with default animations for removing
and adding elements.
RecyclerView
RecyclerView allow to use different layout managers
for positioning items.
Recycler view uses a ViewHolder to store references to
the views for one entry in the recycler view.
A ViewHolder class is a static inner class in your
adapter which holds references to the relevant views.
With these references your code can avoid the time-
consuming findViewById() method to update the
widgets with new data.
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 and
the fragment's lifecycle is directly affected by the host
activity's lifecycle.
Fragment Life Cycle
Android fragments have their own life cycle very
similar to an android activity. This section briefs
different stages of its life cycle.
Fragment Life Cycle
nAttach() 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.
Fragment Life Cycle
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.
Fragment Life Cycle
At this point, view can be accessed with the
findViewById() method.
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().
Fragment Life Cycle
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.

You might also like