SlideShare a Scribd company logo
1 of 48
#AskMeAnything
Introduction to ConstraintLayout
Senior Android Developer
Omar Albelbaisy
Bio
Omar got his bachelor’s degree in Computer Science
from IUG university in 2014 and has been working as
an Android developer since that time.
He worked in many organizations and companies as
well as a freelancer developer.
He earned the Top Rated badge on Upwork freelancing
platform in 2017.
He also worked as an Android Trainer in many training
centers and colleges.
Omar is currently working as a Content Developer at
Coded and a Mentor at Udacity.
Introduction
© 2019 Udacity. All rights reserved.
❖ The activity is one of the main component of Android
applications.
❖ The Activity class takes care of creating a window for you
in which you can place your user interface.
Introduction
© 2019 Udacity. All rights reserved.
❖ Android user interface consist of hierarchy of Views and ViewGroups.
Introduction
© 2019 Udacity. All rights reserved.
❖ Basically, everything you see on screen is a view!
❖ View is the base class for
➢ Widgets, which are used to create interactive UI components like Buttons,
TextViews, ImageView etc.
➢ ViewGroups which are used to contain other Views and ViewGroups!
❖ Android layouts is a special type of ViewGroups which are used to
organize and specify how views aligned and structured on the screen.
Introduction
© 2019 Udacity. All rights reserved.
❖ Android provides several layouts that are different in how they align and
manage their children on the screen.
➢ e.x. FrameLayout, LinearLayout, RelativeLayout, and so on.
❖ In Feb, 2017 Android team released the first version of a new layout
under Support Library and they called it ConstraintLayout.
Introduction
© 2019 Udacity. All rights reserved.
❖ ConstraintLayout allows you to create complex layouts easily, avoiding
deep nested hierarchies and providing many powerful features for
designing UIs.
❖ ConstraintLayout uses a flexible constraints system to determine how to
align and structure the views on the screen.
Introduction
© 2019 Udacity. All rights reserved.
❖ It's 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 and easier to use with Android Studio.
❖ All the power of ConstraintLayout is available directly from the Layout
Editor's visual tools, So you can build your layout with ConstraintLayout
entirely by drag-and-dropping instead of editing the XML.
© 2019 Udacity. All rights reserved.
Introduction
Getting Started
© 2019 Udacity. All rights reserved.
❖ To use ConstraintLayout in your Android project you have to download
ConstraintLayout for Android and Solver for ConstraintLayout from the
SDK Manager.
❖ Then you need to add ConstraintLayout dependency to build.gradle in
the app module and sync gradle to download needed files.
❖ Next step is to convert your root view to ConstraintLayout which is the
default case in the newer versions of Android studio.
Let’s start .. Drag & Drop !
© 2019 Udacity. All rights reserved.
Relative Position
© 2019 Udacity. All rights reserved.
❖ Widgets can be aligned relative to parent or siblings like RelativeLayout!
❖ You can constrain a widget on the horizontal and vertical axis:
➢ Horizontal Axis: left, right, start and end sides
➢ Vertical Axis: top, bottom sides and text baseline
❖ The general concept is to constrain a given side of a widget to another
side of any other widget or parent (In the same Axis).
❖ Margins can be added between constrained widgets.
Relative Position
© 2019 Udacity. All rights reserved.
A A B
C
Relative Position
© 2019 Udacity. All rights reserved.
A A
B B
24
A B
Centering positioning and bias control
© 2019 Udacity. All rights reserved.
❖ A useful aspect of ConstraintLayout is in how it deals with "impossible"
constraints.
❖ Unless the View has the same exact size as the distance between two
opposite constraints, both constraints cannot be satisfied at the same
time (both sides cannot be where we want them to be).
❖ What happens in this case is that the constraints act like opposite forces
pulling the widget apart equally, such that the widget will end up being
centered.
Centering positioning and bias control
© 2019 Udacity. All rights reserved.
A
Centering positioning and bias control
© 2019 Udacity. All rights reserved.
❖ The default when encountering opposite constraints is to center the
widget; but you can tweak the positioning to favor one side over
another using the bias attributes.
Centering positioning and bias control
© 2019 Udacity. All rights reserved.
A
Circular positioning
© 2019 Udacity. All rights reserved.
❖ You can constrain a widget center relative to another widget center, at
an angle and a distance.
❖ This allows you to position a widget on a circle!
Circular positioning
© 2019 Udacity. All rights reserved.
Visibility behavior
© 2019 Udacity. All rights reserved.
❖ GONE widgets, as usual, are not going to be displayed and are not part
of the layout itself.
❖ But in terms of the layout computations, GONE widgets are still part of
it, with an important distinction:
➢ For the layout pass, their dimension will be considered as zero
(basically, they will be resolved to a point)
➢ If they have constraints to other widgets they will still be respected, but
any margins will be as if equals to zero
Visibility behavior
© 2019 Udacity. All rights reserved.
A B B
Dimensions constraints
© 2019 Udacity. All rights reserved.
❖ You can define minimum and maximum sizes for the ConstraintLayout
itself but those minimum and maximum dimensions will be used by
ConstraintLayout when its dimensions are set to WRAP_CONTENT.
❖ The dimension of the widgets can be specified by setting the
android:layout_width and android:layout_height attributes in 3 different
ways!
Dimensions constraints
© 2019 Udacity. All rights reserved.
➔ Using a specific dimension (either a literal value such as 123dp or a
dimension reference).
➔ Using WRAP_CONTENT, which will ask the widget to compute its own
size based on it’s content.
➔ Using 0dp, which is the equivalent of "MATCH_CONSTRAINT".
Dimensions constraints
© 2019 Udacity. All rights reserved.
A
A
AAAAAAAAA
Dimensions constraints
© 2019 Udacity. All rights reserved.
Important: MATCH_PARENT is not recommended for
widgets contained in a ConstraintLayout! Similar behavior
can be defined by using MATCH_CONSTRAINT with the
corresponding left/right or top/bottom constraints being
set to "parent".
Dimensions constraints
© 2019 Udacity. All rights reserved.
Important: If a dimension is set to WRAP_CONTENT you
have an option to keep enforcing constraints to limit the
resulting dimension by setting layout_constrainedWidth /
layout_constrainedHeight to true.
Dimensions constraints
© 2019 Udacity. All rights reserved.
Important: When a dimension is set to
MATCH_CONSTRAINT, the default behavior is to have the
resulting size take all the available space but you can
change this behavior by setting minimum/maximum size
for the dimension using. (e.g. layout_constraintWidth_min)
Percent dimension
© 2019 Udacity. All rights reserved.
❖ The dimension should be set to MATCH_CONSTRAINT (0dp)
❖ You can set dimension to be percent of the space between the two
opposite constraints.
❖ To use percent, you need to set the following:
➢ layout_constraintWidth_default="percent"
➢ layout_constraintWidth_percent="(value between 0 to 1)"
Aspect Ratio Support
© 2019 Udacity. All rights reserved.
❖ You can define one dimension of a widget as a ratio of the other one.
❖ In order to do that, you need to have at least one constrained
dimension be set to 0dp (i.e., MATCH_CONSTRAINT) and set the
attribute layout_constraintDimensionRatio to a given ratio.
❖ The ratio can be expressed either as a float value, representing a ratio
between width and height or a ratio in the form "width:height"
Aspect Ratio Support
© 2019 Udacity. All rights reserved.
❖ If both dimensions are set to MATCH_CONSTRAINT you can constrain
specific side based on the dimensions of another by pre append W," or
H, to constrain the width or height respectively.
Chains
© 2019 Udacity. All rights reserved.
❖ Chains provide group-like behavior in a single axis (horizontally or
vertically).
❖ The other axis will be constrained independently.
❖ A set of widgets are considered a chain if they are linked together via a
bi-directional connection.
Chains
© 2019 Udacity. All rights reserved.
A
B
A B
Chains
© 2019 Udacity. All rights reserved.
❖ Chains are controlled by attributes set on the first element of the chain
(the "head" of the chain).
❖ The head is the left-most widget for horizontal chains, and the top-most
widget for vertical chains.
A B C
Chain Head
Chains
© 2019 Udacity. All rights reserved.
❖ If margins are specified on connections, they will be taken in account. In
the case of spread chains, margins will be deducted from the allocated
space.
❖ The behavior of the chain will change according to the specified style
(default is CHAIN_SPREAD).
Chains
© 2019 Udacity. All rights reserved.
Important: In CHAIN_SPREAD mode, if some widgets are
set to MATCH_CONSTRAINT, they will split the available
space.
The attribute layout_constraintHorizontal_weight and
layout_constraintVertical_weight will control how the space
will be distributed among the elements using
MATCH_CONSTRAINT.
Chains
© 2019 Udacity. All rights reserved.
Important: In CHAIN_PACKED mode, the horizontal or
vertical bias attribute of the child will affect the
positioning of the packed elements.
Chains
© 2019 Udacity. All rights reserved.
Important: When using margins on elements in a chain,
the margins are additive.
An item plus its margins are considered together when
calculating, that means the margins will not be contained
by the leftover space.
Chains
© 2019 Udacity. All rights reserved.
Spread Chain A B C
A B CSpread Inside Chain
A B CWeighted Chain
A B CPacked Chain
A B CPacked With Bias
Virtual Helper objects - Guidelines
© 2019 Udacity. All rights reserved.
❖ In addition to the intrinsic capabilities detailed previously, you can also
use special helper objects in ConstraintLayout to help you with your
layout.
❖ Currently, the Guideline object allows you to create Horizontal and
Vertical guidelines which are positioned relative to the ConstraintLayout
container.
❖ Widgets can then be positioned by constraining them to such guidelines.
Virtual Helper objects - Guidelines
© 2019 Udacity. All rights reserved.
❖ You can position the guideline within the layout based on either dp units
or percent, relative to the layout's edge.
❖ Guideline will be invisible to app users!
Virtual Helper objects - Guidelines
© 2019 Udacity. All rights reserved.
A
%
33%
32
Virtual Helper objects - Barriers
© 2019 Udacity. All rights reserved.
❖ Similar to a guideline, a barrier is an invisible line that you can constrain
views to.
❖ Except a barrier does not define its own position; instead, the barrier
position moves based on the position of views contained within it.
❖ This is useful when you want to constrain a view to the a set of views
rather than to one specific view.
Virtual Helper objects - Barriers
© 2019 Udacity. All rights reserved.
C
A
B
C
A
B
Virtual Helper objects - Groups
© 2019 Udacity. All rights reserved.
❖ With groups, you can logically group together certain views allowing you
easily controls the visibility of them by only changing the visibility of the
group itself.
❖ Widgets are added to the group by adding a comma separated list of
their ids.
❖ A group in ConstraintLayout only contains references to the view ids
and not nesting the views inside a group.
❖ This is useful for things such as error screens or loading.
To recap ..
Let's build a real layout together !
© 2019 Udacity. All rights reserved.
Ask Me Anything.
Q&As
© 2019 Udacity. All rights reserved.
© 2017 Udacity. All rights reserved.
Be in Demand
© 2019 Udacity. All rights reserved.

More Related Content

What's hot

z/OS V2.4 Preview: z/OS Container Extensions - Running Linux on Z docker cont...
z/OS V2.4 Preview: z/OS Container Extensions - Running Linux on Z docker cont...z/OS V2.4 Preview: z/OS Container Extensions - Running Linux on Z docker cont...
z/OS V2.4 Preview: z/OS Container Extensions - Running Linux on Z docker cont...zOSCommserver
 
Jdbc architecture and driver types ppt
Jdbc architecture and driver types pptJdbc architecture and driver types ppt
Jdbc architecture and driver types pptkamal kotecha
 
Accessing Hardware on Android
Accessing Hardware on AndroidAccessing Hardware on Android
Accessing Hardware on AndroidGary Bisson
 
Native Android Userspace part of the Embedded Android Workshop at Linaro Conn...
Native Android Userspace part of the Embedded Android Workshop at Linaro Conn...Native Android Userspace part of the Embedded Android Workshop at Linaro Conn...
Native Android Userspace part of the Embedded Android Workshop at Linaro Conn...Opersys inc.
 
Project meeting: Android Graphics Architecture Overview
Project meeting: Android Graphics Architecture OverviewProject meeting: Android Graphics Architecture Overview
Project meeting: Android Graphics Architecture OverviewYu-Hsin Hung
 
GlusterFS CTDB Integration
GlusterFS CTDB IntegrationGlusterFS CTDB Integration
GlusterFS CTDB IntegrationEtsuji Nakai
 
Understanding das-nas-san
Understanding das-nas-sanUnderstanding das-nas-san
Understanding das-nas-sanAshwin Pawar
 
Data Storage In Android
Data Storage In Android Data Storage In Android
Data Storage In Android Aakash Ugale
 
Frontend 'vs' Backend Getting the Right Mix
Frontend 'vs' Backend   Getting the Right MixFrontend 'vs' Backend   Getting the Right Mix
Frontend 'vs' Backend Getting the Right MixBob Paulin
 
Java Servlets
Java ServletsJava Servlets
Java ServletsNitin Pai
 
Android ui dialog
Android ui dialogAndroid ui dialog
Android ui dialogKrazy Koder
 
Android Development: The Basics
Android Development: The BasicsAndroid Development: The Basics
Android Development: The BasicsMike Desjardins
 
Android Programming Basics
Android Programming BasicsAndroid Programming Basics
Android Programming BasicsEueung Mulyana
 
Android graphic system (SurfaceFlinger) : Design Pattern's perspective
Android graphic system (SurfaceFlinger) : Design Pattern's perspectiveAndroid graphic system (SurfaceFlinger) : Design Pattern's perspective
Android graphic system (SurfaceFlinger) : Design Pattern's perspectiveBin Chen
 
Introduction to WordPress for Beginners
Introduction to WordPress for BeginnersIntroduction to WordPress for Beginners
Introduction to WordPress for BeginnersR-Cubed Design Forge
 

What's hot (20)

z/OS V2.4 Preview: z/OS Container Extensions - Running Linux on Z docker cont...
z/OS V2.4 Preview: z/OS Container Extensions - Running Linux on Z docker cont...z/OS V2.4 Preview: z/OS Container Extensions - Running Linux on Z docker cont...
z/OS V2.4 Preview: z/OS Container Extensions - Running Linux on Z docker cont...
 
Jdbc architecture and driver types ppt
Jdbc architecture and driver types pptJdbc architecture and driver types ppt
Jdbc architecture and driver types ppt
 
Accessing Hardware on Android
Accessing Hardware on AndroidAccessing Hardware on Android
Accessing Hardware on Android
 
Native Android Userspace part of the Embedded Android Workshop at Linaro Conn...
Native Android Userspace part of the Embedded Android Workshop at Linaro Conn...Native Android Userspace part of the Embedded Android Workshop at Linaro Conn...
Native Android Userspace part of the Embedded Android Workshop at Linaro Conn...
 
Project meeting: Android Graphics Architecture Overview
Project meeting: Android Graphics Architecture OverviewProject meeting: Android Graphics Architecture Overview
Project meeting: Android Graphics Architecture Overview
 
GlusterFS CTDB Integration
GlusterFS CTDB IntegrationGlusterFS CTDB Integration
GlusterFS CTDB Integration
 
Low Level View of Android System Architecture
Low Level View of Android System ArchitectureLow Level View of Android System Architecture
Low Level View of Android System Architecture
 
Understanding das-nas-san
Understanding das-nas-sanUnderstanding das-nas-san
Understanding das-nas-san
 
Introduction to android testing
Introduction to android testingIntroduction to android testing
Introduction to android testing
 
Android studio installation
Android studio installationAndroid studio installation
Android studio installation
 
Data Storage In Android
Data Storage In Android Data Storage In Android
Data Storage In Android
 
Frontend 'vs' Backend Getting the Right Mix
Frontend 'vs' Backend   Getting the Right MixFrontend 'vs' Backend   Getting the Right Mix
Frontend 'vs' Backend Getting the Right Mix
 
Explore Android Internals
Explore Android InternalsExplore Android Internals
Explore Android Internals
 
Java Servlets
Java ServletsJava Servlets
Java Servlets
 
Android ui dialog
Android ui dialogAndroid ui dialog
Android ui dialog
 
Android Development: The Basics
Android Development: The BasicsAndroid Development: The Basics
Android Development: The Basics
 
Android Programming Basics
Android Programming BasicsAndroid Programming Basics
Android Programming Basics
 
Android graphic system (SurfaceFlinger) : Design Pattern's perspective
Android graphic system (SurfaceFlinger) : Design Pattern's perspectiveAndroid graphic system (SurfaceFlinger) : Design Pattern's perspective
Android graphic system (SurfaceFlinger) : Design Pattern's perspective
 
Introduction to WordPress for Beginners
Introduction to WordPress for BeginnersIntroduction to WordPress for Beginners
Introduction to WordPress for Beginners
 
Intro to kotlin
Intro to kotlinIntro to kotlin
Intro to kotlin
 

Similar to Introduction to ConstraintLayout

WMP_MP02_revd(10092023).pptx
WMP_MP02_revd(10092023).pptxWMP_MP02_revd(10092023).pptx
WMP_MP02_revd(10092023).pptxfahmi324663
 
Constraint layout - Cutting edge Android layout design
Constraint layout - Cutting edge Android layout designConstraint layout - Cutting edge Android layout design
Constraint layout - Cutting edge Android layout designFarhan Rahman Arnob
 
WMP_MP02_revd_03(10092023).pptx
WMP_MP02_revd_03(10092023).pptxWMP_MP02_revd_03(10092023).pptx
WMP_MP02_revd_03(10092023).pptxfahmi324663
 
Android Kotlin-Layout.pptx
Android Kotlin-Layout.pptxAndroid Kotlin-Layout.pptx
Android Kotlin-Layout.pptxDearKurniawanAA
 
Auto Layouts , NSLayoutConstraint, Traits in iOS
Auto Layouts , NSLayoutConstraint, Traits in iOSAuto Layouts , NSLayoutConstraint, Traits in iOS
Auto Layouts , NSLayoutConstraint, Traits in iOSLakhdeep Jawandha
 
Building interactive app
Building interactive appBuilding interactive app
Building interactive appOmar Albelbaisy
 
Designing responsive web design in FIGMA
Designing responsive web design in FIGMADesigning responsive web design in FIGMA
Designing responsive web design in FIGMAFibonalabs
 
Fluid lay out arcweb smac
Fluid lay out arcweb smacFluid lay out arcweb smac
Fluid lay out arcweb smacSmith Johnson
 
Responsive Web Design: Tips and Tricks
Responsive Web Design: Tips and TricksResponsive Web Design: Tips and Tricks
Responsive Web Design: Tips and TricksGautam Krishnan
 
Tk2323 lecture 2 ui
Tk2323 lecture 2   uiTk2323 lecture 2   ui
Tk2323 lecture 2 uiMengChun Lam
 
Autolayout primer
Autolayout primerAutolayout primer
Autolayout primerInferis
 
Margin vs Padding.pdf
Margin vs Padding.pdfMargin vs Padding.pdf
Margin vs Padding.pdfWebMaxy
 
W1_Lec01_Lec02_Layouts.pptx
W1_Lec01_Lec02_Layouts.pptxW1_Lec01_Lec02_Layouts.pptx
W1_Lec01_Lec02_Layouts.pptxssuserc1e786
 
Google Associate Android Developer Certification
Google Associate Android Developer CertificationGoogle Associate Android Developer Certification
Google Associate Android Developer CertificationMonir Zzaman
 
Mobile Application Development -Lecture 07 & 08.pdf
Mobile Application Development -Lecture 07 & 08.pdfMobile Application Development -Lecture 07 & 08.pdf
Mobile Application Development -Lecture 07 & 08.pdfAbdullahMunir32
 
Responsive Web Designs
Responsive Web DesignsResponsive Web Designs
Responsive Web DesignsNusrat Khanom
 
Responsive Web Design Has Become One Of The Hottest Trend
Responsive Web Design Has Become One Of The Hottest TrendResponsive Web Design Has Become One Of The Hottest Trend
Responsive Web Design Has Become One Of The Hottest TrendAditMicrosys Australia
 

Similar to Introduction to ConstraintLayout (20)

WMP_MP02_revd(10092023).pptx
WMP_MP02_revd(10092023).pptxWMP_MP02_revd(10092023).pptx
WMP_MP02_revd(10092023).pptx
 
Constraint layout - Cutting edge Android layout design
Constraint layout - Cutting edge Android layout designConstraint layout - Cutting edge Android layout design
Constraint layout - Cutting edge Android layout design
 
Android ppt.pptx
Android ppt.pptxAndroid ppt.pptx
Android ppt.pptx
 
WMP_MP02_revd_03(10092023).pptx
WMP_MP02_revd_03(10092023).pptxWMP_MP02_revd_03(10092023).pptx
WMP_MP02_revd_03(10092023).pptx
 
Android Kotlin-Layout.pptx
Android Kotlin-Layout.pptxAndroid Kotlin-Layout.pptx
Android Kotlin-Layout.pptx
 
Auto Layouts , NSLayoutConstraint, Traits in iOS
Auto Layouts , NSLayoutConstraint, Traits in iOSAuto Layouts , NSLayoutConstraint, Traits in iOS
Auto Layouts , NSLayoutConstraint, Traits in iOS
 
Building interactive app
Building interactive appBuilding interactive app
Building interactive app
 
Designing responsive web design in FIGMA
Designing responsive web design in FIGMADesigning responsive web design in FIGMA
Designing responsive web design in FIGMA
 
Fluid lay out arcweb smac
Fluid lay out arcweb smacFluid lay out arcweb smac
Fluid lay out arcweb smac
 
Responsive Web Design: Tips and Tricks
Responsive Web Design: Tips and TricksResponsive Web Design: Tips and Tricks
Responsive Web Design: Tips and Tricks
 
Tk2323 lecture 2 ui
Tk2323 lecture 2   uiTk2323 lecture 2   ui
Tk2323 lecture 2 ui
 
Tdf responsive design101_v1
Tdf responsive design101_v1Tdf responsive design101_v1
Tdf responsive design101_v1
 
Autolayout primer
Autolayout primerAutolayout primer
Autolayout primer
 
Margin vs Padding.pdf
Margin vs Padding.pdfMargin vs Padding.pdf
Margin vs Padding.pdf
 
W1_Lec01_Lec02_Layouts.pptx
W1_Lec01_Lec02_Layouts.pptxW1_Lec01_Lec02_Layouts.pptx
W1_Lec01_Lec02_Layouts.pptx
 
Adaptive Layout In iOS 8
Adaptive Layout In iOS 8Adaptive Layout In iOS 8
Adaptive Layout In iOS 8
 
Google Associate Android Developer Certification
Google Associate Android Developer CertificationGoogle Associate Android Developer Certification
Google Associate Android Developer Certification
 
Mobile Application Development -Lecture 07 & 08.pdf
Mobile Application Development -Lecture 07 & 08.pdfMobile Application Development -Lecture 07 & 08.pdf
Mobile Application Development -Lecture 07 & 08.pdf
 
Responsive Web Designs
Responsive Web DesignsResponsive Web Designs
Responsive Web Designs
 
Responsive Web Design Has Become One Of The Hottest Trend
Responsive Web Design Has Become One Of The Hottest TrendResponsive Web Design Has Become One Of The Hottest Trend
Responsive Web Design Has Become One Of The Hottest Trend
 

Introduction to ConstraintLayout

  • 2. Senior Android Developer Omar Albelbaisy Bio Omar got his bachelor’s degree in Computer Science from IUG university in 2014 and has been working as an Android developer since that time. He worked in many organizations and companies as well as a freelancer developer. He earned the Top Rated badge on Upwork freelancing platform in 2017. He also worked as an Android Trainer in many training centers and colleges. Omar is currently working as a Content Developer at Coded and a Mentor at Udacity.
  • 3. Introduction © 2019 Udacity. All rights reserved. ❖ The activity is one of the main component of Android applications. ❖ The Activity class takes care of creating a window for you in which you can place your user interface.
  • 4. Introduction © 2019 Udacity. All rights reserved. ❖ Android user interface consist of hierarchy of Views and ViewGroups.
  • 5. Introduction © 2019 Udacity. All rights reserved. ❖ Basically, everything you see on screen is a view! ❖ View is the base class for ➢ Widgets, which are used to create interactive UI components like Buttons, TextViews, ImageView etc. ➢ ViewGroups which are used to contain other Views and ViewGroups! ❖ Android layouts is a special type of ViewGroups which are used to organize and specify how views aligned and structured on the screen.
  • 6. Introduction © 2019 Udacity. All rights reserved. ❖ Android provides several layouts that are different in how they align and manage their children on the screen. ➢ e.x. FrameLayout, LinearLayout, RelativeLayout, and so on. ❖ In Feb, 2017 Android team released the first version of a new layout under Support Library and they called it ConstraintLayout.
  • 7. Introduction © 2019 Udacity. All rights reserved. ❖ ConstraintLayout allows you to create complex layouts easily, avoiding deep nested hierarchies and providing many powerful features for designing UIs. ❖ ConstraintLayout uses a flexible constraints system to determine how to align and structure the views on the screen.
  • 8. Introduction © 2019 Udacity. All rights reserved. ❖ It's 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 and easier to use with Android Studio. ❖ All the power of ConstraintLayout is available directly from the Layout Editor's visual tools, So you can build your layout with ConstraintLayout entirely by drag-and-dropping instead of editing the XML.
  • 9. © 2019 Udacity. All rights reserved. Introduction
  • 10. Getting Started © 2019 Udacity. All rights reserved. ❖ To use ConstraintLayout in your Android project you have to download ConstraintLayout for Android and Solver for ConstraintLayout from the SDK Manager. ❖ Then you need to add ConstraintLayout dependency to build.gradle in the app module and sync gradle to download needed files. ❖ Next step is to convert your root view to ConstraintLayout which is the default case in the newer versions of Android studio.
  • 11. Let’s start .. Drag & Drop ! © 2019 Udacity. All rights reserved.
  • 12. Relative Position © 2019 Udacity. All rights reserved. ❖ Widgets can be aligned relative to parent or siblings like RelativeLayout! ❖ You can constrain a widget on the horizontal and vertical axis: ➢ Horizontal Axis: left, right, start and end sides ➢ Vertical Axis: top, bottom sides and text baseline ❖ The general concept is to constrain a given side of a widget to another side of any other widget or parent (In the same Axis). ❖ Margins can be added between constrained widgets.
  • 13. Relative Position © 2019 Udacity. All rights reserved. A A B C
  • 14. Relative Position © 2019 Udacity. All rights reserved. A A B B 24 A B
  • 15. Centering positioning and bias control © 2019 Udacity. All rights reserved. ❖ A useful aspect of ConstraintLayout is in how it deals with "impossible" constraints. ❖ Unless the View has the same exact size as the distance between two opposite constraints, both constraints cannot be satisfied at the same time (both sides cannot be where we want them to be). ❖ What happens in this case is that the constraints act like opposite forces pulling the widget apart equally, such that the widget will end up being centered.
  • 16. Centering positioning and bias control © 2019 Udacity. All rights reserved. A
  • 17. Centering positioning and bias control © 2019 Udacity. All rights reserved. ❖ The default when encountering opposite constraints is to center the widget; but you can tweak the positioning to favor one side over another using the bias attributes.
  • 18. Centering positioning and bias control © 2019 Udacity. All rights reserved. A
  • 19. Circular positioning © 2019 Udacity. All rights reserved. ❖ You can constrain a widget center relative to another widget center, at an angle and a distance. ❖ This allows you to position a widget on a circle!
  • 20. Circular positioning © 2019 Udacity. All rights reserved.
  • 21. Visibility behavior © 2019 Udacity. All rights reserved. ❖ GONE widgets, as usual, are not going to be displayed and are not part of the layout itself. ❖ But in terms of the layout computations, GONE widgets are still part of it, with an important distinction: ➢ For the layout pass, their dimension will be considered as zero (basically, they will be resolved to a point) ➢ If they have constraints to other widgets they will still be respected, but any margins will be as if equals to zero
  • 22. Visibility behavior © 2019 Udacity. All rights reserved. A B B
  • 23. Dimensions constraints © 2019 Udacity. All rights reserved. ❖ You can define minimum and maximum sizes for the ConstraintLayout itself but those minimum and maximum dimensions will be used by ConstraintLayout when its dimensions are set to WRAP_CONTENT. ❖ The dimension of the widgets can be specified by setting the android:layout_width and android:layout_height attributes in 3 different ways!
  • 24. Dimensions constraints © 2019 Udacity. All rights reserved. ➔ Using a specific dimension (either a literal value such as 123dp or a dimension reference). ➔ Using WRAP_CONTENT, which will ask the widget to compute its own size based on it’s content. ➔ Using 0dp, which is the equivalent of "MATCH_CONSTRAINT".
  • 25. Dimensions constraints © 2019 Udacity. All rights reserved. A A AAAAAAAAA
  • 26. Dimensions constraints © 2019 Udacity. All rights reserved. Important: MATCH_PARENT is not recommended for widgets contained in a ConstraintLayout! Similar behavior can be defined by using MATCH_CONSTRAINT with the corresponding left/right or top/bottom constraints being set to "parent".
  • 27. Dimensions constraints © 2019 Udacity. All rights reserved. Important: If a dimension is set to WRAP_CONTENT you have an option to keep enforcing constraints to limit the resulting dimension by setting layout_constrainedWidth / layout_constrainedHeight to true.
  • 28. Dimensions constraints © 2019 Udacity. All rights reserved. Important: When a dimension is set to MATCH_CONSTRAINT, the default behavior is to have the resulting size take all the available space but you can change this behavior by setting minimum/maximum size for the dimension using. (e.g. layout_constraintWidth_min)
  • 29. Percent dimension © 2019 Udacity. All rights reserved. ❖ The dimension should be set to MATCH_CONSTRAINT (0dp) ❖ You can set dimension to be percent of the space between the two opposite constraints. ❖ To use percent, you need to set the following: ➢ layout_constraintWidth_default="percent" ➢ layout_constraintWidth_percent="(value between 0 to 1)"
  • 30. Aspect Ratio Support © 2019 Udacity. All rights reserved. ❖ You can define one dimension of a widget as a ratio of the other one. ❖ In order to do that, you need to have at least one constrained dimension be set to 0dp (i.e., MATCH_CONSTRAINT) and set the attribute layout_constraintDimensionRatio to a given ratio. ❖ The ratio can be expressed either as a float value, representing a ratio between width and height or a ratio in the form "width:height"
  • 31. Aspect Ratio Support © 2019 Udacity. All rights reserved. ❖ If both dimensions are set to MATCH_CONSTRAINT you can constrain specific side based on the dimensions of another by pre append W," or H, to constrain the width or height respectively.
  • 32. Chains © 2019 Udacity. All rights reserved. ❖ Chains provide group-like behavior in a single axis (horizontally or vertically). ❖ The other axis will be constrained independently. ❖ A set of widgets are considered a chain if they are linked together via a bi-directional connection.
  • 33. Chains © 2019 Udacity. All rights reserved. A B A B
  • 34. Chains © 2019 Udacity. All rights reserved. ❖ Chains are controlled by attributes set on the first element of the chain (the "head" of the chain). ❖ The head is the left-most widget for horizontal chains, and the top-most widget for vertical chains. A B C Chain Head
  • 35. Chains © 2019 Udacity. All rights reserved. ❖ If margins are specified on connections, they will be taken in account. In the case of spread chains, margins will be deducted from the allocated space. ❖ The behavior of the chain will change according to the specified style (default is CHAIN_SPREAD).
  • 36. Chains © 2019 Udacity. All rights reserved. Important: In CHAIN_SPREAD mode, if some widgets are set to MATCH_CONSTRAINT, they will split the available space. The attribute layout_constraintHorizontal_weight and layout_constraintVertical_weight will control how the space will be distributed among the elements using MATCH_CONSTRAINT.
  • 37. Chains © 2019 Udacity. All rights reserved. Important: In CHAIN_PACKED mode, the horizontal or vertical bias attribute of the child will affect the positioning of the packed elements.
  • 38. Chains © 2019 Udacity. All rights reserved. Important: When using margins on elements in a chain, the margins are additive. An item plus its margins are considered together when calculating, that means the margins will not be contained by the leftover space.
  • 39. Chains © 2019 Udacity. All rights reserved. Spread Chain A B C A B CSpread Inside Chain A B CWeighted Chain A B CPacked Chain A B CPacked With Bias
  • 40. Virtual Helper objects - Guidelines © 2019 Udacity. All rights reserved. ❖ In addition to the intrinsic capabilities detailed previously, you can also use special helper objects in ConstraintLayout to help you with your layout. ❖ Currently, the Guideline object allows you to create Horizontal and Vertical guidelines which are positioned relative to the ConstraintLayout container. ❖ Widgets can then be positioned by constraining them to such guidelines.
  • 41. Virtual Helper objects - Guidelines © 2019 Udacity. All rights reserved. ❖ You can position the guideline within the layout based on either dp units or percent, relative to the layout's edge. ❖ Guideline will be invisible to app users!
  • 42. Virtual Helper objects - Guidelines © 2019 Udacity. All rights reserved. A % 33% 32
  • 43. Virtual Helper objects - Barriers © 2019 Udacity. All rights reserved. ❖ Similar to a guideline, a barrier is an invisible line that you can constrain views to. ❖ Except a barrier does not define its own position; instead, the barrier position moves based on the position of views contained within it. ❖ This is useful when you want to constrain a view to the a set of views rather than to one specific view.
  • 44. Virtual Helper objects - Barriers © 2019 Udacity. All rights reserved. C A B C A B
  • 45. Virtual Helper objects - Groups © 2019 Udacity. All rights reserved. ❖ With groups, you can logically group together certain views allowing you easily controls the visibility of them by only changing the visibility of the group itself. ❖ Widgets are added to the group by adding a comma separated list of their ids. ❖ A group in ConstraintLayout only contains references to the view ids and not nesting the views inside a group. ❖ This is useful for things such as error screens or loading.
  • 46. To recap .. Let's build a real layout together ! © 2019 Udacity. All rights reserved.
  • 47. Ask Me Anything. Q&As © 2019 Udacity. All rights reserved.
  • 48. © 2017 Udacity. All rights reserved. Be in Demand © 2019 Udacity. All rights reserved.