⏬Scrolling views
Last updated
Last updated
© 2024 ~ Yunus Emre Ak ~ yEmreAk
To display a scrollable magazine article on the screen, you might use a RelativeLayout
that includes a separate TextView
for the article heading, another for the article subheading, and a third TextView
for the scrolling article text (see the figure below), set within a ScrollView
. The only part of the screen that would scroll would be the ScrollView
with the article text.
A ScrollView
can contain only one child View
; however, that View
can be a ViewGroup
that contains several View
elements, such as LinearLayout
. You can nest a ViewGroup
such as LinearLayout
within the ScrollView
, thereby scrolling everything that is inside the LinearLayout
.
For example, if you want the subheading of an article to scroll along with the article even if they are separate TextView
elements, add a LinearLayout
to the ScrollView
as a single child View
as shown in the figure below, and then move the TextView
subheading and article elements into the LinearLayout
. The user scrolls the entire LinearLayout
which includes the subheading and the article.
When adding a LinearLayout
inside a ScrollView
, use match_parent
for the LinearLayout
android:layout_width
attribute to match the width of the parent ScrollView
, and use wrap_content
for the LinearLayout
android:layout_height
attribute to make it only large enough to enclose its contents.
Since ScrollView
only supports vertical scrolling, you must set the LinearLayout
orientation attribute to vertical (android:orientation="vertical"
), so that the entire LinearLayout
will scroll vertically. For example, the following XML layout scrolls the article
TextView
along with the article_subheading
TextView
: