Android ScrollView Tutorial With Example | Vertical ScrollView In Android

Hello and welcome to Android ScrollView Tutorial With Example.

There are some cases where you need to display so much vertical information that it can not be displayed in the screen of the device.

For example, you are showing the details about any restaurant. Here, first you will show a big rectangle photo of the restaurant, then the name, address, opening hours, food menu, pricing etc.

Screen of mobile device has not enough space to show all these information at once. Here, Scroll view will help you.

If you put all this information inside the scroll view, user will be able to scroll down or up vertically to show all the information.

Scroll view enable us to create vertical scrollable layout. For horizontal scrolling, we need to use Horizontal Scroll View.

ScrollView is a subclass of frame layout. As frame layout allows only one child, you can place only one child view in scroll view. Child of scroll view can contain entire view hierarchy.

For example, You can add linearlayout with vertical orientation as a child of scrollview. Inside this linearlayout, you can add as many user interface widgets as you want.

RecyclerView and ListView have scrolling effect by default. So you should never add both these elements to the scrollview. It will led to poor user interface performance and a poor user experience.

Similarly, in the child element of ListView or RecyclerView, it is advisable not to use ScrollView. Such thing will also confuse user about scrolling purpose.

You should use the scrollview when you have specific or static number of the children.

If you have dynamic number of children, something like result for “restaurants near me”.

Here, number of restaurants will be different in the different locations. In this case, you can use ListView or RecyclerView.

XML Property

Following is the XML representation of the scrollview.

Below are the properties associated with scroll view.

1. android:layout_width 

It will define the width of the scroll view. Generally, you should give value as match_parent so that it reduce the complexity for different screen size.

But you can also give fix size in the dp.

2. android:layout_height

You can set the height of the scroll view by using this property.

To give fix height to the scroll view, you can use dp here also.

3. android:id

Id will specify the unique identity to the scroll view.

When you want to access your scroll view in the java file, You will need this id in the findViewById() method.

This id should be unique in the whole android studio project to reduce complexity and data redundancy.

4. android:fillViewport

It defines whether the scrollview should stretch its content to fill the viewport.

This property accepts boolean value. If true then scroll view will stretch it’s content otherwise not.

For more details about this property, visit this tutorial.

Methods of ScrollView

Android gives some methods to programmatically control the scroll view from JAVA class.

Following are various methods.

1. addView(View child)

You can add a child view to your scroll view using this method.

2. isFillViewport()

Indicates whether this ScrollView’s content is stretched to fill the viewport.

This method will return the boolean value (true or false).

If fillViewport property is enabled then it will return true otherwise false.

3. onTouchEvent(MotionEvent ev)

Implement this method to handle touch screen motion events.

4. requestChildFocus(View child, View focused)

Called when a child of this parent wants focus.

By default, system has focus on the scroll view. Suppose you have edittext in the scroll view. Now you can use this method to request focus on this method to enter the text.

5. scrollTo(int x, int y)

Set the scrolled position of your view.

This version also clamps the scrolling to the bounds of our child.

6. setFillViewport(boolean fillViewport)

Indicates this ScrollView whether it should stretch its content height to fill the viewport or not.

You can pass boolean value (true or false) in the parameter of this method.

If you pass true then system will stretch its content height to fill the viewport.

7. setSmoothScrollingEnabled(boolean smoothScrollingEnabled)

Set whether arrow scrolling will animate its transition.

8. computeVerticalScrollRange()

The scroll range of a scroll view is the overall height of all of its children.

ScrollView Examples In Android Studio

Let us develop two projects in android studio to understand the scrollview practically.

In the first example, we will use plenty of buttons to make our view scrollable.

It will be simple example to understand the core elements in the scrollview.

So follow all the below steps to make first example.

Step 1. XML code

Make a new fresh project in the android studio.

When you are going through the process of making a new project in android studio, you will be asked to choose the type of the main activity.

Here, you need to select Empty Activity. Empty activity does not write any source code automatically, so that we can have complete clear and empty project.

Now in the new project, there are two files. activity_main.xml and

In the activity_main.xml file, you need to write down the following coding lines.

In this file, the root element is ConstraintLayout.

After this ConstraintLayout, I have taken the scroll view.

Now scroll view should have only one child. Here, we are going to develop vertical scroll view.

So I have taken a Linear Layout inside the scroll view.

Now inside this linear layout you add as many widgets as you want.

I have taken eleven buttons inside this linear layout.

Make sure that orientation property of linear layout has “vertical” value. (So that all the child elements will be set vertically)

Now because our linearlayout’s parent element is scroll view, android will make our linearlayout and it’s child elements scrollable.

Step 2. Main JAVA

We do not need to change anything in the Main file

Source code for is as the below

So after this much writings, you should get the following output

Second Example For ScrollView

First example includes only buttons in the scrollview.

In this example, we will see how we can put different views like imageview, textview, button etc. together inside the scroll view.

So create another brand new project in android studio with empty activity as the default activity.

Step 1. Changing XML Files

When you create a new project in android studio, you will have two files, activity_main.xml and

In the activity_main.xml file, add the following coding lines

Similar to the XML file of the first example, this file also have one scrollviw.

Inside this scrollview there is only element. It is linearlayout.

Orientation of linearlayout is vertical so that all the children widgets of this linearlayout are set in the vertical format.

This linearlayout has three elements. ImageView, TextView and Button.

Textview has long text such that it can not be written on the small screen of mobile device.

Imageview is above the textview and button is at the bottom of the textview.

Thus android will enable the scroll for this layout.

Step 2. Main Code

We do not need to change anything in the file.

Source code for file is as the following

Output of this file can be see in the below video.


Hardik Parsania

I am mobile app developer. I am always passionate about solving complex and real world problems.I have developed many apps with great interest and superior logical skills.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *