Android Adventures - Layouts 2.3
Written by Mike James   
Thursday, 19 March 2015
Article Index
Android Adventures - Layouts 2.3
Units
LinearLayout
RelativeLayout
ConstraintLayout
Summary

Summary

  • You can use different Layout containers to create UIs. Each Layout has its own facilities for how child controls are positioned and sized.

  • The most important are LinearLayout, RelativeLayout and FrameLayout.

  • Each Layout has its own set of Layout properties to control positioning and sizing of a control. Child controls have an instance of the Layout properties class to tell the Layout how to position and size them.

  • Layout properties are all prefixed with layout_ in the XML and written as layout: in the Properties window.

  • All Layouts support layout:width and layout:height.

  • You can specify position using a number of different units but in most cases use dp density independent pixels as this works the same way on screens of the same size but different resolutions.

  • As far as the Layout is concerned a control is just a rectangle wdith by height positioned using top and left.

  • All controls have padding properties which specify extra space around the control's content.

  • Some Layouts provide layout:margin properties that set extra space around the outside of the control. 

  • Gravity simply sets the simple positioning of an object - e.g. top, bottom, right, left. Every control has a gravity property which sets the position of its content - e.g. the text in a Button. Some Layout's have a layout:gravity property that sets how a control will be positioned.

  • The FrameLayout is the simplest of all Layouts and just has layout:gravity for positioning. In most cases it holds a single control and it is most often used as a placeholder.

  • The LinearLayout can be used to organize controls as a row or a column. 

  • Complex layouts can be created by nesting LinearLayouts inside each other to produce a column or rows or a row or columns. This has resulted in the LinearLayout being the most used.

  • As well as gravity, the LinearLayout also supports the specification of a control's weight. After the measured size of each control is determined by the Layout, the remaining unused space is allocated to the controls in the same proportions as their assigned weights.

  • The RelativeLayout is the most complex of the Layouts and can be used to create almost any UI. 

  • The general principle is to try to select a Layout that results in the smallest nesting of Layout containers. It is generally better to use a single RelativeLayout rather than deeply nested LinearLayouts.

  • The RelativeLayout can seem complicated or difficult to use in the Designer because of the way properties are set and unset as you move controls around. If you understand the principles then it is much easier to use. As you move a control the closest edge on another control or the RelativeLayout contain are set to be aligned and a margin property is set to the distance between them. 

  • You can resize controls using the Designer but only by dragging on an edge and aligning it with the same edge on another control. That is, you can make controls the same size but you cannot set a control to an arbitrary size.

  • The ConstraintLayout is an improved version of the RelativeLayout. It has tools that will apply constraints automatically for you but in practice the task needs some intelligence and manual constraints are nearly always better.

  • The ConstraintLayout can be used to create all types of layout without the need to nest layout controls. This flat approach to layout is more efficient.

  • The Designer simplifies the Layout properties displayed in the Properties window and maps them onto a bigger set of properties in the generated XML file. 

  • Start and end properties have been introduced to handle a layout direction specification. For a left-to-right layout start=left and end=right. For a right-to-left layout start=right and end=left.

 

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

 

Coming Next

We need to look in detail at the ConstraintLayout.

 

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

 

raspberry pi books

 

Comments




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



Last Updated ( Monday, 27 March 2017 )