Slide 1

Slide 1 text

Constraint Layout One layout for all your views

Slide 2

Slide 2 text

What? • Constraints are relationship between 2 views in a layout. • Controls how that view should be positioned on the screen relative to other elements in the layout. • Constraints connect points on a view (called anchor points) to a target of some kind.

Slide 3

Slide 3 text

Why? • Great IDE Support. • Ability to do complex layouts. • Better performance due to minimised nesting. • Supports API +9.

Slide 4

Slide 4 text

How? layout_constraint[SourceAnchor]_[TargetAnchor]="[TargetId]" implementation ‘com.android.support.constraint:constraint-layout:1.1.0-beta1' Gradle Dependency Constraint Format

Slide 5

Slide 5 text

Source-Target

Slide 6

Slide 6 text

Biasing Constraints Bias applies a weighted proportion to the pair of constraints to unevenly distribute the spacing.

Slide 7

Slide 7 text

View Measurements • Wrap Content: The view expands as needed to fit its contents. • Match Constraints (0dp): The view expands as needed to meet the definition of its constraints after accounting for margins. • However, if the given dimension has only one constraint, then the view expands to fit its contents. Using this mode on either the height or width also allows you to set a size ratio. • Fixed: You specify a specific dimension in the text box below or by resizing the view in the editor.

Slide 8

Slide 8 text

Constraint Dimension Ratio • With ConstraintLayout, you will no longer have to create a custom subclass of View or ViewGroup thanks to layout_constraintDimensionRatio. • This feature requires one of the dimensions in the attached view to be “known” (fixed dimension or wrap_content) and the other to be “any size” (0dp).

Slide 9

Slide 9 text

Constraint Dimension Ratio …

Slide 10

Slide 10 text

Guidelines • In cases where an arbitrary anchor point is needed for view alignment, ConstraintLayout supports guidelines. • They always measure their size to be 0. They force their own visibility to View.GONE. • A guideline can have one of the following attributes: layout_constraintGuide_begin or layout_constraintGuide_end or layout_constraintGuide_Percent

Slide 11

Slide 11 text

Guidelines

Slide 12

Slide 12 text

Slide 13

Slide 13 text

Barriers • A barrier is an invisible view that contains reference to the views that you wish to use to form a “barrier” against. • If one of the views grows, the barrier will adjust its size to the largest height or width of the referenced items.

Slide 14

Slide 14 text

Slide 15

Slide 15 text

Chains • Chains allow you to control the space between elements and how the elements use the space. • To create a chain, select the elements that you want to form part of the chain, and then right click – Chain – Create Horizontal/Vertical Chain. • There are four different modes: spread_inside, packed, spread and weighted.

Slide 16

Slide 16 text

Chains

Slide 17

Slide 17 text

Slide 18

Slide 18 text

Groups • With groups, you can logically group together certain views. • A group in ConstraintLayout only contains references to the view ids and not nesting the views inside a group. • With a group, you can set the visibility of all views in the group, by just setting the groups visibility without needing to set every view’s visibility.

Slide 19

Slide 19 text

Slide 20

Slide 20 text

Constraint Set @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Context context = this; mConstraintSet2.clone(context, R.layout.state2); // get constraints from layout setContentView(R.layout.state1); mConstraintLayout = (ConstraintLayout) findViewById(R.id.activity_main); mConstraintSet1.clone(mConstraintLayout); // get constraints from ConstraintSet } public void foo(View view) { TransitionManager.beginDelayedTransition(mConstraintLayout); if (mOld = !mOld) { mConstraintSet1.applyTo(mConstraintLayout); // set new constraints } else { mConstraintSet2.applyTo(mConstraintLayout); // set new constraints } } This class allows you to define programmatically a set of constraints to be used with ConstraintLayout.

Slide 21

Slide 21 text

Infer Constraints • Instead of adding constraints to every view as you place them in the layout, you can move each view into the positions you desire, and then click Infer Constraints to automatically create constraints. • Infer Constraints scans the layout to determine the most effective set of constraints for all views. It makes a best effort to constrain the views to their current positions while allowing flexibility.

Slide 22

Slide 22 text

Auto Connect • Autoconnect is a separate feature that is either on or off. • When turned on, it automatically creates two or more constraints for each view as you add them to the layout, but only when appropriate to constrain the view to the parent layout. • Autoconnect does not create constraints to other views in the layout.

Slide 23

Slide 23 text

References • https://riggaroo.co.za/constraintlayout-guidelines- barriers-chains-groups/ • http://wiresareobsolete.com/2016/07/constraintlayout- part-1/ • https://developer.android.com/training/constraint-layout/ index.html

Slide 24

Slide 24 text

Thank you Chetan Sachdeva Android Dev at Fueled