Android Tips, Techniques and Theory

PART 1: Creating Content with XML with Android Studio (No programming required)

With Simple Recipes

Go Back To TOC


Views

As the name implies, any component that can be “viewed” is a View component. Hence, all UI components in an Activity are a subclass of the View class (android.view.View). As a result, all components attributes (e.g., padding, margin) are part of the View class as well. Only attributes that are SPECIFIC to a component will be members of that component’s class.
Views are the basic building blocks (b3) of any app. Some characteristics of views are:

All UI components can have one of three states:

Basic Views

Text Views

Button Views

ImageView

ImageView displays an image that can be loaded from various sources that allows for:

ImageView Scaling:

WebView

WebView allows you do embed a HTML page within an app. WebView displays a web page that is customizable from remote or local web pages. It can dynamically load URLs from Java code at runtime.  Also provides various types of callbacks:

Specialized Views

Advanced Views

ViewStub

ViewStub represents an invisible with no dimensions lightweight placeholder for less used components that can be inflated later on demand for better performance. It can be replaced with a specific View by assigning a layout attribute.
ViewGroup
A ViewGroup is a subclass of the View class and is a LAYOUT CONTAINER that has several subclasses. Some of these are:

Two views from the support design library that uses the basic templates.

ScrollView

ScrollView is a view that contains other views so that the content can be scrolled either horizontally or vertically. It is useful when large amount of content needs to be displayed within the view area.  It is best practice to embed a LinearLayout component of the same orientation in a ScrollView. Do not use a ListView and a ScrollView at the same time because they both perform scrolling.
ScrollView is a framelayout so it can only have one child—a View (e.g., TextView with a lot of text) or a View Group (e.g., Relative Layout).
It is common that the amount of text on a screen will exceed the viewport area of the screen especially on a smaller device like a phone so that the rest of the text will not be initially visible to a user. To see that rest of the text, a scroller is added so that the user can “scroll” the text up or down to see it.

EXAMPLE: Creating Scrollable Text

<resources>
<string name="app_name">Creating Scrollabe Text App</string>
<string name="gb_address">Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.
….
Abraham Lincoln
November 19, 1863
</string>

</resources>

ViewPage

ViewPage is a view that contains other views so that the content can be swipe left and right.

Dynamic (Adapter) Views

ListView

ListViews is the most used view that uses an adapter to display its content in a list. The ListView is used to display a “view” of “list” items as the name implies that may or may not be scrollable depending on the number of items in the view. It is typically populated using a generic Adapter. If the ListView is the only component, you can use the ListActivity class instead of the Activitty class. Content for list view can be retrieved from a variety of sources:

GridView

GridView uses an adapter to display its content in a grid (e.g., Photo Gallery). Like the ListView, the GridView is a ViewGroup that displays items in a 2-dimensional grid instead of a list that can be scrolled. However, there are some additional attributes that can be added:

Spinner

Spinners is a view that uses an adapter to display its content in a drop down menu that allows for a single selection.

ExpandableListView

ExpandableListView—provides a two level list view

RecyclerView

RecyclerView—

CardView

CardView—