Upgrade to Pro — share decks privately, control downloads, hide ads and more …

ConstraintLayout 2.0 & MotionLayout

Efeturi Money
November 03, 2018

ConstraintLayout 2.0 & MotionLayout

Efeturi Money

November 03, 2018
Tweet

More Decks by Efeturi Money

Other Decks in Technology

Transcript

  1. ConstraintLayout so far... Relative positioning <Button android:id="@+id/buttonA" ... /> <Button

    android:id="@+id/buttonB" app:layout_constraintLeft_toRightOf="@+id/buttonA" ... />
  2. ConstraintLayout so far... Relative positioning <Button android:id="@+id/buttonA" ... /> <Button

    android:id="@+id/buttonB" app:layout_constraintLeft_toRightOf="@+id/buttonA" ... /> app:layout_constraintBaseline_toBaselineOf Baseline Constraints FTW!
  3. ConstraintLayout so far... Margin & ‘GONE’ View handling <Button android:id="@+id/buttonA"

    ... /> <Button android:id="@+id/buttonB" app:layout_constraintLeft_toRightOf="@+id/buttonA" app:layout_marginLeft="@dimen/button_margin" ... />
  4. ConstraintLayout so far... Percent, Aspect Ratio and Min/Max constraints <Button

    android:id="@+id/buttonA" app:layout_constraintWidth_percent="0.25" ... /> <Button android:id="@+id/buttonB" app:layout_width="match_parent" app:layout_constraintDimensionRatio="W,16:9" ... />
  5. ConstraintLayout so far... Constraint Sets override fun onCreate(savedInstanceState: Bundle?) {

    ... val constraintSet1 = ConstraintSet() constraintSet1.clone(constraintLayout) val constraintSet2 = ConstraintSet() constraintSet2.clone(this, R.layout.layout_state_2) var changed = false findViewById(R.id.button).setOnClickListener { (if (changed) { constraintSet1 } else { constraintSet2 }).applyTo(constraintLayout) changed = !changed } } Change Constraints dynamically
  6. ConstraintLayout so far... Constraint Sets Switch between AB test variants

    of your app in a single (sort of) line of code (if (userIsPartOfABTest) ratingsConstraints else noRatingConstraints).applyTo(constraintLayout)
  7. ConstraintLayout so far... Constraint Sets override fun onCreate(savedInstanceState: Bundle?) {

    ... val constraintSet1 = ConstraintSet() constraintSet1.clone(constraintLayout) val constraintSet2 = ConstraintSet() constraintSet2.clone(this, R.layout.layout_state_2) var changed = false findViewById(R.id.button).setOnClickListener { TransitionManager.beginDelayedTransition(constraintLayout) (if (changed) { constraintSet1 } else { constraintSet2 }).applyTo(constraintLayout) changed = !changed } } Change Constraints dynamically
  8. Group Provides an easy way to hide or show a

    set of views directly in XML <android.support.constraint.Group android:id="@+id/group" android:layout_width="wrap_content" android:layout_height="wrap_content" android:visibility="visible" app:constraint_referenced_ids="button4,button9"/>
  9. ConstraintLayout 1.1 Circle Constraints <Button android:id="@+id/buttonA" ... /> <Button android:id="@+id/buttonB"

    app:layout_constraintCircle="@+id/buttonA" app:layout_constraintCircleRadius="100dp" app:layout_constraintCircleAngle="45" ... />
  10. ConstraintLayout 2.0 ConstraintHelper: Quick Facts • Keep reference to existing

    views • Views can be referenced by multiple Helpers
  11. ConstraintLayout 2.0 ConstraintHelper: Quick Facts • Keep reference to existing

    views • Views can be referenced by multiple Helpers • Not ViewGroups - UI layout remains FLAT :)
  12. ConstraintLayout 2.0 ConstraintHelper: Quick Facts • Keep reference to existing

    views • Views can be referenced by multiple Helpers • Not ViewGroups - UI layout remains FLAT :) • They are divided into 3 broad categories
  13. ConstraintLayout 2.0 ConstraintHelper: 3 main categories of helpers Layout Manipulation

    Post-Layout Manipulation Rendering / Decorating aka VirtualLayout
  14. VirtualLayout Linear Kind of like LinearLayout but as a ConstraintHelper

    Creates horizontal or virtual chains Location
  15. VirtualLayout Flow Implements FlexboxLayout-like semantics Overflow Views will be pushed

    to the next row Layout is stil FLAT :) Able to position outside elements relatives to ones in Flow Location
  16. Post-Layout Manipulations Are able to act after Views have been

    laid out. Very useful for visibility animations/transitions. Also useful for Eg. FadeInHelper Location
  17. Layer Graphically manipulate a set of Views as one Apply

    transforms (setRotation, setTranslationY etc) on multiple Views Can be set as a bounding box for its referenced Views Location
  18. ConstraintLayout 2.0 Constraint Sets pre 2.0 override fun onCreate(savedInstanceState: Bundle?)

    { ... val constraintSet1 = ConstraintSet() constraintSet1.clone(constraintLayout) val constraintSet2 = ConstraintSet() constraintSet2.clone(this, R.layout.layout_state_2) var changed = false findViewById(R.id.button).setOnClickListener { (if (changed) { constraintSet1 } else { constraintSet2 }).applyTo(constraintLayout) changed = !changed } }
  19. ConstraintLayout 2.0 Constraint Sets 2.0 override fun onCreate(savedInstanceState: Bundle?) {

    ... constraintLayout.loadLayoutDescription(R.xml.states) var changed = false findViewById(R.id.button).setOnClickListener { constraintLayout.setState( if (changed) R.id.start else R.id.end ) changed = !changed } }
  20. MotionLayout <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent">

    <View android:id="@+id/button" android:background="@color/colorAccent" android:layout_width="64dp" android:layout_height="64dp" android:layout_marginStart="8dp" android:text="Button" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </android.support.constraint.ConstraintLayout>
  21. MotionLayout <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent">

    <View android:id="@+id/button" android:background="@color/colorAccent" android:layout_width="64dp" android:layout_height="64dp" android:layout_marginEnd="8dp" android:text="Button" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" /> </android.support.constraint.ConstraintLayout>
  22. MotionLayout Defining MotionLayout <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.motion.MotionLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/motionLayout" android:layout_width="match_parent" android:layout_height="match_parent" app:layoutDescription="@xml/scene_01" tools:showPaths="true"> <View android:id="@+id/button" android:layout_width="64dp" android:layout_height="64dp" android:background="@color/colorAccent" android:text="Button" /> </android.support.constraint.motion.MotionLayout> <?xml version="1.0" encoding="utf-8"?> <MotionScene xmlns:motion="http://schemas.android.com/apk/res-auto"> <Transition motion:constraintSetStart="@layout/motion_01_cl_start" motion:constraintSetEnd="@layout/motion_01_cl_end" motion:duration="1000"> <OnSwipe motion:touchAnchorId="@+id/button" motion:touchAnchorSide="right" motion:dragDirection="dragRight" /> </Transition> </MotionScene> scene_01.xml
  23. MotionLayout Self contained MotionLayout <?xml version="1.0" encoding="utf-8"?> <MotionScene> <Transition motion:constraintSetStart="@+id/start"

    motion:constraintSetEnd="@+id/end" motion:duration="1000"> <OnSwipe motion:touchAnchorId="@+id/button" motion:touchAnchorSide="right" motion:dragDirection="dragRight" /> </Transition> <ConstraintSet android:id="@+id/start"> <Constraint android:id="@+id/button" android:layout_width="64dp" android:layout_height="64dp" android:layout_marginStart="8dp" motion:layout_constraintBottom_toBottomOf="parent" motion:layout_constraintStart_toStartOf="parent" motion:layout_constraintTop_toTopOf="parent" /> </ConstraintSet> <ConstraintSet android:id="@+id/end"> <Constraint android:id="@+id/button" android:layout_width="64dp" android:layout_height="64dp" android:layout_marginEnd="8dp" motion:layout_constraintBottom_toBottomOf="parent" motion:layout_constraintEnd_toEndOf="parent" motion:layout_constraintTop_toTopOf="parent" /> </ConstraintSet> </MotionScene>
  24. MotionLayout Advanced MotionLayout • KeyFrames • Nested MotionLayout • Image

    Transitions • Integrating MotionLayout with existing Layouts • And lots more
  25. Further Info • GoogleDevs series on MotionLayout starting here (https://medium.com/google-developers/introduction-t

    o-motionlayout-part-i-29208674b10d) • ConstraintLayout github example repo (https://github.com/googlesamples/android-Constraint LayoutExamples) • Google IO 2018 talk on ConstraintLayout and MotionLayout (https://www.youtube.com/watch?v=ytZteMo4ETk)