Layout
Layout
Eg:
EditText, Button, CheckBox, etc. ViewGroup is an invisible container of
other views (child views) and other ViewGroup. Eg: LinearLayout is a
ViewGroup that can contain other views in it.
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.
• Linear Layout
• Relative Layout
• Constraint Layout
• Frame Layout
• Table Layout
gravity: Specifies how an object should position its content, on both the X
and Y axes, within its own bounds.
padding: Padding is the space inside the border, between the border and
the actual view’s content.
Activity_main.xml
<?xmlversion="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"
android:padding="30dp"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout>
</LinearLayout>
Relative Layout
This layout is for specifying the position of the elements in relation to the
other elements that are present there.
Activity_main.xml
<?xmlversion="1.0"encoding="utf-8"?>
<RelativeLayout
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"
android:padding="30dp">
<EditText
android:id="@+id/et1"
android:layout_width="170dp"
android:layout_height="wrap_content"
android:hint="FirstName"
android:layout_weight="1"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:importantForAutofill="no"
android:inputType="text"
tools:ignore="TextFields"/>
</RelativeLayout>
Constraint Layout 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 layered 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.
Constraints
Chains
Packed Chain
The final mode is which packed will pack the views together (although you
can provide margins to separate them slightly), and then centers the group
within the available space.
Spread Chain
The default mode is spread mode, and this will position the views in the
chain at even intervals within the available space.
The next mode is spread_inside which snaps the outermost views in the
chain to the outer edges and then positions the remaining views in the
chain at equal intervals within the available space.
Guidelines
A guideline is a visual guide which will not be seen at runtime that is used
align other views.
Creating a Guideline
Using guidelines
Dimensions
Sometimes we need to create views which have a fixed aspect ratio. This
is particularly useful with ImageView we need to display images which
come from a feed, and we know that any images we get will be a specific
aspect ratio.
This allows us to position a view along the vertical axis using a bias value,
this will be relative to it’s constrained position.
?xmlversion="1.0"encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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"
tools:context=".MainActivity"
android:padding="16dp">
<EditText
android:id="@+id/editTextTextPersonName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:hint="Firstname"
app:layout_constraintEnd_toStartOf="@+id/editTextTextPersonName2"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/editTextTextPersonName2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ems="10"
android:hint="LastName"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/editTextTextPersonName"
app:layout_constraintTop_toTopOf="parent" />
.
.
.
</androidx.constraintlayout.widget.ConstraintLayout>
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitXY"
app:srcCompat="@drawable/halong" />
</FrameLayout>
TableLayout:
TableLayout arranges the View(s) in table format.
Specifically, TableLayout is a ViewGroup containing one or
more TableRow(s). Each TableRow is a row in the table containing cells.
Child View(s) can be placed in one cell or in a merged cell from adjacent
cells of a row. Unlike tables in HTML, you cannot merge consecutive cells
in the one column.
Attributes:
1. android:stretchColumns
2. android:shrinkColumns
3. android:collapseColumns
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_span="4"
android:gravity="center"
android:text="Login"
android:textSize="22sp" />
.
.
.
</TableRow>
</TableLayout>