Android Programming In Java: Programming the UI
Written by Mike James   
Monday, 03 June 2019
Article Index
Android Programming In Java: Programming the UI
View Group
Layout Properties
 

Programming Layout Properties

At the moment we are relying on default settings for the properties, and the layout properties in particular, of the View objects we are creating. However, in practice you could spend the time and lines of code to set all of the properties needed to create any user interface and layout you wanted to. 

You now know how to create a UI completely in code. All you have to do is create all of the objects you need, set their properties and add them to suitable layout objects. This is a little more complicated than you might think because each layout type has a different set of layout properties. Exactly how this is done is easy enough, but if you don't want to know about it at this stage you can skip ahead as it doesn't change any of the general principles.

As explained in the previous chapter, each type of Layout has an associated class derived from LayoutParams called layout.LayoutParams where layout is the name of the Layout class. For example, LinearLayout has the LinearLayout.LayoutParams class, which is used to define all of the layout properties that a View object can use when added to a LinearLayout object. 

You can probably guess how to make use of the LayoutParams class. All you do is create a correctly initialized instance of the appropriate LayoutParams class and use setLayoutParams on any View object you want to customize. 

For example, to set the height and width in a LayoutParams object we could use:

LinearLayout.LayoutParams LP=
new LinearLayout.LayoutParams(100,100);

There is a constructor for all of the LayoutParams classes that accepts just the width and the height properties. Once you have a LayoutParams object you can assign it to any View object by setting the View object’s LayoutParams property:

b3.setLayoutParams(LP);
linLayout.addView(b3);

With this change the third button in our previous layout will be exactly 100 by 100 pixels. 

Notice that the constructor works in pixels, i.e. px, instead of device-independent pixels, dp. You can also use constants for MATCH_PARENT and WRAP_CONTENT. For example:

LinearLayout.LayoutParams LP=
new LinearLayout.LayoutParams(WRAP_CONTENT,WRAP_CONTENT);

There is also a constructor that allows you to set the weight. Other layout properties have to be set using setProperty methods after the constructor has done its job. For example:

LP.setMargins(20,20,20,20);

which sets the left, top, right and bottom margins accordingly: 

More complex Layout objects have correspondingly more complex LayoutParams that you have to spend time setting up. 

So to be clear – there are properties such as Text that you set directly on the View object, but there are also Layout properties that you have to set on an appropriate LayoutParams object, which is then set as the View object's LayoutParam property. 

Not included in this extract:

  • The View Hierarchy
  • XML Layout
  • Inflation Theory
  • Finding View objects
  • How to Build a UI?

Summary

  • All of the UI components are derived from the View class.

  • An Activity can host and display a single instance of the View class set by one of its setContentView methods.

  • You can create instances of View objects in code and set them to be displayed by the Activity.

  • A Layout or ViewGroup object is a View object that can contain many View objects, so creating a sophisticated layout that the Activity can display.

  • Each Layout has its associated LayoutParams class, which is used by each View object it contains to control how it treats it within the layout. 

  • You generally have to create an instance of the LayoutParams class, set the parameters you want to determine, and then set the instance as the LayoutParams of each View object that needs to use it via the setLayoutParams method.

  • The use of Layout containers results in a View hierarchy, i.e. a hierarchy of View objects which are displayed by the Activity.

  • You can also code the View hierarchy using XML. Each XML tag corresponds to a View object and they are nested to define the hierarchy. The properties of the objects are set within the XML as attributes. Layout properties are treated in the same way but with layout_ as a prefix.

  • When the time comes for the View hierarchy to be assigned to the Activity, the XML file is converted into a nested set of View objects by the use of an inflater method. This simply reads the XML and converts each tag to an object and sets the object’s properties.

  • To find particular View objects in an inflated hierarchy the usual approach is to assign an id and use the findViewById method. 

 

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

 

 

  • If you want to see how to handle Events using the Kotlin language, see Chapter 4 in Android Programming in Kotlin: Starting With An App

 

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.

espbook

 

Comments




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

<ASIN:1871962552>

<ASIN:1871962544>



Last Updated ( Monday, 03 June 2019 )