4 Layouts&Widgets
4 Layouts&Widgets
---specify Layout
android.widget.TableLayout
android.widget.RelativeLayout
android.widget.FrameLayout
android.widget.ScrollLayout
other
GridView: displays items in two-dimensional,
scrollable grid
Absolute & Frame Layouts also
Interfaces: Two Alternatives
for creation: Code or XML
You have two ways you can create the
interface(s) of your Application.
1. Code = write code using SDK with
classes like LinearLayout, TextView, ……
Here is code
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
of
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Please Login"
android:id="@+id/textView_TopLabel" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
layout created
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="login"
android:id="@+id/textView_login" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText_Login" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="password"
android:id="@+id/textView_password" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="@+id/editText_Password" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button_Enter" />
</LinearLayout>
XML Interface tags
Besides drag and
drop you can edit
the xml file
directly….
Lets return to looking at
some of the possible
ViewGroup Layouts
Layouts determines how
the widgets will be placed
inFrameLayout
UI
LinearLayout
A Layout that arranges its children in a single column or a single
More commonly
used in Vertical
Orientation
LinearLayout
Good:
Simple
Know exactly how it will look on every
device
Bad:
too much simple look….
BUT see next slide
BUT, REMEMBER you can have a ViewGroup
(another Layout) inside as a member of the
LinearLayout to make a more COMPLEX
interface
Example
LinearLayout Very SIMPLE
Example
arranges by single column (vertical
orientation)
<Text View
android:layout_width=“fill_parent”
android:layout_height=“wrap_content”
VERY simple
android:text=“@string/hello”/> example – LinearLayout with one
child View object, a TextView saying Hello….
</LinearLayout>
LinearLayout Example 2
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent“
android:orientation="vertical" >
<Button android:id="@+id/btn_webbrowser"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Web Browser“
android:onClick="onClickWebBrowser" />
<Button android:id="@+id/btn_makecalls"
android:layout_width="fill_parent"
android:layout_height="wrap_content“
android:text="Make Calls"
android:onClick="onClickMakeCalls" />
<Button android:id="@+id/btn_showMap"
android:layout_width="fill_parent"
android:layout_height="wrap_content“
android:text="Show Map"
android:onClick="onClickShowMap" />
<Button android:id="@+id/btn_launchMyBrowser"
LinearLayout with 4 child View objects,
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Launch My Browser"all are buttons
android:onClick="onClickLaunchMyBrowser" />
Each View or ViewGroup can have its own
set of attributes…but, some are very
common
Attribute Description
Cell Frame
Boundary
Linear layout
(padding)
What about more complex
interfaces?
…..yes you can do this especially with
Nested Layouts
More Complexity Example
of Nested LinearLayouts
Here have First LinearLayout (vertical)
that contains ImageView and then
another LinearLayout (itself has 2 TextViews)
ImageView
2
TextViews
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
Another Nested LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
Example <LinearLayout
android:layout_height="match_parent" <TextView android:layout_width="match_p
arent"
android:layout_margin="10dp" android:layout_width="wrap_conten
t" android:layout_height="wrap_co
android:orientation="vertical"> ntent"
android:layout_height="wrap_conte
<TextView nt" android:layout_marginTop="10d
android:layout_width="wrap_content" p"
android:layout_marginTop="20dp"
android:layout_height="wrap_content" android:orientation="horizontal"
android:text="Using weightSum"
android:text="Without using weightSum" android:textColor="@android:color/ android:weightSum="1">
black"
android:textColor="@android:color/black" <Button
TableLayout
Table Layout
TableLayout positions its children into rows and
columns. TableLayout containers do not display
border lines for their rows, columns, or cells. The
table will have as many columns as the row with
the most cells. A table can leave cells empty, but
cells cannot span vertical, as they can in HTML.
TableRow: TableRow objects are the child
views of a TableLayout (each TableRow defines
a single row in the table). Each row has zero or
more cells, each of which is defined by any kind
of other View.
So, the cells of a row may be composed of a
variety of View objects, like
ImageView or TextView objects. A cell may also
TableRow will be automatically created
(if that row has not been existed) when
you drag Subview to the table cell.
GridLayout
A layout that places its children in a
rectangular grid.
GridLayout uses a grid of infinitely-thin
lines to separate its drawing area into:
rows, columns, and cells. It supports both
row and column spanning, this means it is
possible to merge adjacent cells into a
large cell (a rectangle) to contain a View.
Throughout the API, grid lines are
referenced by grid indices. A grid with N
columns has N + 1 grid indices that run
from 0 through N inclusive.
Sizes, Margins and Alignment/Gravity
In GridLayout, specifying sizes and margins is
done just as with a LinearLayout.
Alignment/gravity also works just like gravity in
LinearLayout and uses the same
constants: left, top, right, bottom,
center_horizontal, center_vertical, center,
fill_horizontal, fill_vertical and fill.
Flexibility: Unlike most grids in other
toolkits, Android GridLayout does not associate
data with rows or columns. Instead, everything to
do with alignment and flexibility is associated
with the components themselves.
The flexibility of columns is inferred from
With GridLayout, if there is no object
displaces grid column, the width of the
grid would be 0. Similarly, if row does
not be displaced by any objects, the
height of the grid is 0.
layout_columnWeight
In GridLayout, layout_columnWeight
attribute is weight by column of the
object in the cell, it may affect the
occupancy by column, the default value
is 0.
Most Important!
If you specify a value different than 0
for layout_columnWeight, you need to set
layout_gravity value for the object, this is
mandatory, if not specified
the layout_gravity value, object will not be
displayed on the grid:
The value for layout_gravity in this case is:
left
right
center_horizontal
center
fill_horizontal
layout_rowSpan
layout_rowWeight
In GridLayout, layout_rowWeight attri
bute is weight by row of the object in
the cell, it may affect the occupancy by
row, the default value is 0.
layout_columnSpan
GridLayout Vs TableLayout?
For the same UI, a GridLayout will
generally be faster and take less
memory than a TableLayout
ConstraintLayout
ConstraintLayout allows you to create
large and complex layouts with a flat
view hierarchy (no nested view groups).
It's similar to RelativeLayout in that all
views are laid out according to
relationships between sibling views and
the parent layout, but it's more flexible
than RelativeLayout and easier to use
with Android Studio's Layout Editor.
ConstraintLayout
To define a view's position in
ConstraintLayout, you must add at least
one horizontal and one vertical
constraint for the view.
Each constraint represents a connection
or alignment to another view, the parent
layout, or an invisible guideline.
Each constraint defines the view's
position along either the vertical or
horizontal axis; so each view must have
a minimum of one constraint for each
ConstraintLayout
When you drop a view into the Layout
Editor, it stays where you leave it even if
it has no constraints. However, this is
only to make editing easier; if a view has
no constraints when you run your layout
on a device, it is drawn at position [0,0]
(the top-left corner).
In figure 1, the layout looks good in the
editor, but there's no vertical constraint
on view C. When this layout draws on a
device, view C horizontally aligns with
the left and right edges of view A, but
ConstraintLayout
Figure-1 Figure-2
There is also Absolute
Layout
You position View widgets
exactly where you want them.
It is DEPRECATED
speed:
Measure: 0.977ms
Layout: 0.167ms
speed:
Measure: 0.598ms
Layout: 0.110ms
Draw: 2.717ms Draw: 2.146ms
RelativeLayout is FASTER
Coordinator Vs Constraint
The CoordinatorLayout is to manage the
complex behavior (especially
animations) of your activity's
components, and ConstraintLayout for
components proper placement
(especially list items).
Basic Views (Widgets)
UI Controls
Sr.N UI Control & Description
o.
1 TextView: This control is used to display text to the user.
2 EditText: EditText is a predefined subclass of TextView that includes rich editing
capabilities.
3 AutoCompleteTextView: The AutoCompleteTextView is a view that is similar to
EditText, except that it shows a list of completion suggestions automatically while
the user is typing.
4 Button: A push-button that can be pressed, or clicked, by the user to perform an
action.
5 ImageButton: An ImageButton shows a button with an image (instead of text) that
can be pressed or clicked by the user.
6 CheckBox: An on/off switch that can be toggled by the user. You should use check
box when presenting users with a group of selectable options that are not mutually
exclusive.
7 ToggleButton: An on/off button with a light indicator.
8 RadioButton: The RadioButton has two states: either checked or unchecked.
9 RadioGroup: A RadioGroup is used to group together one or more RadioButtons.
10 ProgressBar: The ProgressBar view provides visual feedback about some ongoing
tasks, such as when you are performing a task in the background.
11 Spinner: A drop-down list that allows users to select one value from a set.
Related Layout Tags
to create a DYNAMIC layout
contents –one where the contents
are dynamic (maybe read in from
database or???)
Subclasses of
AdapterView
SOME Examples of Layout Tags
that can load contents/data
dynamically
ListView Gallery GridView
Attributes
android:animationDuration setAnimationDuration(int) Sets
how long a transition animation should run (in
milliseconds) when layout has changed.
android:gravity setGravity(int) Specifies how to place the
content of an object, both on the x- and y-axis, within the
object itself.
android:spacing setSpacing(int)
Fragments
A Fragment is a piece of an activity
which enable more modular activity
design. It will not be wrong if we say, a
fragment is a kind of sub-activity.
You create fragments by
extending Fragment class and You can
insert a fragment into your activity
layout by declaring the fragment in the
activity's layout file, as
a <fragment> element.
Prior to fragment introduction, we had a
So we were not able to divide device
screen and control different parts
separately.
But with the introduction of fragment we
got more flexibility and removed the
limitation of having a single activity on
the screen at a time.
Now we can have a single activity but
each activity can comprise of multiple
fragments which will have their own
layout, events and complete life cycle.
A fragment has its own layout and its own
behavior with its own lifecycle callbacks.
You can add or remove fragments in an
activity while the activity is running.
You can combine multiple fragments in a
single activity to build a multi-pane UI.
A fragment can be used in multiple
activities.
Fragment life cycle is closely related to the
lifecycle of its host activity which means
when the activity is paused, all the
fragments available in the activity will also
be stopped.
A fragment can implement a behavior
that has no user interface component.
Fragments were added to the Android