Slide 1

Slide 1 text

No content

Slide 2

Slide 2 text

Why yet another layout Why yet another layout ● Flat hierarchy – Simpler layouts – Faster

Slide 3

Slide 3 text

Basics Basics ● Normal ViewGroup ● Similar to RelativeLayout – But more flexible ● Similar to Apple‘s AutoLayout ● Separate library (like RecyclerView) ● Backwards compatible to v9

Slide 4

Slide 4 text

Project Initialization Project Initialization compile { 'com.android.support.constraint:constraint-layout:1.0.2' } ● Android Studio 2.2 or later – I recommend to use the 2.4 preview ● Up to date Android Support Repository ● Build.gradle:

Slide 5

Slide 5 text

What are Constraints What are Constraints ● Define appearence of view with respect to – Size – Position – Visibility

Slide 6

Slide 6 text

Concepts Concepts ● LayoutEditor – Blueprint and preview view – Create constraints – Define properties ● XML still usable – Good for pull requests / code reviews

Slide 7

Slide 7 text

Concepts Concepts ● Cassowary algorithm – Also used in Apple's AutoLayout ● iOS 6 / iPhone 5 – before for OS X development – And on the web (Constraint CSS)

Slide 8

Slide 8 text

Blueprint Editor Blueprint Editor

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

View sizes View sizes ● wrap_content ● fixed sized ● Stretched (0dp / match_constraint) – No match_parent for CL children

Slide 11

Slide 11 text

Visibility Visibility ● View.GONE – View takes up 0dp size – Constraints held up transitively – View has no margin – Extra margins to View.GONE targets possible ● For example: layout_goneMarginStart="16dp" ● Currently XML only

Slide 12

Slide 12 text

Guidelines Guidelines ● Proper views – But ● 0dp sized ● Not drawn ● Not layouted ● Can represent percentages

Slide 13

Slide 13 text

Aspect Ratio Aspect Ratio ● For match_constraint views ● Constraints size of – one explicitly stated side of the view – Or both depending on available space

Slide 14

Slide 14 text

Chains Chains ● Created by constraining views to each other ● Useful for – positioning the chain itself – Creating views of the same size – Spreading views equally

Slide 15

Slide 15 text

Chains Chains ● Three modes: – packed ● Variation: packed with bias – Determines the positioning of the packed chain – spread – spread inside ● Variation: spread with weights (as with LinearLayout) – Determines the distribution of chain elements

Slide 16

Slide 16 text

Chains Chains

Slide 17

Slide 17 text

Chains Chains

Slide 18

Slide 18 text

Chains Chains

Slide 19

Slide 19 text

Chains Chains ● One View can be part of multiple chains

Slide 20

Slide 20 text

wrap_content wrap_content ● CL can be set to wrap_content ● Uses sizes defined with android:minWidth android:minHeight

Slide 21

Slide 21 text

CL programmatically CL programmatically ● Use ConstraintSet ● Works great when cloning from a layout ● You don‘t want to do everything in code ● Easily usable for changing constraints at runtime

Slide 22

Slide 22 text

CL programmatically CL programmatically TransitionManager.beginDelayedTransition(layout); ConstraintSet set = new ConstraintSet(); set.clone(layout); set.clear(R.id.btn_move, ConstraintSet.END); set.connect(R.id.btn_move, ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START, margin); set.applyTo(layout);

Slide 23

Slide 23 text

CL programmatically CL programmatically TransitionManager.beginDelayedTransition(layout); ConstraintSet set = new ConstraintSet(); set.clone(layout); set.clear(R.id.btn_move); set.constrainHeight(R.id.btn_move, ConstraintSet.WRAP_CONTENT); set.constrainWidth(R.id.btn_move, ConstraintSet.WRAP_CONTENT); set.connect(R.id.btn_move, ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START, margin); // another connect for vertical position also needed set.applyTo(layout);

Slide 24

Slide 24 text

CL programmatically CL programmatically TransitionManager.beginDelayedTransition(layout); ConstraintSet set = new ConstraintSet(); set.clone(layout); set.clear(R.id.btn_move); set.constrainHeight(R.id.btn_move, ConstraintSet.WRAP_CONTENT); set.constrainWidth(R.id.btn_move, ConstraintSet.WRAP_CONTENT); // another connect for vertical position also needed set.connect(R.id.btn_move, ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START, margin); set.applyTo(layout);

Slide 25

Slide 25 text

CL vs. AutoLayout CL vs. AutoLayout Maybe I'm a bit biased, but...

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

CL vs. AutoLayout CL vs. AutoLayout ● CL much simpler to handle ● CL less powerful – Inequalities and priorities ● Android's XML easier to understand ● No guidelines in AutoLayout ● View.GONE equivalent in AutoLayout requires ton of extra work

Slide 32

Slide 32 text

Some bugs Some bugs ● Sometimes superfluous absoluteX/absoluteY attributes ● Preview wrong for unconstrained views – Alas, Google did this on purpose ● match_constraint bug for packed chains – treated as View without width

Slide 33

Slide 33 text

Some more bugs Some more bugs ● LE sometimes omits margins ● LE looses zoom level when switching view types ● LE doesn‘t always show bias handles ● LE has difficulties with start/end ● Conversion of Layouts in 2.4 buggy

Slide 34

Slide 34 text

Some nice to haves Some nice to haves ● Yellow instead of red indicator for warnings ● Indicator for chains in component tree ● ConstraintSet.clone() as factory methods

Slide 35

Slide 35 text

Future work Future work ● No I/O talk about layouts scheduled

Slide 36

Slide 36 text

Future work Future work ● No I/O talk about layouts scheduled ● But one about the layout editor

Slide 37

Slide 37 text

Future work Future work ● And this is still in the docs: ConstraintLayout is available as a support library ... As such, we are planning in enriching its API and capabilities over time. https://developer.android.com/reference/android/support/constraint/Constraint Layout.html

Slide 38

Slide 38 text

Even More Material Even More Material ● Google training https://developer.android.com/training/constraint- layout/index.html ● Layout Editor User Guide https://developer.android.com/studio/write/layout-editor.html ● API reference https://developer.android.com/reference/android/support/constr aint/package-summary.html

Slide 39

Slide 39 text

No content