Slide 1

Slide 1 text

ConstraintLayout Nicolas Roard

Slide 2

Slide 2 text

What is it • Expressive Layout Manager • Flat Layouts • Deep integration with Android Studio + Layout Editor • Unbundled Library • Compatible with 99% of devices

Slide 3

Slide 3 text

Once upon a time

Slide 4

Slide 4 text

Slide 5

Slide 5 text

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

The Tetris Model

Slide 8

Slide 8 text

The Tetris Model • Relatively easy at first

Slide 9

Slide 9 text

The Tetris Model • Relatively easy at first • Can get very complex very quickly

Slide 10

Slide 10 text

The Tetris Model • Change is hard • Impose limits for animation / transitions • Not conductive to a great design tool experience • Performance can become an issue

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

Layout != Views

Slide 17

Slide 17 text

Flat Layouts

Slide 18

Slide 18 text

Flat Layouts • Layout definition is not mixed with the view hierarchy • Easy to manipulate in a graphical editor • Easy to change • Animation friendly • Keep layout computation in a single place

Slide 19

Slide 19 text

Slide 20

Slide 20 text

$ANDROID_HOME/platform-tools/systrace/systrace.py —time=10 -o ~/trace.html gfx view res CL 1.0.2, Nexus 5X

Slide 21

Slide 21 text

Expressivity

Slide 22

Slide 22 text

Solver

Slide 23

Slide 23 text

Simplex • Linear Programming • Invented by Georges Dantzig in 1947 • Large, empty matrices representing the equations

Slide 24

Slide 24 text

Example Minimize Constrained by Z x y z s t = Basic Feasible Solution (BFS) Matrix representation (plus slack variables s & t) Pivot on z, row 2 BFS Objective row

Slide 25

Slide 25 text

Cassowary • https://constraints.cs.washington.edu/cassowary/ • Constraints for User Interface Applications • Alan Borning, Kim Marriott, Peter Stuckey, and Yi Xiao, Solving Linear Arithmetic, Proceedings of the 1997 ACM Symposium on User Interface Software and Technology, October 1997, pages 87-96.

Slide 26

Slide 26 text

Cassowary • Incremental resolution and construction of the system • Handles positive and negative variables • Goal function and error representation

Slide 27

Slide 27 text

Pros / Cons • Very flexible, can represent any type of layout situation • But “relatively” heavy computation compared to simple layouts • Can be more memory intensive • Specifying layout via equations?…

Slide 28

Slide 28 text

ConstraintLayout solver • Pure java • Represents the system matrix as rows of sparse arrays • Memory Efficient • Efficient incremental construction • Has to have good performances on both Dalvik and Art

Slide 29

Slide 29 text

Constraints Model

Slide 30

Slide 30 text

Constraints Model • Simple model — relative positioning, centering • No Equations exposed • Evolving (bias, new dimension constraints, etc.)

Slide 31

Slide 31 text

Constraints Model

Slide 32

Slide 32 text

Constraints Model

Slide 33

Slide 33 text

Constraints Model

Slide 34

Slide 34 text

Constraints Model

Slide 35

Slide 35 text

Constraint Layout Solver Constraints Model

Slide 36

Slide 36 text

Constraint Layout Solver Constraints Model Optimizer

Slide 37

Slide 37 text

Optimizer

Slide 38

Slide 38 text

Optimizer • Constraints which can be resolved unambiguously • app:layout_optimizationLevel=“none|all|basic|chains” • multiple steps: • wrap content computation • direct resolution of simple dependency • chains on parent

Slide 39

Slide 39 text

Optimizer • match_constraint is more costly, as we need to remeasure • wrap_content, fixed dimension are cheap on a widget (end up as a value in the solver)

Slide 40

Slide 40 text

Optimizer - wrap content • If widgets contain match_constraint, no optimizations: • if ratio is specified • if percent is specified (for dimension, guideline) • if wrap_content is specified in the other dimension

Slide 41

Slide 41 text

Optimizer - Direct resolution • Only if the container is not in wrap_content • Only for widgets that are not match_constraints • Will resolve as much as it can before handing the system to the full solver

Slide 42

Slide 42 text

Optimizer - Chains • Only for chains that are connected to the container (stable endpoints) • Only for widgets in a chain that are not match_constraints / ratio • Right now, only for chains in spread mode

Slide 43

Slide 43 text

Functionalities

Slide 44

Slide 44 text

1.0 • Relative positioning • Center positioning & bias • Guidelines - helper objects • Chains • Dimension constraints: min/max, Ratio • ConstraintSet

Slide 45

Slide 45 text

Gone Behavior

Slide 46

Slide 46 text

Gone Behavior

Slide 47

Slide 47 text

Inadapted Margin

Slide 48

Slide 48 text

Inadapted Margin

Slide 49

Slide 49 text

Gone Margins

Slide 50

Slide 50 text

Gone Margins

Slide 51

Slide 51 text

Example

Slide 52

Slide 52 text

Example

Slide 53

Slide 53 text

beta 1 1.1.0

Slide 54

Slide 54 text

• Barriers beta 1 1.1.0

Slide 55

Slide 55 text

• Barriers beta 1 1.1.0

Slide 56

Slide 56 text

• Barriers beta 1 1.1.0

Slide 57

Slide 57 text

Barriers

Slide 58

Slide 58 text

Barriers

Slide 59

Slide 59 text

Barriers

Slide 60

Slide 60 text

Barriers

Slide 61

Slide 61 text

Barriers

Slide 62

Slide 62 text

• Barriers • Group - apply visibility to a set of widgets beta 1 1.1.0

Slide 63

Slide 63 text

Groups textview2 textview3

Slide 64

Slide 64 text

Groups

Slide 65

Slide 65 text

Groups Invisible Gone

Slide 66

Slide 66 text

• Barriers • Group • Placeholder beta 1 1.1.0 ?

Slide 67

Slide 67 text

class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) } fun select(v: View) { TransitionManager.beginDelayedTransition(main_layout) placeholder.setContentId(v.id) main_title.text= v.tag as CharSequence?;""; } } Placeholder

Slide 68

Slide 68 text

class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) } fun select(v: View) { TransitionManager.beginDelayedTransition(main_layout) placeholder.setContentId(v.id) main_title.text= v.tag as CharSequence?;""; } } Placeholder

Slide 69

Slide 69 text

• Barriers • Group • Placeholder • Percent dimensions beta 1 1.1.0

Slide 70

Slide 70 text

Dimension Constraints • width|height set to 0dp (“match_constraint”) • control behavior with • app:layout_constraintWidth_default=“spread|wrap|percent” • spread matches endpoints • wrap behave like wrap_content, but respect constraints

Slide 71

Slide 71 text

No content

Slide 72

Slide 72 text

default=wrap

Slide 73

Slide 73 text

default=wrap

Slide 74

Slide 74 text

default=wrap

Slide 75

Slide 75 text

Percent Dimension • As a percentage of the parent container • Not bounded by the container • value from 0 to 1 android:layout_width="0dp" app:layout_constraintWidth_default="percent" app:layout_constraintWidth_percent="0.5"

Slide 76

Slide 76 text

beta 1 1.1.0 maven { url "https://maven.google.com" } dependencies { ... compile 'com.android.support.constraint:constraint-layout:1.1.0-beta1' }

Slide 77

Slide 77 text

ConstraintSet

Slide 78

Slide 78 text

Layout != Views

Slide 79

Slide 79 text

ConstraintLayout Views

Slide 80

Slide 80 text

ConstraintLayout Views ConstraintSet

Slide 81

Slide 81 text

ConstraintLayout Views ConstraintSet

Slide 82

Slide 82 text

ConstraintLayout Views ConstraintSet

Slide 83

Slide 83 text

ConstraintSet

Slide 84

Slide 84 text

ConstraintSet • Separate views from how we layout them

Slide 85

Slide 85 text

ConstraintSet • Separate views from how we layout them • Encapsulate all constraints in an object

Slide 86

Slide 86 text

ConstraintSet • Separate views from how we layout them • Encapsulate all constraints in an object • You can apply a ConstraintSet to an existing ConstraintLayout

Slide 87

Slide 87 text

ConstraintSet • Separate views from how we layout them • Encapsulate all constraints in an object • You can apply a ConstraintSet to an existing ConstraintLayout • Switch between multiple ConstraintSet

Slide 88

Slide 88 text

No content

Slide 89

Slide 89 text

ConstraintSet mConstraintSet1 = new ConstraintSet(); ConstraintSet mConstraintSet2 = new ConstraintSet();

Slide 90

Slide 90 text

ConstraintSet mConstraintSet1 = new ConstraintSet(); ConstraintSet mConstraintSet2 = new ConstraintSet(); // get constraints from layout mConstraintSet2.clone(context, R.layout.state2); setContentView(R.layout.state1); mConstraintLayout = (ConstraintLayout) findViewById(R.id.activity_main); // get constraints from ConstraintSet mConstraintSet1.clone(mConstraintLayout);

Slide 91

Slide 91 text

ConstraintSet mConstraintSet1 = new ConstraintSet(); ConstraintSet mConstraintSet2 = new ConstraintSet(); // get constraints from layout mConstraintSet2.clone(context, R.layout.state2); setContentView(R.layout.state1); mConstraintLayout = (ConstraintLayout) findViewById(R.id.activity_main); // get constraints from ConstraintSet mConstraintSet1.clone(mConstraintLayout); mConstraintSet1.applyTo(mConstraintLayout);

Slide 92

Slide 92 text

Landscape

Slide 93

Slide 93 text

Rotation

Slide 94

Slide 94 text

Rotation

Slide 95

Slide 95 text

Motion

Slide 96

Slide 96 text

Motion • Flat Hierarchy == No clipping issues

Slide 97

Slide 97 text

Motion • Flat Hierarchy == No clipping issues • Scene Graph

Slide 98

Slide 98 text

Motion • Flat Hierarchy == No clipping issues • Scene Graph • ConstraintSet == Keyframe

Slide 99

Slide 99 text

ConstraintSet mConstraintSet1 = new ConstraintSet(); ConstraintSet mConstraintSet2 = new ConstraintSet(); // get constraints from layout mConstraintSet2.clone(context, R.layout.state2); setContentView(R.layout.state1); mConstraintLayout = (ConstraintLayout) findViewById(R.id.activity_main); // get constraints from ConstraintSet mConstraintSet1.clone(mConstraintLayout); mConstraintSet1.applyTo(mConstraintLayout);

Slide 100

Slide 100 text

ConstraintSet mConstraintSet1 = new ConstraintSet(); ConstraintSet mConstraintSet2 = new ConstraintSet(); // get constraints from layout mConstraintSet2.clone(context, R.layout.state2); setContentView(R.layout.state1); mConstraintLayout = (ConstraintLayout) findViewById(R.id.activity_main); // get constraints from ConstraintSet mConstraintSet1.clone(mConstraintLayout); mConstraintSet1.applyTo(mConstraintLayout);

Slide 101

Slide 101 text

TransitionManager.beginDelayedTransition(mConstraintLayout); ConstraintSet mConstraintSet1 = new ConstraintSet(); ConstraintSet mConstraintSet2 = new ConstraintSet(); // get constraints from layout mConstraintSet2.clone(context, R.layout.state2); setContentView(R.layout.state1); mConstraintLayout = (ConstraintLayout) findViewById(R.id.activity_main); // get constraints from ConstraintSet mConstraintSet1.clone(mConstraintLayout); mConstraintSet1.applyTo(mConstraintLayout);

Slide 102

Slide 102 text

No content

Slide 103

Slide 103 text

No content

Slide 104

Slide 104 text

ConstraintLayout : choreographer • What you can do with widgets, you can do with components • Everything is still the same • Flexible • Easy to build and test

Slide 105

Slide 105 text

NavigationView

Slide 106

Slide 106 text

NavigationView

Slide 107

Slide 107 text

No content

Slide 108

Slide 108 text

No content

Slide 109

Slide 109 text

• |

Slide 110

Slide 110 text

• |

Slide 111

Slide 111 text

Custom Transitions • ConstraintSet • Custom TransitionSet • Support library physics TransitionManager.beginDelayedTransition(cl, new MyCustomAnimationSet())

Slide 112

Slide 112 text

Custom Transitions • ConstraintSet • Custom TransitionSet • Support library physics TransitionManager.beginDelayedTransition(cl, new MyCustomAnimationSet())

Slide 113

Slide 113 text

What’s next

Slide 114

Slide 114 text

What’s next • More performance optimizations • experimental background resolution • segmentation of constraints • broadening the optimizer surface

Slide 115

Slide 115 text

What’s next • ConstraintHelper • Chains • Layers • Custom • Motion • better motion control • gesture support

Slide 116

Slide 116 text

Documentation • http://www.constraintlayout.com • https://developer.android.com/reference/android/support/ constraint/package-summary.html • https://developer.android.com/training/constraint-layout/ index.html • https://codelabs.developers.google.com/codelabs/constraint-layout • https://medium.com/google-developers/building-interfaces-with- constraintlayout-3958fa38a9f7

Slide 117

Slide 117 text

Documentation • http://www.constraintlayout.com • https://developer.android.com/reference/android/support/ constraint/package-summary.html • https://developer.android.com/training/constraint-layout/ index.html • https://codelabs.developers.google.com/codelabs/constraint-layout • https://medium.com/google-developers/building-interfaces-with- constraintlayout-3958fa38a9f7

Slide 118

Slide 118 text

Thank you! +NicolasRoard @camaelon http://b.android.com File bugs & requests on: