Lecture 5
Lecture 5
Proliferate Gratification!
1. Introduction to ConstraintLayout
1.1. Overview
- ConstraintLayout is a powerful, flexible layout in Android that allows you to create complex
and responsive UI layouts with a flat view hierarchy.
- Introduced in Android Studio 2.2, it aims to simplify the design process and reduce nested
view hierarchies.
1.2. Advantages
- Flexibility: Can create a variety of layouts, from simple to complex.
- Performance: Reduces the number of nested views, which can improve performance.
- Ease of Use: Integrated with the Layout Editor in Android Studio, providing a visual design
interface.
---
2.1. Constraints
- Constraints define how views are positioned relative to other views or the parent layout.
- Each view must have at least two constraints (usually one horizontal and one vertical).
2.2. Anchors
- Constraint anchors are used to define constraints on a view.
- Anchors can be left, right, top, bottom, baseline, center, etc.
2.3. Attributes
WhatsApp: +91 7208106107 Gandharva, Shree Nagar, Thane [email protected]
Gera’s Imperium Alpha, Kharadi, Pune [email protected]
GODJN Solutions Pvt. Ltd.
Proliferate Gratification!
- `app:layout_constraintLeft_toLeftOf`
- `app:layout_constraintRight_toRightOf`
- `app:layout_constraintTop_toTopOf`
- `app:layout_constraintBottom_toBottomOf`
2.4. Example
<?xml version="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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OM"
android:textColor="#FF0000"
android:textSize="40dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
3.1. Chain
- Chains are a way to position multiple views in a linear manner, either vertically or horizontally.
- Types of chains: Spread, Spread Inside, and Packed.
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@+id/button2"
app:layout_constraintHorizontal_chainStyle="spread"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button2"
android:text="Button 2"
app:layout_constraintStart_toEndOf="@+id/button1"
app:layout_constraintEnd_toStartOf="@+id/button3"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button3"
android:text="Button 3"
app:layout_constraintStart_toEndOf="@+id/button2"
app:layout_constraintEnd_toEndOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
3.2. Guideline
- Guidelines help align views with a fixed or percentage-based position.
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.4" />
<TextView
WhatsApp: +91 7208106107 Gandharva, Shree Nagar, Thane [email protected]
Gera’s Imperium Alpha, Kharadi, Pune [email protected]
GODJN Solutions Pvt. Ltd.
Proliferate Gratification!
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Below Guideine"
app:layout_constraintTop_toBottomOf="@+id/guideline"
app:layout_constraintLeft_toLeftOf="parent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Above Guideine"
app:layout_constraintBottom_toTopOf="@+id/guideline"
app:layout_constraintLeft_toLeftOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
3.3. Barrier
- Barriers are dynamic guidelines that adjust their position based on the size and position of
views.
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text View 1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView2"
android:text="Text View 2"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toEndOf="@+id/textView1" />
<androidx.constraintlayout.widget.Barrier
android:id="@+id/barrier"
app:barrierDirection="end"
app:constraint_referenced_ids="textView1, textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:text="Button"
app:layout_constraintTop_toBottomOf="@id/textView1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="@id/barrier"/>
</androidx.constraintlayout.widget.ConstraintLayout>
4. Hands-on Exercises
<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"
WhatsApp: +91 7208106107 Gandharva, Shree Nagar, Thane [email protected]
Gera’s Imperium Alpha, Kharadi, Pune [email protected]
GODJN Solutions Pvt. Ltd.
Proliferate Gratification!
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text"
android:text="Username"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:ems="10"
android:inputType="text"
android:text="Password"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/username" />
<Button
android:id="@+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/password" />
</androidx.constraintlayout.widget.ConstraintLayout>
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
<ImageView
android:id="@+id/logo"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/logo1"
android:contentDescription="logo"
android:maxWidth="50dp"
android:minHeight="50dp"/>
</androidx.appcompat.widget.Toolbar>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@drawable/lion"
android:background="@drawable/way"
android:padding="2dp"
android:scaleType="centerCrop"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="User"
android:textSize="12sp"/>
</LinearLayout>
</LinearLayout>
WhatsApp: +91 7208106107 Gandharva, Shree Nagar, Thane [email protected]
Gera’s Imperium Alpha, Kharadi, Pune [email protected]
GODJN Solutions Pvt. Ltd.
Proliferate Gratification!
</HorizontalScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/om1"
android:scaleType="centerCrop"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginStart="8dp"
android:text="username"
android:textStyle="bold"/>
</LinearLayout>
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/logo1"
android:layout_marginEnd="16dp"/>
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/lion"
android:layout_marginEnd="16dp"/>
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/way"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
Summary
By the end of Day 5, students should have a solid understanding of ConstraintLayout and its
advanced features. They should be able to create complex layouts efficiently using constraints,
chains, guidelines, and barriers, enhancing the UI and performance of their Android
applications.
---
Homework
1. Create a Custom Layout: Design a layout for a profile screen using ConstraintLayout.
2. Experiment: Modify the constraints, chains, and guidelines to see how they affect the layout.
WhatsApp: +91 7208106107 Gandharva, Shree Nagar, Thane [email protected]
Gera’s Imperium Alpha, Kharadi, Pune [email protected]
GODJN Solutions Pvt. Ltd.
Proliferate Gratification!