Lay Out

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 18

User Interface components

Layouts
• A layout defines the structure for a user interface in your
app, such as in an activity.
• All elements in the layout are built using a hierarchy of
View and ViewGroup objects.
• 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
• The View objects are usually called "widgets" and can be
one of many subclasses, such as Button or TextView.
• The ViewGroup objects are usually called "layouts" can
be one of many types that provide a different layout
structure, such as LinearLayout or ConstraintLayout .
Layouts
• Declare UI elements in XML. Android provides
a straightforward XML vocabulary that
corresponds to the View classes and
subclasses, such as those for widgets and
layouts. You can also use Android Studio's
Layout Editor to build your XML layout using a
drag-and-drop interface.
• Instantiate layout elements at runtime. Your
app can create View and ViewGroup objects
(and manipulate their properties)
programmatically.
Android Layout Attributes

• android:id : This is the ID which uniquely identifies the view


• android:layout_width : This is the width of the layout
• android:layout_height : This is the height of the layout
• android:layout_margin : This is the extra space outside of the
view. For example if you give android:marginLeft=20dp, then
the view will be arranged after 20dp from left
• android:layout_gravity : This specifies how child Views are
positioned
• android:layout_weight : This specifies how much of the extra
space in the layout should be allocated to the view
• android:layout_x : This specifies the x-coordinate of the layout
• android:layout_y : This specifies the y-coordinate of the layout
Layouts
• LinearLayout:
LinearLayout is a view
group that aligns all
children in a single
direction, vertically or
horizontally.
• You can specify the layout
direction with the
android:orientation
attribute.
Linear Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First"
android:id="@+id/button1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Second"
android:id="@+id/button2"/>
</LinearLayout>
Main Attributes In Linear Layout
• orientation: The orientation attribute used to set the
childs/views horizontally or vertically. In Linear layout
default orientation is vertical.
• gravity: The gravity attribute is an optional attribute
which is used to control the alignment of the layout like
left, right, center, top, bottom etc.
android:gravity="right“
• layout_weight: The layout weight attribute specify
each child control’s relative importance within the
parent linear layout. android:layout_weight="2“
Layouts
• RelativeLayout is a view
group that displays child
views in relative positions.
Relative Layout is a layout
which arranges
views/widgets/viewGroups
according to the position of
other
views/widgets/viewGroups
i.e the new views are
placed relative to the
already existing views.
Attributes of Relative layout:

• above: Position the bottom edge of the view above the


given anchor view ID and must be a reference of the
another resource in the form of id.
• alignLeft: alignLeft is used to make the left edge of the
view match the left edge of the given anchor view ID and
must be a reference to another resource
android:layout_alignLeft="@+id/textView“
• alignRight: alignRight property is used to make the right
edge of this view match the right edge of the given anchor
view ID and must be a reference to another resource
android:layout_alignRight="@+id/textView"
Layouts
• TableLayout :TableLayout is used
to display child View elements in
rows and columns.
<?xml version="1.0" encoding="utf-8"?>
<TextView
<TableLayout android:layout_width="wrap_content"
xmlns:android="http://schemas.android.co android:layout_height="wrap_content"
m/apk/res/android" android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent" android:text="Version Name" />
android:layout_marginTop="10dp"> <TextView
<TableRow android:layout_width="wrap_content"
android:background="#607D8B" android:layout_height="wrap_content"
android:padding="5dp"> android:layout_weight="1"
<TextView android:text="API Level" />
android:layout_width="wrap_content" </TableRow>
android:layout_height="wrap_content"
android:layout_weight="1" </TableLayout>
android:text="Version" />
Layouts Displays a scrolling grid of columns and rows.

• GridLayout : 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.
Layouts Displays a scrolling single column list.

• Android ListView is a
view which groups
several items and
display them in
vertical scrollable list.
The list items are
automatically inserted
to the list using an
Adapter that pulls
content from a source
such as an array or
database.
Layouts Displays web pages.

• Android’s WebView allows you to


integrate a webpage as a part of the
app.
• WebView comes with all the features
that of a desktop browser like
managing history, cookies, HTML5
support and lot more.
WebView webView = (WebView)
findViewById(R.id.webView);
webView.loadUrl("http://google.com");
Android View and ViewGroup
• The Android View class and ViewGroup class
are two very central classes in Android apps.
• An Android app contains one or more activities.
An Android activity is a screen, similar to
windows in a desktop application.
• Inside an activity you can have GUI
components. The GUI components are
instances of View or ViewGroup subclasses.
Android View and ViewGroup
• View
• The View class is a superclass for all
GUI components in Android. For
instance, the TextView class which
is used to display text labels in
Android apps is a subclass of View.
Android contains the following
commonly used View subclasses:
• TextView
• EditText
• ImageView
• ProgressBar
• Button
• ImageButton
• CheckBox
• DatePicker
Android View and ViewGroup
• ViewGroup
• The ViewGroup class is a
subclass of the View class.
ViewGroup instances work
as containers for View
instances to group View
instances together. Android
contains the following
commonly used
• ViewGroup subclasses:
•LinearLayout
•RelativeLayout
•ListView
•GridView
View
• View
• The View class is a superclass for all GUI components in Android. For
instance, the TextView class which is used to display text labels in Android
apps is a subclass of View. Android contains the following commonly used
View subclasses:
• TextView
• EditText
• ImageView
• ProgressBar
• Button
• ImageButton
• CheckBox
• DatePicker
• These are only some of the many, many subclasses of the View class.
ViewGroup

• ViewGroup
• The ViewGroup class is a subclass of the View class.
ViewGroup instances work as containers for View instances to
group View instances together. Android contains the
following commonly used ViewGroup subclasses:
• LinearLayout
• RelativeLayout
• ListView
• GridView

• These are not the only ViewGroup subclasses Android


contains. There are others but which are less used.

You might also like