Android Adventures - UI Graphics A Deep Dive
Written by Mike James   
Tuesday, 07 April 2015
Article Index
Android Adventures - UI Graphics A Deep Dive
Layout Properties And XML
Inflating Layouts
How To Build A UI

How To Build A UI?

You now have two distinct approaches to building a UI. You can do the whole job in code or you can create an XML layout. 

In practice it is usually easier to use the Android Studio designer to generate the XML file for you.

You can however mix the two approaches and change a UI "on the fly". For example you can load a UI by implicitly or explicitly inflating an XML file and then writing code to create and add other widgets or even remove View objects from the layout.

To remove View object you simply use the removeView or removeViewAt methods of the ViewGroup object.

There are other UI components such as menus that the designer doesn't support. In this case you have to work with the XML or create the UI in code. This means you do need to know the inner workings of the View hierarchy even though the designer is the easiest way to create 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 LayoutParam 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 LayoutParam class, set the parameters you want to determine and then set the instance as the LayoutParam 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 objects properties.

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

 

What more is there to learn?

Next we need to look at menus and some other interesting UI components. 

 

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, install the I Programmer Toolbar, subscribe to the RSS feed, follow us on, Twitter,FacebookGoogle+ or Linkedin,  or sign up for our weekly newsletter.

 

kotlin book

 

Comments




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

  



Last Updated ( Friday, 28 October 2016 )