Ch 5: Building User Interfaces Flashcards
Building User Interfaces:
Important Topics
- Density-Independent UI Design
- Using Views and Layouts
- Optimizing Layouts
- Working with Lists and Grids
- Using Recycler View and Adapters
- Data Binding
- Extending, grouping, creating and using Views
Major Design Topics
that influence Material Design
User Interface (UI) design
User Experience (UX)
Human Computer Interaction (HCI)
*These are not covered in depth in this book
Density-Independent Design:
Basics
- The number of pixels available varies wildly depending on hardware
- This can be abstracted away by thinking in terms of Density-Independent Pixels (dp)
- these represent physical sizes
- UI elements with the same dp size will appear the same size on a screen regardless of the pixel density of the screen
- For font sizes, we use Scalable Pixels(sp)
- Shares same base density independence as dp
- also scaled independently based on user’s preferred text size
- Android 5.0(API 21) and up, supports Vector Drawables - scalable to support any display density
- Making designs density independent allows you to focus on optimizing and adapting designs for different screens
Density-Independent Design:
Fundamental Features
in Android that support it
- Density-Independent Pixels (dp)
- Scalable Pixels (sp)
- Support for device-independent Vector Drawables
Android User Interface:
Basic Components
-
Views
- Also referred to as “controls” or “widgets”
- Pretty much all visual components in Android
- Can draw their own customized UI
- Can handle user interactions
- Base ‘View’ class
-
View Groups
- Base ‘ViewGroup’ class
- An extension of View that supports adding child Views
- Responsible for sizing and positioning child Views
- View Groups that focus primarily on laying out child Views are called “layouts”
Android User Interface:
Assigning User Interface to an Activity
- To set the UI of an Activity, use setContentView()
- Pass in the View Instance or layout resource to display
- Typically done in the onCreate() handler
- Example:
- setContentView(R.layout.main);
Layouts:
Most Commonly Used Layouts
FrameLayout
LinearLayout
RelativeLayout
ConstraintLayout
Layouts:
FrameLayout
Summary
- Simplest of the Layout Managers
- Pins each child View within its frame
- Default position is the top-left corner
- Can use the “layout_gravity” attribute on a child View to alter its location
- Adding multiple children stacks each new child on top of the one before, with each new View potentially obscuring the previous ones
Layouts:
LinearLayout
Summary
- Aligns child Views in either a vertical or horizontal line - column, or row
- LinearLayout supports a “layout_weight” attribute for each child View that can control the relative size of each child View within the available space
Layouts:
RelativeLayout
Summary
- One of the most flexible of the native layouts
- But, potentially expensive to render
- Lets you define the positions of each child View relative to the others, and to the layout’s boundaries
Layouts:
ConstraintLayout
Summary
- Newest( and recommended) layout
- Designed to support large and complex layouts without the need to nest layouts
- Similar to the Relative Layout, but provides greater flexibility and is more efficient to lay out.
- Constraint Layout positions its children through a series of constraints
- requiring child Views to be positioned according to a boundary, other child Views, or to custom guidelines
- Has its own Visual Layout Editor, used to position each control and define the constraints rather than relying on manually editing XML.
- Available through the Constraint Layout package of the Android Support Library, making it backward compatible
Layouts:
How layouts are sized
- Each layout can scale to fill a screen, or whatever container it is in
- This is done by avoiding the use of absolute positions or predetermined pixel values
- This makes them well suited for designing UIs that work on a variety of devices
- Need to set the attributes:
- android:layout_width
- android:layout_height
- Dynamic values are:
- “match_parent”
- “wrap_content”
Defining Layouts:
XML Layout Basics
- Using XML external resources is preferred
- Must have a single root element, such as <linearlayout></linearlayout>
- The layout node has several attributes assigned
- The attributes modify the way in which all child nodes will be positioned
- There are many predefined constants for the attributes that can be used
- The root node can have an arbitrary number of child nodes, including other layouts
Layouts:
Constants:
match_parent
Used with width and height generally.
Expands the View to match the available space within the parent View, Fragment or Activity
Layouts:
Constants:
wrap_content
Generally used with width and height.
Sets the size of a View to the minimum required to contain the contents it displays,
such as the height required to display a wrapped text string.
Optimizing Layouts:
General Tips
-
Avoid redundant layouts
- Such as nesting Layout nodes
- Tip: for a set of child nodes defined in another file, use the <merge></merge> tag
-
Avoid using excessive Views
- Takes additional time and resources
- Tip: In a complex layout, use a ViewStub to stop some child views from being inflated until they are needed
-
Use the Lint tool to analyze your layouts
- It can detect optimization issues
Optimizing Layouts:
<merge></merge> tag
- Used when one layout file will be added to another
- Instead of using one of the Layouts for a root node, the file uses the <merge></merge> tag as the root node
- When this sublayout is added to another, the <merge></merge> tag is removed and its Child Views are added directly to the new parent
- The parent uses the <include></include> tag as it would with any other include
Optimizing Layouts:
<include></include> tag
- Used to insert the contents of one layout file into another
- The root node of the included file is added as a Child View (unless <merge></merge> tag is used)
- Need to assign an id to the new child view
- Use:
- <include></include>
Optimizing Layouts:
ViewStub
- Useful for reducing the number of Views inflated at a time
- Views in the stub not created until they are required
- Works like a lazy include-
- a stub represents a subset of child Views within the parent layout
- Only inflated when stub is inflated
- Only inflated explicitly via the “inflate” method or when it is made visible
Android Widget Toolbox:
Most Common Controls/Views
(12)
- TextView
- EditText
- ImageView
- VideoView
- Toolbar
- ProgressBar
- RecyclerView
- ViewPager
- Button
- ImageButton
- CheckBox
- RadioButton
Android Widget Toolbox:
Common Controls:
TextView:
Basic Function
A standard, read-only text label.
Supports multiline display, string formatting and automatic word-wrapping.
Android Widget Toolbox:
Common Controls:
EditText:
Basic Function
An editable text entry box.
Accepts multi-line entry, word-wrapping, and hint text.
Android Widget Toolbox:
Common Controls:
ImageView:
Basic Function
A View that shows a single image.
Android Widget Toolbox:
Common Controls:
Toolbar:
Basic Function
A View that shows a title and common actions.
Often used as the main app bar at the top of an Activity.
Android Widget Toolbox:
Common Controls:
ProgressBar:
Basic Function
A View that shows either an indeterminate progress indicator( a spinning circle),
or a horizontal progress bar.
Android Widget Toolbox:
Common Controls:
RecyclerView:
Basic Function
A View Group that manages displaying a large number of Views in a scrolling container.
Supports a number of layout managers that allow you to lay out Views as vertical and horizontal lists, or a grid.
Android Widget Toolbox:
Common Controls:
Button:
Basic Function
A standard, interactive push button.