Slide 1

Slide 1 text

CONSTRAINT LAYOUT 2.0 Nicolas Roard & John Hoford @camaelon @johnhoford (Sneak peek)

Slide 2

Slide 2 text

BUT FIRST…

Slide 3

Slide 3 text

CL 1.1 1.1

Slide 4

Slide 4 text

ConstraintLayout 2.0 preview GOOGLE MAVEN REPOSITORY dependencies { compile 'com.android.support.constraint:constraint-layout:1.1.0' }

Slide 5

Slide 5 text

ConstraintLayout 2.0 preview CONSTRAINT LAYOUT 1.1 ➤ Constraints ➤ Helper objects ➤ Optimizer ➤ ton of bug fixes!

Slide 6

Slide 6 text

ConstraintLayout 2.0 preview CONSTRAINTS ➤ Constrained Dimensions : percent, min/max, wrap ➤ Circular constraints ➤ Complex chains

Slide 7

Slide 7 text

ConstraintLayout 2.0 preview CIRCULAR CONSTRAINTS

Slide 8

Slide 8 text

ConstraintLayout 2.0 preview EXAMPLE Andrew Kelly https://medium.com/devnibbles/ constraintlayout-circular- positioning-9489b11cb0e5

Slide 9

Slide 9 text

ConstraintLayout 2.0 preview EXAMPLE Andrew Kelly https://medium.com/devnibbles/ constraintlayout-circular- positioning-9489b11cb0e5

Slide 10

Slide 10 text

ConstraintLayout 2.0 preview HELPER OBJECTS ➤ Barriers ➤ Groups ➤ Placeholders

Slide 11

Slide 11 text

ConstraintLayout 2.0 preview BARRIERS ➤ A barrier takes the minimum or maximum of a set of widget ➤ Other widgets can then be constrained to it ➤ Allows to build more flexible layouts Helper object

Slide 12

Slide 12 text

Barriers

Slide 13

Slide 13 text

Barriers

Slide 14

Slide 14 text

Barriers 1 2 3

Slide 15

Slide 15 text

Barriers 1 2 3 4

Slide 16

Slide 16 text

Barriers

Slide 17

Slide 17 text

Barriers

Slide 18

Slide 18 text

ConstraintLayout 2.0 preview GROUPS ➤ Define a set of widgets ➤ Apply visibility to them Helper object

Slide 19

Slide 19 text

Groups textview2 textview3

Slide 20

Slide 20 text

Groups Invisible Gone

Slide 21

Slide 21 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 22

Slide 22 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 23

Slide 23 text

ConstraintLayout 2.0 preview OPTIMIZER ➤ New optimizer in 1.1 ➤ A lot more powerful :) ➤ Multiple optimizations passes available ➤ Direct & Center connections ➤ Barriers ➤ Chains (experimental) ➤ Dimensions (experimental)

Slide 24

Slide 24 text

ConstraintLayout 2.0 preview OPTIMIZER ATTRIBUTE app:layout_optimizationLevel=“standard” app:layout_optimizationLevel=“direct|barriers|chains|dimensions”

Slide 25

Slide 25 text

ConstraintLayout 2.0 preview OPTIMIZER CONSTRAINTS OPTIMIZER SOLVER MEASURES

Slide 26

Slide 26 text

ConstraintLayout 2.0 preview OPTIMIZER Widget A

Slide 27

Slide 27 text

ConstraintLayout 2.0 preview OPTIMIZER Widget A

Slide 28

Slide 28 text

ConstraintLayout 2.0 preview OPTIMIZER Widget A

Slide 29

Slide 29 text

ConstraintLayout 2.0 preview OPTIMIZER Widget A

Slide 30

Slide 30 text

ConstraintLayout 2.0 preview OPTIMIZER Widget A Widget B

Slide 31

Slide 31 text

ConstraintLayout 2.0 preview OPTIMIZER Widget A Widget B

Slide 32

Slide 32 text

ConstraintLayout 2.0 preview OPTIMIZER Widget A Widget B

Slide 33

Slide 33 text

ConstraintLayout 2.0 preview OPTIMIZER

Slide 34

Slide 34 text

ConstraintLayout 2.0 preview OPTIMIZER

Slide 35

Slide 35 text

CONSTRAINT LAYOUT 2.0Preview

Slide 36

Slide 36 text

Slide 37

Slide 37 text

CL 2.0 WHY?

Slide 38

Slide 38 text

CL 2.0 UI : RAISING THE BAR

Slide 39

Slide 39 text

CL 2.0 HELP!

Slide 40

Slide 40 text

CL 2.0 VOCABULARY

Slide 41

Slide 41 text

ConstraintLayout 2.0 preview TOOLING ➤ Integrated manipulation in the component tree (like for barriers) ➤ Exposed in the Palette ➤ Support 3rd party libraries ➤ exposing their custom views / helpers

Slide 42

Slide 42 text

CL 2.0 HELPERS

Slide 43

Slide 43 text

ConstraintLayout 2.0 preview THREE MAIN CATEGORIES OF HELPERS Layout Manipulation Rendering or Decorating Post-Layout Manipulation

Slide 44

Slide 44 text

ConstraintLayout 2.0 preview CONSTRAINT HELPERS ➤ Keep a reference to existing views ➤ Flat hierarchy ➤ Views can be referenced by multiple helpers ➤ Essentially, it allows you to tag views, setting specific behaviors

Slide 45

Slide 45 text

ConstraintLayout 2.0 preview API ➤ Access a list of views ➤ pre/post layouts callbacks ➤ normal views ➤ already used ➤ barriers ➤ groups

Slide 46

Slide 46 text

ConstraintLayout 2.0 preview COLLECTION ➤ Trampoline object ➤ forward function calls to referenced views ➤ setVisibility ➤ setAlpha ➤ setRotation, etc. ➤ no getter!

Slide 47

Slide 47 text

CL 2.0 DECORATORS

Slide 48

Slide 48 text

ConstraintLayout 2.0 preview DECORATORS ➤ Can reference views ➤ Easily build decorators that depends on views location / visibility / etc

Slide 49

Slide 49 text

ConstraintLayout 2.0 preview METABALLS public class MetaballsDecorator extends ConstraintHelper { public void updatePostLayout(ConstraintLayout container) { int[] ids = getReferencedIds(); final int count = ids.length; for (int i = 0; i < count; i++) { View view = container.getViewById(ids[i]); // do something } } @Override public void onDraw(Canvas canvas) { // do something } }

Slide 50

Slide 50 text

Decorator Canvas ImageViews

Slide 51

Slide 51 text

Decorator Canvas ImageViews

Slide 52

Slide 52 text

Decorator Canvas ImageViews

Slide 53

Slide 53 text

Metaballs Decorator

Slide 54

Slide 54 text

Metaballs Decorator

Slide 55

Slide 55 text

Metaballs Decorator

Slide 56

Slide 56 text

Metaballs Decorator

Slide 57

Slide 57 text

ConstraintLayout 2.0 preview LAYER DECORATOR ➤ Group ++ ➤ Manipulate graphically a collection of views ➤ Supports transforms, etc. ➤ Can be set as the bounding box of the referenced views

Slide 58

Slide 58 text

ConstraintLayout 2.0 preview LET’S CREATE A DRAWABLE…

Slide 59

Slide 59 text

ConstraintLayout 2.0 preview LAYER DECORATOR

Slide 60

Slide 60 text

ConstraintLayout 2.0 preview LAYER DECORATOR

Slide 61

Slide 61 text

ConstraintLayout 2.0 preview LAYER DECORATOR

Slide 62

Slide 62 text

ConstraintLayout 2.0 preview LAYER DECORATOR

Slide 63

Slide 63 text

ConstraintLayout 2.0 preview LAYERS : PROGRAMMATIC ACCESS Layer layer = findViewById(R.id.layer); layer.setRotation(angle); layer.setScaleX(1+(180 - Math.abs(angle-180))/20f); layer.setScaleY(1+(180 - Math.abs(angle-180))/20f); float shift_x = 500 * (float) Math.sin(Math.toRadians(angle*5)); float shift_y = 500 * (float) Math.sin(Math.toRadians(angle*7)); layer.setTranslationX(shift_x); layer.setTranslationY(shift_y);

Slide 64

Slide 64 text

ConstraintLayout 2.0 preview FLY-IN DECORATOR

Slide 65

Slide 65 text

ConstraintLayout 2.0 preview FLY-IN DECORATOR

Slide 66

Slide 66 text

ConstraintLayout 2.0 preview FLY-IN DECORATOR

Slide 67

Slide 67 text

ConstraintLayout 2.0 preview FLY-IN DECORATOR

Slide 68

Slide 68 text

ConstraintLayout 2.0 preview FLY-IN DECORATOR

Slide 69

Slide 69 text

ConstraintLayout 2.0 preview FLY-IN DECORATOR

Slide 70

Slide 70 text

ConstraintLayout 2.0 preview FLY-IN DECORATOR

Slide 71

Slide 71 text

ConstraintLayout 2.0 preview FLY-IN DECORATOR

Slide 72

Slide 72 text

ConstraintLayout 2.0 preview FLY IN DECORATOR @Override public void updatePostLayout(ConstraintLayout container) { update(); } void update(){ if (mContainer == null) { return; } mComputedCenterX = Float.NaN; mComputedCenterY = Float.NaN; View[] views = getViews(mContainer); calcCenters(); float shift = myFlyValue-1; for (int i = 0; i < mCount; i++) { View view = views[i]; int x = (view.getLeft() + view.getRight()) / 2; int y = (view.getTop() + view.getBottom()) / 2; view.setTranslationX((x - mComputedCenterX)*shift); view.setTranslationY((y - mComputedCenterY)*shift); } }

Slide 73

Slide 73 text

ConstraintLayout 2.0 preview FLY IN DECORATOR @Override public void updatePreLayout(ConstraintLayout container) { if (mContainer!=container) { setFlyIn(10); ObjectAnimator.ofFloat(this, "FlyIn", 1f) .setDuration(1000).start(); } mContainer = container; } public void setFlyIn(float flyIn) { myFlyValue = flyIn; update(); }

Slide 74

Slide 74 text

ConstraintLayout 2.0 preview FLY IN DECORATOR: XML

Slide 75

Slide 75 text

ConstraintLayout 2.0 preview (BONUS CODE) public void calcCenters() { if (!(Float.isNaN(mComputedCenterX) || Float.isNaN(mComputedCenterY))) { return; } if (Float.isNaN(mCenterX) || Float.isNaN(mCenterY)) { int minx = Integer.MAX_VALUE, miny= Integer.MAX_VALUE; int maxx= Integer.MIN_VALUE,maxy= Integer.MIN_VALUE; View []views = getViews(mContainer); for (int i = 0; i < mCount; i++) { View view = views[i]; minx = Math.min(minx, view.getLeft()); miny = Math.min(miny, view.getTop()); maxx = Math.max(maxx, view.getRight()); maxy = Math.max(maxy, view.getBottom()); } mComputedCenterX = (Float.isNaN(mCenterX))?(minx + maxx) / 2:mCenterX; mComputedCenterY = (Float.isNaN(mCenterY))?(miny + maxy) / 2:mCenterY; } else { mComputedCenterY = mCenterY; mComputedCenterX = mCenterX; } }

Slide 76

Slide 76 text

ConstraintLayout 2.0 preview CIRCULAR REVEAL

Slide 77

Slide 77 text

ConstraintLayout 2.0 preview CIRCULAR REVEAL

Slide 78

Slide 78 text

ConstraintLayout 2.0 preview CIRCULAR REVEAL @Override public void updatePostLayout(ConstraintLayout container) { super.updatePostLayout(container); if (mContainer != container) { int rad =(int) Math.hypot(mComputedMaxY- mComputedMinY,mComputedMaxX-mComputedMinX); Animator anim = ViewAnimationUtils.createCircularReveal(this, (int)mComputedCenterX-getLeft(), (int)mComputedCenterY-getTop(), 0, rad); anim.setDuration(2000); anim.start(); } mContainer = container; }

Slide 79

Slide 79 text

ConstraintLayout 2.0 preview DECORATOR HELPERS ➤ Tag your views with behavior ➤ Encapsulate behavior ➤ Declaratively use them

Slide 80

Slide 80 text

CL 2.0 VIRTUAL LAYOUTS

Slide 81

Slide 81 text

ConstraintLayout 2.0 preview VIRTUAL LAYOUTS ➤ Layout the referenced views ➤ Still keep flat hierarchy ➤ Linear ➤ Flow

Slide 82

Slide 82 text

ConstraintLayout 2.0 preview LINEAR ➤ Create horizontal or vertical chains ➤ Somewhat similar to linear layout, but as a helper

Slide 83

Slide 83 text

ConstraintLayout 2.0 preview LINEAR

Slide 84

Slide 84 text

ConstraintLayout 2.0 preview LINEAR

Slide 85

Slide 85 text

ConstraintLayout 2.0 preview

Slide 86

Slide 86 text

ConstraintLayout 2.0 preview LINEAR

Slide 87

Slide 87 text

ConstraintLayout 2.0 preview FLOW ➤ Implements FlexboxLayout-like semantics ➤ Overflow elements will be pushed to the next row ➤ Still flat layout! ➤ Able to position outside elements relative to the ones in Flow

Slide 88

Slide 88 text

ConstraintLayout 2.0 preview FLOW

Slide 89

Slide 89 text

ConstraintLayout 2.0 preview REFERENCE BUTTONS

Slide 90

Slide 90 text

ConstraintLayout 2.0 preview FLOW

Slide 91

Slide 91 text

ConstraintLayout 2.0 preview FLOW

Slide 92

Slide 92 text

ConstraintLayout 2.0 preview FLOW

Slide 93

Slide 93 text

CL 2.0 LAYOUT MANAGEMENT

Slide 94

Slide 94 text

ConstraintLayout 2.0 preview MANAGING STATES

Slide 95

Slide 95 text

ConstraintLayout 2.0 preview MANAGING STATES

Slide 96

Slide 96 text

ConstraintLayout 2.0 preview STATE XML

Slide 97

Slide 97 text

ConstraintLayout 2.0 preview JAVA-SIDE @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout); cl = findViewById(R.id.root); cl.setLayoutDescription(R.xml.layout_states); }

Slide 98

Slide 98 text

ConstraintLayout 2.0 preview JAVA-SIDE @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout); cl = findViewById(R.id.root); cl.setLayoutDescription(R.xml.layout_states); } public void change(View v) { cl.setState(closed ? R.id.large : R.id.small); closed = !closed; }

Slide 99

Slide 99 text

ConstraintLayout 2.0 preview STATE XML : SIZE QUALIFIERS

Slide 100

Slide 100 text

ConstraintLayout 2.0 preview ON CONFIGURATION CHANGE @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); cl.setState(newConfig.screenWidthDp, newConfig.screenHeightDp); }

Slide 101

Slide 101 text

ConstraintLayout 2.0 preview LIVE RESIZE

Slide 102

Slide 102 text

ConstraintLayout 2.0 preview LIVE RESIZE

Slide 103

Slide 103 text

ConstraintLayout 2.0 preview LIVE RESIZE

Slide 104

Slide 104 text

ConstraintLayout 2.0 preview LIVE RESIZE

Slide 105

Slide 105 text

ConstraintLayout 2.0 preview LIVE RESIZE

Slide 106

Slide 106 text

ConstraintLayout 2.0 preview LIVE RESIZE

Slide 107

Slide 107 text

ConstraintLayout 2.0 preview CALLBACK cl.setOnConstraintsChanged(new ConstraintsChangedListener() { @Override public void preLayoutChange(int state,int layoutId) { TransitionManager.beginDelayedTransition(cl); } });

Slide 108

Slide 108 text

ConstraintLayout 2.0 preview ANIMATED LIVE RESIZE (TRANSITION MANAGER)

Slide 109

Slide 109 text

ConstraintLayout 2.0 preview ANIMATED LIVE RESIZE (TRANSITION MANAGER)

Slide 110

Slide 110 text

ConstraintLayout 2.0 preview LAYOUT MANAGEMENT ➤ Centralize multiple layouts in a single XML ➤ Allows to easily initialize and deal with different representations for your layout ➤ Size qualifiers & live resize

Slide 111

Slide 111 text

CL 2.0 FLUENT API

Slide 112

Slide 112 text

ConstraintLayout 2.0 preview FLUENT API ➤ ConstraintSet: great to capture full state and manage it ➤ …But not that ideal for direct manipulation ➤ Fluent API to change layout params directly

Slide 113

Slide 113 text

ConstraintLayout 2.0 preview FLUENT API new Constraints(view).margin(ConstraintProperties.TOP,23).apply(); Constraints.on(view) .margin(TOP, 16) .margin(BOTTOM, 16) .margin(LEFT, 16) .margin(RIGHT, 16) .apply(); Constraints.on(view) .center(R.id.button1, LEFT, 23, R.id.button2, RIGHT, 23,.05f) .connect(TOP,PARENT_ID, TOP, 32) .apply();

Slide 114

Slide 114 text

ConstraintLayout 2.0 preview EXAMPLE : CENTER Constraints.on(myView) .center(R.id.button1, LEFT, R.id.button2, RIGHT) .horizontalBias(.5f) .top(PARENT_ID,TOP,32) .apply();

Slide 115

Slide 115 text

ConstraintLayout 2.0 preview center(int firstID, int firstSide, int firstMargin, int secondId, int secondSide, int secondMargin, f centerHorizontally(int leftId, int leftSide, int leftMargin, int rightId, int rightSide, int rightMar centerHorizontallyRtl(int startId, int startSide, int startMargin, int endId, int endSide, int endMar centerVertically(int topId, int topSide, int topMargin, int bottomId, int bottomSide, int bottomMargi centerHorizontally(int toView) centerHorizontallyRtl(int toView) centerVertically(int toView) removeConstraints(int anchor) margin(int anchor, int value) goneMargin(int anchor, int value) horizontalBias(float bias) verticalBias(float bias) dimensionRatio(String ratio) visibility(int visibility) alpha(float alpha) elevation(float elevation) rotation(float rotation) rotationX(float rotationX) rotationY(float rotationY) scaleX(float scaleX) scaleY(float scaleY) CONSTRAINTS API… translationX(float translationX) translationY(float translationY) translation(float translationX, float translationY) translationZ(float translationZ) constrainHeight(int height) constrainWidth(int width) constrainMaxHeight(int height) constrainMaxWidth(int width) constrainMinHeight(int height) constrainMinWidth(int width) constrainDefaultHeight(int height) constrainDefaultWidth(int width) horizontalWeight(float weight) verticalWeight(float weight) horizontalChainStyle(int chainStyle) verticalChainStyle(int chainStyle) addToHorizontalChain(int leftId, int rightId) addToHorizontalChainRTL(int leftId, int rightId) addToVerticalChain(int topId, int bottomId) removeFromVerticalChain() removeFromHorizontalChain() connect(int startSide, int endID, int endSide, int margin

Slide 116

Slide 116 text

ConstraintLayout 2.0 preview CONSTRAINT LAYOUT 2.0 - SUMMARY ➤ Virtual Layouts ➤ Layers & Transitions ➤ Decorators ➤ State management & Live Resize ➤ Fluent API for LayoutParams ➤ …more to come!

Slide 117

Slide 117 text

THANKS! Nicolas Roard & John Hoford @camaelon @johnhoford

Slide 118

Slide 118 text

CL 2.0 TO BE CONTINUED… … at Google IO 2018!

Slide 119

Slide 119 text

ConstraintLayout 2.0 preview 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