0% found this document useful (0 votes)
4 views

ScrollView

The document explains the use of ScrollView in Android for adding vertical and horizontal scroll bars to layouts that exceed the screen size. It highlights that ScrollView can only contain one direct child and should not be used with ListView or GridView, which manage their own scrolling. Additionally, it provides examples of both ScrollView and HorizontalScrollView implementations in XML format.

Uploaded by

ff.ssg227
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

ScrollView

The document explains the use of ScrollView in Android for adding vertical and horizontal scroll bars to layouts that exceed the screen size. It highlights that ScrollView can only contain one direct child and should not be used with ListView or GridView, which manage their own scrolling. Additionally, it provides examples of both ScrollView and HorizontalScrollView implementations in XML format.

Uploaded by

ff.ssg227
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Android ScrollView (Horizontal, Vertical)

 In android, ScrollView is a kind of layout that is useful to add vertical or


horizontal scroll bars to the content which is larger than the actual size
of layouts such as linearlayout, relativelayout, framelayout, etc.
 Generally, the android ScrollView is useful when we have content that
doesn’t fit our android app layout screen. The ScrollView will enable a
scroll to the content which is exceeding the screen layout and allow users to
see the complete content by scrolling.
 The android ScrollView can hold only one direct child. In case, if we want
to add multiple views within the scroll view, then we need to include them
in another standard layout like linearlayout, relativelayout, framelayout, etc.
 To enable scrolling for our android applications, ScrollView is the best
option but we should not use ScrollView along
with ListView or Gridview because they both will take care of their own
vertical scrolling.
 In android, ScrollView supports only vertical scrolling. In case, if we want
to implement horizontal scrolling, then we need to use
a HorizontalScrollView component.
 The android ScrollView is having a property called android:fillViewport,
which is used to define whether the ScrollView should stretch it’s content to
fill the viewport or not.
 Example

Android ScrollView Example

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/andro
id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="false">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/and
roid"
android:orientation="vertical" android:layout_width="match_pare
nt"
android:layout_height="match_parent">
<TextView android:id="@+id/loginscrn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="80dp"
android:text="ScrollView"
android:textSize="25dp"
android:textStyle="bold"
android:layout_gravity="center"/>
<TextView android:id="@+id/fstTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Welcome to Tutlane"
android:layout_gravity="center"/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="60dp"
android:text="Button One" />
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="60dp"
android:text="Button Two" />
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="60dp"
android:text="Button Three" />
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="60dp"
android:text="Button Four" />
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="60dp"
android:text="Button Five" />
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="60dp"
android:text="Button Six" />
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="60dp"
android:text="Button Seven" />
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="60dp"
android:text="Button Eight" />
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="60dp"
android:text="Button Nine" />
</LinearLayout>
</ScrollView>

Android HorizontalScrollView Example


Now open activity_main.xml file in your android application and write the code like as
shown below.

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk
/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/and
roid"
android:orientation="horizontal" android:layout_width="match_pa
rent"
android:layout_height="match_parent"
android:layout_marginTop="150dp">
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button One" />
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button Two" />
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button Three" />
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button Four" />
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button Five" />
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button Six" />
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button Seven" />
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button Eight" />
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button Nine" />
</LinearLayout>
</HorizontalScrollView>

You might also like