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

Constraint Layout and Constraint sets

Saurabh
August 25, 2018

Constraint Layout and Constraint sets

Constraint Layout is a new type of layout available in the Android Support repository for building flexible and efficient layouts. The Layout Editor uses constraints to determine the position of a UI element within the layout.

Learn how you can use Constraint Layout to build beautiful layouts with a flat hierarchy. We'll also explore Constraint Sets, which allow you to build smooth transition by using multiple Constraint Layout files.

Saurabh

August 25, 2018
Tweet

More Decks by Saurabh

Other Decks in Technology

Transcript

  1. Constraint Layout Constraint Layout RelativeLayout supercharged to the next level

    Apply constraints with respect to each other Helper views such as Barriers, Groups 2
  2. Constraint Layout Constraint Layout RelativeLayout supercharged to the next level

    Apply constraints with respect to each other Helper views such as Barriers, Groups Animation Support 2
  3. Constraint Layout Constraint Layout RelativeLayout supercharged to the next level

    Apply constraints with respect to each other Helper views such as Barriers, Groups Animation Support Keep a flat hierarchy of views 2
  4. Percent Dimension Percent Dimension android:layout_height="0dp" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent" android:layout_width="0dp" android:layout_height="0dp" app:layout_constraintTop_toTopOf="parent"

    app:layout_constraintBottom_toBottomOf="parent" android:layout_width="0dp" android:layout_height="0dp" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintWidth_percent="0.5" 14
  5. Percent Dimension Percent Dimension android:layout_height="0dp" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent" android:layout_width="0dp" android:layout_height="0dp" app:layout_constraintTop_toTopOf="parent"

    app:layout_constraintBottom_toBottomOf="parent" android:layout_width="0dp" android:layout_height="0dp" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintWidth_percent="0.5" 14
  6. Max and Min Size Max and Min Size android:layout_width="0dp" android:layout_height="0dp"

    app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintWidth_max="150dp" 15
  7. Max and Min Size Max and Min Size android:layout_width="0dp" android:layout_height="0dp"

    app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintWidth_max="150dp" 15
  8. <ImageView android:id="@+id/BlueBlock" android:layout_width="0dp" android:layout_height="200dp" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" /> <ImageView android:id="@+id/greenBlock" android:layout_width="50dp"

    android:layout_height="50dp" app:layout_constraintEnd_toEndOf="@id/BlueBlock"/> <ImageView android:id="@+id/BlueBlock" android:layout_width="0dp" android:layout_height="200dp" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" /> <ImageView android:id="@+id/greenBlock" android:layout_width="50dp" android:layout_height="50dp" app:layout_constraintEnd_toEndOf="@id/BlueBlock" app:layout_constraintTop_toBottomOf="@id/BlueBlock" app:layout_constraintBottom_toBottomOf="@id/BlueBlock"/> Trick with constraints Trick with constraints 16
  9. Visiblity Constraint Visiblity Constraint <ImageView android:id="@+id/BlueBlock" android:layout_width="50dp" android:layout_height="50dp" android:layout_marginLeft="8dp" app:layout_constraintStart_toStartOf="parent"

    /> <ImageView android:id="@+id/greenBlock" android:layout_width="50dp" android:layout_height="50dp" android:layout_marginLeft="16dp" app:layout_constraintLeft_toRightOf="@id/BlueBlock" /> 17
  10. Visiblity Constraint Visiblity Constraint <ImageView android:id="@+id/BlueBlock" android:layout_width="50dp" android:layout_height="50dp" android:layout_marginLeft="8dp" app:layout_constraintStart_toStartOf="parent"

    /> <ImageView android:id="@+id/greenBlock" android:layout_width="50dp" android:layout_height="50dp" android:layout_marginLeft="16dp" app:layout_constraintLeft_toRightOf="@id/BlueBlock" /> A B 17
  11. Visiblity Constraint Visiblity Constraint <ImageView android:id="@+id/BlueBlock" android:layout_width="50dp" android:layout_height="50dp" android:layout_marginLeft="8dp" app:layout_constraintStart_toStartOf="parent"

    /> <ImageView android:id="@+id/greenBlock" android:layout_width="50dp" android:layout_height="50dp" android:layout_marginLeft="16dp" app:layout_constraintLeft_toRightOf="@id/BlueBlock" /> A B <ImageView android:id="@+id/BlueBlock" android:layout_width="50dp" android:layout_height="50dp" android:layout_marginLeft="8dp" android:visibility="gone" app:layout_constraintStart_toStartOf="parent" /> <ImageView android:id="@+id/greenBlock" android:layout_width="50dp" android:layout_height="50dp" android:layout_marginLeft="16dp" app:layout_constraintLeft_toRightOf="@id/BlueBlock" /> B 17
  12. Visiblity Constraint Visiblity Constraint <ImageView android:id="@+id/BlueBlock" android:layout_width="50dp" android:layout_height="50dp" android:layout_marginLeft="8dp" app:layout_constraintStart_toStartOf="parent"

    /> <ImageView android:id="@+id/greenBlock" android:layout_width="50dp" android:layout_height="50dp" android:layout_marginLeft="16dp" app:layout_constraintLeft_toRightOf="@id/BlueBlock" /> A B <ImageView android:id="@+id/BlueBlock" android:layout_width="50dp" android:layout_height="50dp" android:layout_marginLeft="8dp" android:visibility="gone" app:layout_constraintStart_toStartOf="parent" /> <ImageView android:id="@+id/greenBlock" android:layout_width="50dp" android:layout_height="50dp" android:layout_marginLeft="16dp" app:layout_constraintLeft_toRightOf="@id/BlueBlock" /> B <ImageView android:id="@+id/BlueBlock" android:layout_width="50dp" android:layout_height="50dp" android:layout_marginLeft="8dp" android:visibility="gone" app:layout_constraintStart_toStartOf="parent" /> <ImageView android:id="@+id/greenBlock" android:layout_width="50dp" android:layout_height="50dp" android:layout_marginLeft="16dp" app:layout_goneMarginLeft="8dp" app:layout_constraintLeft_toRightOf="@id/BlueBlock" /> B 17
  13. Guideline Guideline Just a vertical or horizontal line Marked as

    Gone so not really shown to the user Acts as a helper to place views 21
  14. Guideline Guideline Just a vertical or horizontal line Marked as

    Gone so not really shown to the user Acts as a helper to place views Can be in percent or fixed value 21
  15. Guideline Guideline Just a vertical or horizontal line Marked as

    Gone so not really shown to the user Acts as a helper to place views Can be in percent or fixed value <android.support.constraint.Guideline android:id="@+id/guideline" app:layout_constraintGuide_begin="100dp" android:orientation="vertical"/> <Button android:text="Button" app:layout_constraintLeft_toLeftOf="@+id/guideline" app:layout_constraintTop_toTopOf="parent" /> 21
  16. Group Group Group views together Control their visibility Views can

    belong to more than one group Last one in XML order wins 22
  17. Group Group Group views together Control their visibility Views can

    belong to more than one group Last one in XML order wins <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" /> 22
  18. Constraint Set Constraint Set Defines the set of constraints applied

    to views Can be applied to a ConstraintLayout 24
  19. Constraint Set Constraint Set Defines the set of constraints applied

    to views Can be applied to a ConstraintLayout Can be used to create animations 24
  20. <!-- activity_main.xml --> <android.support.constraint.ConstraintLayout ...> <ImageView android:id="@+id/image" android:layout_width="0dp" android:layout_height="wrap_content" app:layout_constraintLeft_toLeftOf="parent"

    app:layout_constraintRight_toRightOf="parent" ... /> <Button ... /> </android.support.constraint.ConstraintLayout> Constraint Set Constraint Set 25
  21. <!-- activity_main.xml --> <android.support.constraint.ConstraintLayout ...> <ImageView android:id="@+id/image" android:layout_width="0dp" android:layout_height="wrap_content" app:layout_constraintLeft_toLeftOf="parent"

    app:layout_constraintRight_toRightOf="parent" ... /> <Button ... /> </android.support.constraint.ConstraintLayout> <!-- activity_main_alt.xml --> <android.support.constraint.ConstraintLayout ...> <ImageView android:id="@+id/image" android:layout_width="0dp" android:layout_height="wrap_content" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent" ... /> <Button ... /> </android.support.constraint.ConstraintLayout> Constraint Set Constraint Set 25
  22. <!-- activity_main.xml --> <android.support.constraint.ConstraintLayout ...> <ImageView android:id="@+id/image" android:layout_width="0dp" android:layout_height="wrap_content" app:layout_constraintLeft_toLeftOf="parent"

    app:layout_constraintRight_toRightOf="parent" ... /> <Button ... /> </android.support.constraint.ConstraintLayout> <!-- activity_main_alt.xml --> <android.support.constraint.ConstraintLayout ...> <ImageView android:id="@+id/image" android:layout_width="0dp" android:layout_height="wrap_content" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent" ... /> <Button ... /> </android.support.constraint.ConstraintLayout> Constraint Set Constraint Set override fun onCreate(savedInstanceState: Bundle?) { ... val constraintSet1 = ConstraintSet() constraintSet1.clone(constraintLayout) val constraintSet2 = ConstraintSet() constraintSet2.clone(this, R.layout.activity_main_alt) var changed = false findViewById(R.id.button).setOnClickListener { TransitionManager.beginDelayedTransition(constraintLayout) val constraint = if (changed) constraintSet1 else constraintSet2 constraint.applyTo(constraintLayout) changed = !changed } } 25
  23. <!-- activity_main.xml --> <android.support.constraint.ConstraintLayout ...> <ImageView android:id="@+id/image" android:layout_width="0dp" android:layout_height="wrap_content" app:layout_constraintLeft_toLeftOf="parent"

    app:layout_constraintRight_toRightOf="parent" ... /> <Button ... /> </android.support.constraint.ConstraintLayout> <!-- activity_main_alt.xml --> <android.support.constraint.ConstraintLayout ...> <ImageView android:id="@+id/image" android:layout_width="0dp" android:layout_height="wrap_content" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent" ... /> <Button ... /> </android.support.constraint.ConstraintLayout> Constraint Set Constraint Set override fun onCreate(savedInstanceState: Bundle?) { ... val constraintSet1 = ConstraintSet() constraintSet1.clone(constraintLayout) val constraintSet2 = ConstraintSet() constraintSet2.clone(this, R.layout.activity_main_alt) var changed = false findViewById(R.id.button).setOnClickListener { TransitionManager.beginDelayedTransition(constraintLayout) val constraint = if (changed) constraintSet1 else constraintSet2 constraint.applyTo(constraintLayout) changed = !changed } } 25
  24. Constraint Set - Duplicate Constraint Set - Duplicate XML? XML?

    Omit non styling attributes from transition XML 26
  25. Constraint Set - Duplicate Constraint Set - Duplicate XML? XML?

    Omit non styling attributes from transition XML Constraint set works on constraints and not styles 26
  26. Constraint Set - Duplicate Constraint Set - Duplicate XML? XML?

    Omit non styling attributes from transition XML Constraint set works on constraints and not styles Create constraint set in code 26