Developing the User Interface
Okay, it’s time to start developing the user interface. First make sure that you’re in XML view of your layout by clicking the activity_main.xml
tab. When you’re in XML view, delete the XML and replace it with the following. Your layout should now look like this:
<?xml version=”1.0” encoding=”utf-8”?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:orientation=”vertical”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
>
</LinearLayout>
Viewing XML layout attributes
Before continuing, you need to understand the attributes of the Android layout XML you’re working with. See Table 4-3.
Table 4-3 XML Layout Attributes
Layout |
What It Does |
|
Defines the XML namespace that you use to reference part of the Android SDK |
|
Informs Android that this view will be laid out vertically, such as in the portrait format in printing |
|
Informs the view that it should fill as much horizontal space as it can, up to its parent, to make its own width the same as its parent’s |
|
Informs the view that it should fill as much vertical space as it can, up to its parent, to make its own height the same as its parent’s |
At this point, you have defined the layout to fill the entire screen by setting the width and height to “match_parent”
.
Working with views
As stated earlier in this chapter, views in Android are the basic building blocks of user interface components. Anytime you implement a user interface component, such as a Layout
or TextView
, in the Android system, you’re using a view. When you work with views in Java, you have to cast them to their appropriate type to be able to work with them.
Setting layout_width and layout_height values
Before a view can be presented to the screen, a couple of settings must be configured on the view so that Android knows how to layout the view on the screen. The attributes that are required, layout_width
and layout_height
, are known as LayoutParams
in the Android SDK.
The layout_width
attribute specifies the given width of a view, and the layout_height
attribute specifies the given height of a view.
Setting match_parent and wrap_content values
The layout_width
and layout_height
attributes can take any pixel value or density-independent pixel value to specify their respective dimensions. However, two of the most common values for layout_width
and layout_height
are match_parent
and wrap_content
constants.
The match_parent
value informs the Android system to fill as much space as possible on screen based on the available space of the parent layout. The wrap_content
value informs the Android system to occupy only as much space as needed to show the view. As the view’s contents grow, as would happen with a TextView
, the view’s viewable space grows, similar to the Autosize
property in Windows forms development.
If you’re using a static layout, these two attributes must be set in the XML layout. If you’re creating views dynamically via code, the layout parameters must be set via Java code. Either way, you cannot be without them. To find out more about dynamic creation of views, see the API samples that come with the Android SDK.