Creating Android UI using Views Layouts Flashcards
Describe layouts
● A layout defines the structure for a user interface in an application.
● All elements in the layout are built using a hierarchy of View and ViewGroup objects.
Describe and illustrate views and view groups
A View usually draws something the user can see and interact with, whereas a ViewGroup is
an invisible container that defines the layout structure for View and other ViewGroup objects, all coming together to define a UI layout.
*See page 3 for illustration
Describe view objects and give examples
● View objects are referred to as widgets.
● Widgets are any UI components that enable a
user view or manipulate an activity’s screen
● They include as scroll bars, buttons, textviews, selection boxes, pull-down menus, etc.
Describe ViewGroup objects and give examples
● ViewGroup objects are referred to as layouts.
● Layouts include LinearLayout, GridLayout, RelativeLayout, etc.
Describe the two ways of declaring layouts
Layouts can be declared in two ways (which could be used independently or together to
build an app):
– Using Android’s XML to declare UI elements
● Through XML code on layout editors under the layout
subfolder
● Through a drag-and-drop interface in the layout editor
– Instantiation of layout elements at runtime
● Creating View and ViewGroup objects programmatically
Describe the hierarchy when declaring a layout using Android’s XML
● Each layout file must contain just one root element
(either a View or a ViewGroup object)
● Additional layout objects or widgets can then be added as
child elements to the root to build a View hierarchy that
defines a layout.
● Both the root element and the widgets have further children to describe various attributes to fit a certain layout requirement.
Describe the process of loading an XML resource and the involved function
● When an app is compiled, each XML layout is compiled
into a View resource.
● The layout resource is loaded from the app code under the
Activity.onCreate() callback.
● The setContentView() function, which takes the reference
to the layout resource is used to facilitate the loading.
fun onCreate
(savedInstanceState: Bundle) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main_layout)
}
Describe XML attributes
● View and ViewGroups support their own diversity of XML attributes
● Some attributes may be specific to given View objects, but there are some common attributes that are common to all from the root View class, eg, the id attribute.
Discuss the id attribute
id: This integer attribute identifies a View object
uniquely within a hierarchy/tree.
android:id=”@+id/my_button”
● The @ symbol indicates that the XML parser should parse and expand the rest of the id string and identify it as an ID
resource.
● The + means that the the component is a new resource name that must be created and added to our resources (R.java file).
● The other ID resources offered by the Android framework
should be referenced as follows to show reference of the ID
from the android.R resources and not the local R.java
resources:
android:id=”@android:id/empty”
Write a code snippet to create an instance of an element
under the onCreate() callback:
val myButton: Button = findViewById(R.id.my_button)
Describe layout parameters
● XML layout attributes define layout parameters for the View that are appropriate for the ViewGroup in which it resides.
● Every ViewGroup class implements a nested class that extends
ViewGroup.LayoutParams.
● This subclass contains property types that define the size and position for each child view, as appropriate for the view group.
● All view groups include a width and height (layout_width and layout_height), and each view is required to define them.
What is the recommended approach to specify height and width
The recommended approach includes using wrap_content (size as required by a given content), match_parent (as big as a a parent may allow) or dp
(density independent pixels) to ensure consistency across device screen sizes.
List view positions and dimensions
● A View has location (pair of left and top coordinates) expressed in pixels.
● A View also has two dimensions, expressed as a width and a height (expressed in pixels).
What methods can be used to retrieve view locations
– getLeft() - Returns the x coordinate of the views rectangle
– getTop() - Returns the Y coordinate of the view rectangle
– getRight() - Returns coordinates of the right edge
– getBottom() - Retruns coordinates of the bottom edge
What is the difference between measured width and height and actual width and height. Mention the functions associated with each
● The size of a view is expressed as a width and height
● Measured width and height define how big a view wants
to be with its parent, and can be obtained by calling
getMeasuredWidth() and getMeasuredHeight()
● Width and height define the actual view size on screen
and can be called by using getWidth() and getHeight()