Android Adventures - The ConstraintLayout
Written by Mike James   
Monday, 27 March 2017
Article Index
Android Adventures - The ConstraintLayout
Manual Constraints
Chains
Guidelines
Constraint Properties

Constraint Properites

As you might guess, there are properties very similar to those used by the RelativeLayout which set the contstraints. 

To see all of the properties you have to click on the view all properties icon at the top right of the properties window - the double arrow.

For example, if you look in the Properties window you will see:

layout_constraintLeft_toLeftOf  

which gives the id of the control that the left side of this control will be aligned its left side. There are similar constraints for the other possiblities - for example

layout_constrainBottom_toBottomOf

and so on.

It is also worth understanding that in allowing you to set the position of a component by simply dragging it to the location you want the Designer is working out how to set multiple properties correctly. You could do this manually to get the same effect but the Designer does it simply from where you have positioned a component.

This is why it is easier to let the Designer set the properties for you. 

However all this said it is very easy to get into a complete mess with relative layout in the Designer. If components go missing then chances are they are on top of each other. The easiest way to sort this problem out is to go to the Properties window and manually reset one of the positioning properties. 

It is helpful to notice the following:  

  • Use positioning relative to another component if it makes logical sense. That is if you have a text entry component then it make sense to position its accept Button relative to its righthand side.
     
  • If you position everything relative to the container parent then you effectively have an absolute layout.
     
  • If the screen size changes then it is possible that components will overlap one another if there isn't enough space. Always make your layouts have plenty of unnecessary space.
     
  • A good strategy when working with a group of components is to pick one that you position relative to the container then position all other components relative to it to ensure that you can move the group and keep alignments.
  • Remember that some components can change their size as well as location and this can modify the position of components positioned relative to them. 

 

The ConstraintLayout is a way of creating reactive layouts that adjust to the size of the screen. However doing this effectively is difficult. The amount of intelligence required goes well beyond what the constraint inference tool has. It may even be beyond a human. A simpler but more work intensive way is to provide a separate layout for radically different screen sizes and use constraints to make small adjustments. 

 Summary

  • The ConstraintLayout is the layout of the future, the new Designer was created to work best with it.

  • There are two automatic constraint tools – Autoconnect which works out constraints for a single component, and Infer Constraints which works out any missing constraints for the entire layout. Neither tool is currently particularly useful.

  • You can clear all of the constraints in a layout, or just those on a single component or a constraint at a time.

  • Constraints can be applied from a component to the parent container and these act like absolute positioning.

  • You can set default margins to make positioning components more regular.

  • Components can be positioned relative to another component.

  • You cannot set negative margins.

  • You can align text baselines.

  • Bias constraints allow you to set positions that divide the space available in a specified proportion.

  • Chains provide some of the features of a LinearLayout. For example, you can use a chain to distribute components across the available space.

  • Guidelines can be added to a layout and components can be positioned relative to them, even though they don’t appear in the final layout.

  • You can set the size of a component absolutely or to be determined by the content. In addition, you can allow a pair of constraints to determined the size.

  • An Aspect Ratio Constraint can set one dimension as ratio of another.

androidJavaSmallAndroid Programming In Java:
Starting With an App
Third Edition

Is now available in paperback and ebook.

Available from Amazon.

 

 

  1. Getting Started With Android Studio 3
  2. The Activity And The UI
  3. Building The UI and a Calculator App
  4. Android Events
         Extract: Using Lambdas
  5. Basic Controls
  6. Layout Containers
  7. The ConstraintLayout
        Extract: Guidelines and Barriers
  8. UI Graphics A Deep Dive
        Extract: Programming the UI ***NEW
  9. Menus & The Action Bar
  10. Menus, Context & Popup
  11. Resources
  12. Beginning Bitmap Graphics
        Extract: Simple Animation
  13. Staying Alive! Lifecycle & State
  14. Spinners
  15. Pickers
  16. ListView And Adapters

If you are interested in creating custom template also see:

Custom Projects In Android Studio

Androidgears

 

 

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

raspberry pi books

 

Comments




or email your comment to: comments@i-programmer.info

androidJavaSmallAndroid Programming In Java:
Starting With an App
Third Edition

Is now available in paperback and ebook.

Available from Amazon.

 

 

  1. Getting Started With Android Studio 3
  2. The Activity And The UI
  3. Building The UI and a Calculator App
  4. Android Events
         Extract: Using Lambdas
  5. Basic Controls
  6. Layout Containers
  7. The ConstraintLayout
        Extract: Guidelines and Barriers
  8. UI Graphics A Deep Dive
        Extract: Programming the UI ***NEW
  9. Menus & The Action Bar
  10. Menus, Context & Popup
  11. Resources
  12. Beginning Bitmap Graphics
        Extract: Simple Animation
  13. Staying Alive! Lifecycle & State
  14. Spinners
  15. Pickers
  16. ListView And Adapters

If you are interested in creating custom template also see:

Custom Projects In Android Studio

Androidgears

 



Last Updated ( Monday, 27 March 2017 )