SWE2008 ANDROID
PROGRAMMING
Module – 3
Android User Interface Design Essentials
Introduction
• The Android View
• The Android Controls
• The Android Layout
Displaying Text to Users with TextView
• One of the most basic user interface elements, or controls, in the
Android SDK is the TextView control
• TextView control is a child control within other screen elements and
controls
• with most of the user interface elements, it is derived from View and
is within the [Link] package
• To display this TextView on the screen, all your Activity needs to do
is call the set ContentView() method
• Change the text displayed programmatically by calling the setText()
method on the TextView object
Configuring Layout and Sizing
• Creating Contextual Links in Text
• Retrieving Data from Users with Text Fields
• Retrieving Text Input Using EditText Controls
• Constraining User Input with Input Filters
Designing with Layouts
• Creating User Interfaces in Android
Application user interfaces can be simple or complex, involving many
different screens
The term layout is used for two different but related purposes in
Android user interface design
In terms of resources, the /res/layout directory contains XML resource
definitions often called layout resource files
These XML files provide a template for how to draw controls on the
screen, layout resource files may contain any number of controls
The term is also used to refer to a set of ViewGroup classes, such as
LinearLayout, FrameLayout, TableLayout, RelativeLayout, and
GridLayout
Designing with Layouts
• Creating Layouts Using XML Resources
• Creating Layouts Programmatically
Partitioning the User Interface with Fragments
• In Android 3.0 (Honeycomb), the concept of a user interface
component called a Fragment was introduced
• Fragments decouple user interface behavior from a specific Activity
lifecycle
• Fragments were added to the Android SDK at a crucial time when
consumers were experiencing an explosion in the variety of Android
devices coming to market
Defining Fragments
• Fragment implementations that have been defined as regular classes
within your applica tion can be added to your layout resource files by
using the XML tag
• This tag has a few important attri butes. Specifically, you will need to
set the android:name attribute of the fragment to the fully qualified
Fragment class name
Managing Fragment Modifications
• when you have multiple Fragment components on a single screen,
within a single Activity, user interaction on one Fragment (such as our
news ListView Fragment) often causes the Activity to update another
Fragment
• An update or modification to a Fragment is performed using a
FragmentTransaction
• A Fragment can be attached or reattached to the parent Activity
• A Fragment can be hidden and unhidden from view
Attaching and Detaching Fragments with
Activities
• After you have a Fragment that you want to include within your
Activity class, the lifecycle of the Fragment comes into play
• callback methods are important to managing the lifecycle of a
Fragment, as it is created and then destroyed when it is no longer
used
Working with Special Types of Fragments
Designing with Layouts,” that there are a number of special Activity
classes for managing certain common types of user interfaces
List Fragment
Preference Fragment
WebViewFragment
DialogFragment
Designing Fragment-Based Applications
Displaying Dialogs
• One important technique developers can use is to implement dialogs
to inform the user or allow the user to perform actions such as edits
without redrawing the main screen
• There are two methods for incorporating dialogs into the application
• The legacy method
• Using the legacy method, which has existed since the first Android
SDK was re leased, an Activity class manages its dialogs in a dialog
pool
• Dialogs are created, initialized, updated, and destroyed using Activity
class callback methods
Using the Fragment-based method, which
was introduced in API Level 11
• The method recommended for developers moving forward
• Dialogs are managed using the Fragment Manager class
• A Dialog becomes a special type of Fragment that must still be used
within the scope of an Activity class, but its lifecycle is managed like
that of any other Fragment
Different Types of Dialogs
• Dialog
• AlertDialog
• CharacterPickerDialog
• DatePickerDialog
• ProgressDialog
• TimePickerDialog
• Presentation
Working with Dialogs and Dialog Fragments
• An Activity can use dialogs to organize information and react to user-
driven events. For example, an Activity might display a dialog
informing the user of a problem or asking the user to confirm an
action such as deleting a data record
• A DialogFragment is the best way to define and manage dialogs for
use within your user interface
Tracing the Lifecycle of a Dialog and Dialog
Fragment
• Each Dialog must be defined within the DialogFragment in which it is
used
• A DialogFragment class can be defined within an Activity or within a
Fragment. The type of Dialog you are creating will determine the
type of data that you must supply to the Dialog definition inside the
DialogFragment class