<TableRow>
<Button android:id='@+id/cancel'
android:layout_column='2'
android:text='Cancel' />
<Button android:id='@+id/ok'
android:text='OK' />
</TableRow>
</TableLayout>
When compiled against the generated Java code and run on the emulator, we get the result shown in Figure 7-7.

Figure 7-7.
Scrollwork
Phone screens tend to be small, which requires developers to use some tricks to present a lot of information in the limited available space. One trick for doing this is to use scrolling, so only part of the information is visible at one time, and the rest is available via scrolling up or down.
ScrollView
is a container that provides scrolling for its contents. You can take a layout that might be too big for some screens, wrap it in a ScrollView
, and still use your existing layout logic. It just so happens that the user can see only part of your layout at one time; the rest is available via scrolling.
For example, here is a ScrollView
used in an XML layout file (from the Containers/Scroll
demo in the Source Code area of http://apress.com):
<?xml version='1.0' encoding='utf-8'?>
<ScrollView
xmlns:android='http://schemas.android.com/apk/res/android'
android:layout_width='fill_parent'
android:layout_height='wrap_content'>
<TableLayout
android:layout_width='fill_parent'
android:layout_height='fill_parent'
android:stretchColumns='0'>
<TableRow>
<View
android:layout_height='80px'
android:background='#000000'/>
<TextView android:text='#000000'
android:paddingLeft='4px'
android:layout_gravity='center_vertical' />
</TableRow>
<TableRow>
<View
android:layout_height='80px'
android:background='#440000' />
<TextView android:text='#440000'
android:paddingLeft='4px'
android:layout_gravity='center_vertical' />
</TableRow>
<TableRow>
<View
android:layout_height='80px'
android:background='#884400' />
<TextView android:text='#884400'
android:paddingLeft='4px'
android:layout_gravity='center_vertical' />
</TableRow>
<TableRow>
<View
android:layout_height='80px'
android:background='#aa8844' />
<TextView android:text='#aa8844'
android:paddingLeft='4px'
android:layout_gravity='center_vertical' />
</TableRow>
<TableRow>
<View
android:layout_height='80px'
android:background='#ffaa88' />
<TextView android:text='#ffaa88'
android:paddingLeft='4px'
android:layout_gravity='center_vertical' />
</TableRow>
<TableRow>
<View
android:layout_height='80px'
android:background='#ffffaa' />
<TextView android:text='#ffffaa'
android:paddingLeft='4px'
android:layout_gravity='center_vertical' />
</TableRow>
<TableRow>
<View
android:layout_height='80px'
android:background='#ffffff' />
<TextView android:text='#ffffff'
android:paddingLeft='4px'
android:layout_gravity='center_vertical' />
</TableRow>
</TableLayout>
</ScrollView>
Without the ScrollView
, the table would take up at least 560 pixels (7 rows at 80 pixels each, based on the View
declarations). There may be some devices with screens capable of showing that much information, but many will be smaller. The ScrollView
lets us keep the table as is, but present only part of it at a time.
On the stock Android emulator, when the activity is first viewed, you see what’s shown in Figure 7-8.
