Unit 3
Unit 3
<!-- Include other widget or layout tags here. These are considered "child views" or "children" of
the linear layout -->
</LinearLayout>
Relative Layout
RelativeLayout is a view group that displays child views in relative positions.
The position of each view can be specified as relative to sibling elements or in
positions relative to the parent RelativeLayout area.
As it allows us to position the component anywhere, it is considered as most
flexible layout.
Relative layout is the most used layout after the Linear Layout in Android.
In Relative Layout, you can use “above, below, left and right” to arrange the
component’s position in relation to other component.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp" >
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/reminder" />
<Spinner
android:id="@+id/dates"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="@id/name"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/times" />
<Spinner
android:id="@id/times"
android:layout_width="96dp"
android:layout_height="wrap_content"
android:layout_below="@id/name"
android:layout_alignParentRight="true" />
<Button
android:layout_width="96dp"
android:layout_height="wrap_content"
android:layout_below="@id/times"
android:layout_alignParentRight="true"
android:text="@string/done" />
TableLayout
TableLayout is a ViewGroup that displays child View elements in rows and columns.
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.
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 be a ViewGroup object (for example, you can nest another
TableLayout as a cell).
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns=“1”>
<TableRow>
<TextView
android:text="@string/table_layout_4_open"
android:padding="3dip" />
<TextView
android:text="@string/table_layout_4_open_shortcut"
android:gravity="right"
android:padding="3dip" />
</TableRow>
<TableRow>
<TextView
android:text="@string/table_layout_4_save"
android:padding="3dip" />
<TextView
android:text="@string/table_layout_4_save_shortcut"
android:gravity="right"
android:padding="3dip" />
</TableRow>
</TableLayout>
GridLayout
A layout that places its children in a rectangular grid.
The grid is composed of a set of infinitely thin lines that separate the viewing
area into cells.
Children occupy one or more contiguous cells, as defined by their rowSpec
and columnSpec layout parameters.
Attributes Description
android:columnCount The maximum number of columns to create when automatically positioning children.
android:columnOrderPreserved When set to true, forces column boundaries to appear in the same order as column
indices.
android:orientation The orientation property is not used during layout.
android:rowCount The maximum number of rows to create when automatically positioning children.
android:rowOrderPreserved When set to true, forces row boundaries to appear in the same order as row indices.
<?xml version="1.0" encoding="utf-8"?> <EditText android:ems="10" />
<TextView
<GridLayout android:text="Password:"
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_column="0"
android:layout_width="match_parent"
android:layout_gravity="right" />
android:layout_height="match_parent" <EditText android:ems="8" />
android:useDefaultMargins="true" <Space android:layout_row="4"
android:alignmentMode="alignBounds" android:layout_column="0"
android:columnOrderPreserved="false" android:layout_columnSpan="3"
android:layout_gravity="fill" />
android:columnCount="4" >
<Button android:text="Next"
android:layout_row="5"
<TextView android:text="Email setup“ android:layout_column="3" />
android:textSize="32dip" </GridLayout>
android:layout_columnSpan="4"
android:layout_gravity="center_horizontal" />
<TextView android:text="You can configure email in just a few
steps:“
android:textSize="16dip"
android:layout_columnSpan="4"
android:layout_gravity="left" />
<TextView android:text="Email address:“
android:layout_gravity="right" />
Frame Layout
Frame Layout is designed to block out an area on the screen to
display a single item.
It is used to specify the position of Views.
Generally, FrameLayout should be used to hold a single child
view.
It contains Views on the top of each other to display only
single View inside the FrameLayout.
You can, add multiple children to a FrameLayout and control
their position by using gravity attribute.
In this the child views are added in a stack and the most recently
added child will show on the top.
The size of the FrameLayout is the size of its largest child (plus
padding), visible or not (if the FrameLayout's parent permits).
FrameLayout
ConstraintLayout
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.
You can build your layout with ConstraintLayout entirely by dragging instead of
editing the XML.
To define a view's position in ConstraintLayout, you 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 the vertical or horizontal axis.
Each view must have a minimum of one constraint for each axis, but often more
are necessary.
TabLayout
TabLayout is used to implement horizontal tabs.
Population of the tabs to display is done through TabLayout.Tab instances.
Create tabs via newTab() From there we can change the tab's label or icon via
TabLayout.Tab.setText(int).
A TabLayout can be setup with a ViewPager
Dynamically create TabItems based on the number of pages, their titles, etc.
Synchronize the selected tab and tab indicator position with page swipes.
There are two types of tabs: Fixed tabs, Scrollable tabs.
1) Fixed tabs display all tabs on one screen, with each tab at a fixed width.
2) Scrollable tabs are displayed without fixed widths. They are scrollable, such
that some tabs will remain off-screen until scrolled.
Android UI Controls
There are number of Ul controls provided by Android that allow you to build
the graphical user interface for your app.
UI Control Description
TextView This control is used to display text to the user.
EditText EditText is a predefined subclass of TextView that includes rich editing capabilities.
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.
Button A push-button that can be pressed, or clicked, by the user to perform an action.
ImageButton An ImageButton is an AbsoluteLayout which enables you to specify the exact location
of its children. This shows a button with an image (instead of text) that can be pressed
or clicked by the user.
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.
UI Control Description
ProgressBar The ProgressBar view provides visual feedback about some ongoing tasks, such as
when you are performing a task in the background.
Spinner A drop-down list that allows users to select one value from a set.
TimePicker The TimePicker view enables users to select a time of the day, in either 24-hour mode
or AM/PM mode.
DatePicker The DatePicker view enables users to select a date of the day.
TextView
TextView is a UI Component that displays the text to the user on their Display Screen .
Attribution Description
android:id This is the ID which uniquely identifies the control.
android:fontFamily Font family (named by string) for the text.
android:gravity Specifies how to align the text when the text is smaller than the view.
android:maxHeight Makes the TextView be at most this many pixels tall.
android:maxWidth Makes the TextView be at most this many pixels wide.
android:text Text to display in view.
android:textAllCaps Make the text in Capital. Possible value either "true" or "false".
android:textColor Text color. May be a color value, in the form of "#rgb", "#argb", "#rrggbb", or "#aarrggbb".
android:textStyle Style (bold, italic, bolditalic) for the text. You can use or more of the values separated by '|'. normal
– 0, bold – 1, italic - 2
android:typeface Typeface (normal, sans, serif, monospace) for the text. You can use or more of the values separated
by '|'. normal – 0, sans – 1, serif – 2, monospace – 3.
EditText
A EditText is an overlay over TextView that configures itself to be editable.
It is the predefined subclass of TextView that includes rich editing capabilities.
Attribute Description
android:autoText TextView has a textual input method and automatically corrects some common spelling errors.
android:drawableBottom This is the drawable to be drawn below the text.
android:completionHint This defines the hint displayed in the drop down menu
android:completionHintView This defines the hint view displayed in the drop down menu
android:completion Threshold This defines the number of characters that the user must type before
completion suggestions are displayed in a drop down menu.
Attribute Description
android:onClick This is the name of the method in this View's context to invoke when the view is clicked.
ImageButton
Displays a button with an image (instead of text) that can be pressed or clicked
by the user.
By default, an ImageButton looks like a regular Button, with the standard
button background that changes color during different button states.
ToggleButton
A ToggleButton displays checked/unchecked states as a button. It is basically
an on/off button with a light indicator.
Attributes Description
android:disabledAlpha The alpha to apply to the indicator when
disabled.
setContentView(R.layout.content_layout_id);
<ProgressBar
android:id="@+id/determinateBar" style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:progress="25"/>
You can update the percentage of progress displayed by using the setProgress(int) method, or by
calling incrementProgressBy(int) to increase the current progress completed by a specified
amount.
Spinner
A view that displays one child at a time and lets the user pick among them.
Android Spinner is a view similar to the dropdown list which is used to
select one option from the list of options.
It provides an easy way to select one item from the list of items and it
shows a dropdown list of all values when we click on it.
The default value of the android spinner will be the currently selected value.
By using Adapter we can easily bind the items to the spinner objects.
You can add a spinner to your layout with the Spinner object.
<Spinner
android:id="@+id/planets_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
String[] planets_array = {“Mercury”, “Venus”, “Earth”, “Mars”, “Jupiter”, “Saturn”,
“Uranus”, “Neptune”};
protected void onCreate(Bundle savedInstanceState) simple_spinner_item is the
default layout for the spinner's
{ appearance
Spinner spinner = (Spinner) findViewById(R.id.planets_spinner);
spinner.setOnItemSelectedListener(this);
ArrayAdapter adapter = new ArrayAdapter( this, android.R.layout.simple_spinner_item, planets_array);
//to specify the layout the adapter uses to display the list of spinner choices.
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ListView l;
String[] planets_array = {“Mercury”, “Venus”, “Earth”, “Mars”, “Jupiter”, “Saturn”, “Uranus”, “Neptune”};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
l = findViewById(R.id.list_view);
ArrayAdapter<String> arr;
arr = new ArrayAdapter<String>( this, R.layout.support_simple_spinner_dropdown_item, planets_array);
l.setAdapter(arr);
}
Dialog
A dialog is a small window that prompts the user to make a decision or enter
additional information.
A dialog doesn't fill the screen and is normally used for modal events that
require users to take an action before they can proceed.
The Dialog class is the base class for dialogs, but don't
instantiate Dialog directly.
Instead, use one of the following subclasses:
AlertDialog: A dialog that can show a title, up to three buttons,
a list of selectable items, or a custom layout.
DatePickerDialog or TimePickerDialog: A dialog with a predefined