Android Tutorial

(avery) #1
Android Tutorial 225

Two different methods to create a screen have the same
result.

First, in the XML layout files, layout_width
and layout_height are required attributes.
Next, you see that each TextView object
has a unique id property assigned so that
it can be accessed programmatically at
runtime. Finally, the textSize property
needs to have its units defined. The XML
attribute takes a dimension type instead
of a float.

The end result differs only slightly from
the programmatic method. However, it’s
far easier to read and maintain. Now you
need only one line of code to display this< layout view. Again, if
the layout resource is stored in the
/res/layout/resource_based_layout.xml file, that is
setContentView(R.layout.resource_based_layout);

Organizing Your User Interface

In Chapter “Exploring User Interface Screen Elements,” we talk
about how the class View is the building block for user interfaces in
Android. All user interface controls, such as Button, Spinner, and
EditText, derive from the View class.
Free download pdf