1. Android Layout is used to define the user interface that holds the UI controls or
widgets that will appear on the screen of an android application or activity screen.
Layouts in Android are used to define the way we want a screen to be shown.
The Layout is the master plan or a blueprint of the printed/published work that
lays out the order of its various graphic elements.
Generally, every application is a combination of View and ViewGroup.
As we know, an android application contains a large number of activities and we
can say each activity is one page of the application.
So, each activity contains multiple user interface components and those
components are the instances of the View and ViewGroup.
All the elements in a layout are built using a hierarchy of View and ViewGroup
objects.
2. Views
A `View` is the fundamental building block of any Android UI. It represents
interactive elements like buttons, text fields, and images, serving as the user
interface’s backbone.
ViewGroups
On the other hand, a `ViewGroup` is a specialized `View` that acts as a container
for other views or view groups. It plays a pivotal role in organizing and arranging
the layout of its child views.
3. Types of Android Layout
Android Linear Layout: LinearLayout is a ViewGroup subclass, used to provide child
View elements one by one either in a particular direction either horizontally or
vertically based on the orientation property.
Android Relative Layout: RelativeLayout is a ViewGroup subclass, used to specify
the position of child View elements relative to each other like (A to the right of B) or
relative to the parent (fix to the top of the parent).
Android Constraint Layout: ConstraintLayout is a ViewGroup subclass, used to
specify the position of layout constraints for every child View relative to other views
present. A ConstraintLayout is similar to a RelativeLayout, but having more power.
Android Frame Layout: FrameLayout is a ViewGroup subclass, used to specify the
position of View elements it contains on the top of each other to display only a single
View inside the FrameLayout.
4. Android Table Layout: TableLayout is a ViewGroup subclass, used to display
the child View elements in rows and columns.
Android Web View: WebView is a browser that is used to display the web pages
in our activity layout.
Android ListView: ListView is a ViewGroup, used to display scrollable lists of
items in a single column.
Android Grid View: GridView is a ViewGroup that is used to display a scrollable
list of items in a grid view of rows and columns.
5. Different Attribute of the Layouts
XML attributes Description
android:id Used to specify the id of the view.
android:layout_width
Used to declare the width of View and ViewGroup elements
in the layout.
android:layout_height
Used to declare the height of View and ViewGroup elements
in the layout.
android:layout_marginL
eft
Used to declare the extra space used on the left side of View
and ViewGroup elements.
android:layout_marginR
ight
Used to declare the extra space used on the right side of View
and ViewGroup elements.
android:layout_marginT
op
Used to declare the extra space used in the top side of View
and ViewGroup elements.
android:layout_marginB
ottom
Used to declare the extra space used in the bottom side of
View and ViewGroup elements.
android:layout_gravity Used to define how child Views are positioned in the layout.
6. LinearLayout is the most basic layout in android studio, that aligns all the children
sequentially either in a horizontal manner or a vertical manner by specifying
the android:orientation attribute. If one applies android:orientation=”vertical” then
elements will be arranged one after another in a vertical manner and If you
apply android:orientation=”horizontal” then elements will be arranged one after
another in a horizontal manner.
<?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="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
</LinearLayout>
7. LinearLayout- A LinearLayout aligns each of the child View in either a vertical or a
horizontal line. A vertical layout has a column of Views, whereas in a horizontal layout
there is a row of Views. It supports a weight attribute for each child View that can
control the relative size of each child View within the available space.
8. Attributes Description
android:layout_weight
It is defined individually to the child’s views to specify how
LinearLayout
divides the remaining space amongst the views it contains
android:weightSum Defines the total weight sum
android:orientation
How the elements should be arranged in the layout. It can be
horizontal or vertical.
android:gravity
It specifies how an object should position its content on its
X and Y axes.
Possible values are – center_vertical, fill, center, bottom,
end, etc.
android:layout_gravity
Sets the gravity of the View or Layout relative to its parent.
Possible values are – center_vertical, fill, center, bottom,
end, etc.
android:baselineAligned
This must be a boolean value, either “true” or “false” and
prevents the layout
from aligning its children’s baselines.
android:id This gives a unique id to the layout.
9. How to arrange children views in a vertical manner
<?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="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<!-- Add vertical in the android:orientation-->
<!-- Add Button-->
<Button
android:layout_width="match_parent"
android:layout_margin="10dp"
android:layout_height="wrap_content"/>
11. How to arrange children views in a horizontal manner
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".MainActivity">
<!-- Add horizontal in the android:orientation-->
<!-- Add Button-->
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="10dp" />
13. How to use layout_weight and weightSum
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="3"
tools:context=".MainActivity">
<!-- Add value in the android:weightSum-->
<!-- Add horizontal in the android:orientation-->
<!-- Add Button-->
<!-- Add value in the android:layout_weight-->
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:layout_weight="1" />
14. <!-- Add Button-->
<!-- Add value in the android:layout_weight-->
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:layout_weight="1" />
<!-- Add Button-->
<!-- Add value in the android:layout_weight-->
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:layout_weight="1" />
</LinearLayout>
15. 4. How to use gravity
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="3"
tools:context=".MainActivity">
<!-- Add value in the android:weightSum-->
<!-- Add horizontal in the android:orientation-->
<!-- Add Button-->
<!-- Add value in the android:gravity -->
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="10dp"
16. android:layout_weight="1"
android:gravity="bottom|center"
android:text="GFG" />
<!-- Add Button-->
<!-- Add value in the android:gravity -->
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:layout_weight="1"
android:gravity="center"
android:text="GFG" />
<!-- Add Button-->
<!-- Add value in the android:gravity -->
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:layout_weight="1"
android:gravity="center|top"
android:text="GFG" />
</LinearLayout>
17. How to use layout_gravity
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<!-- Add vertical in the android:orientation-->
<!-- Add Button-->
<!-- Add value in the layout_gravity -->