Scroll View

A ScrollView is a container for another View that lets the user scroll that View vertically (a scrollbar is optional). A ScrollView often contains a LinearLayout, which in turn contains the Views that make up the form.

Don't confuse ScrollView with ListView. Both Views present the user with a scrollable set of Views, but the ListView is designed to display a set of similar things, such as the cheeses in the previous section. The ScrollView, on the other hand, allows an arbitrary View to scroll vertically. The Android documentation warns that one should never house a ListView within a ScrollView, because that defeats the performance optimizations of a ListView.

A ScrollView is a FrameLayout, which means that it can have only one child View. The most popular View for this purpose is a LinearLayout.

The following layout code from ApiDemos, scroll_view_2.xml, shows how to set up a ScrollView. The XML layout resource is sufficient; this example includes no extra Java code:

<ScrollView xmlns:android="" android:layout_width="fill_parent" android:layout_height="wrap content" android:scrollbars="none">Q <LinearLayout©

android:id="@+id/layout" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content">





<Button android:layout_width="fill_parent"



</LinearLayout> </ScrollView>

Here are some of the highlights of the code:

O The unnamed ScrollView fills the width of the screen and is as tall as it needs to be to contain all of its contents. It has no scrollbars, but that's not a problem, because scrollbars act only as visual queues in Android; they're not as important in Uls that scroll by flicking as opposed to mousing.

© The child view is a LinearLayout.

© The XML layout file has two controls within the LinearLayout: a TextView and a Button. The Java code that uses this layout creates 63 more buttons, to ensure that the example LinearLayout will be larger than the screen device and big enough to scroll.

Figure 11-7. The first tab of a TabHost ViewGroup


Content for tab with tag tab2

Figure 11-8. The second tab of a TabHost ViewGroup

0 0


    How to scroll contents in tabhost?
    7 years ago

Post a comment