Android ScrollView


Download Android ScrollView sample

Android ScrollView must be used when app content might be longer than device height. Android ScrollView makes content vertically scrollable.

Android ScrollView can have only one direct child so if there are more than one child then each child must be wraped in a container like Layout.

In this tutorial, we are going to discuss following things:

  1. Android ScrollView usage
  2. Xml attributes of Android ScrollView
  3. Customization of Android ScrollView

Android ScrollView Usage?


Android ScrollView simple usage in xml layout is given below:

Android ScrollView usage

How to use android:fillViewport?


android:fillViewport specify whether the scrollview should stretch its content to fill the viewport or not. It can have true or false as its value.

android:fillViewport to true means this will make the subviews occupy the whole space of the ScrollView

android:scrollbarTrackVertical attribute of Android ScrollBar?


android:scrollbarTrackVertical attribute of Android ScrollBar define the vertical scrollbar track drawable.

android:scrollbarThumbVertical attribute of Android ScrollBar?


android:scrollbarThumbVertical attribute of Android ScrollBar define the vertical scrollbar thumb drawable.

android:scrollbars attribute of Android ScrollBar?


android:scrollbars attribute of Android ScrollBar define which scrollbars should be displayed on scrollbar. It can have one or more than one values among none, horizontal, vertical separated by ‘|’.

android:scrollbarFadeDuration attribute of Android ScrollBar?


android:scrollbarFadeDuration attribute of Android ScrollBar define the delay in milliseconds that a scrollbar takes to fade out. Must be an integer value in milisecond.

android:scrollbarDefaultDelayBeforeFade attribute of Android ScrollBar?


android:scrollbarDefaultDelayBeforeFade attribute of Android ScrollBar define delay in milliseconds that a scrollbar waits before fade out. Must be an integer value in milisecond.

android:scrollbarAlwaysDrawVerticalTrack attribute of Android ScrollBar?


android:scrollbarAlwaysDrawVerticalTrack attribute of Android ScrollBar define whether the vertical scrollbar track should always be drawn. It can have true or false as its value.

android:scrollbarAlwaysDrawHorizontalTrack attribute of Android ScrollBar?


android:scrollbarAlwaysDrawHorizontalTrack attribute of Android ScrollBar define whether the horizontal scrollbar track should always be drawn. It can have true or false as its value.

android:scrollbarThumbHorizontal attribute of Android ScrollBar?


android:scrollbarThumbHorizontal attribute of Android ScrollBar define the horizontal scrollbar thumb drawable.

android:scrollbarTrackHorizontal attribute of Android ScrollBar?


android:scrollbarTrackHorizontal attribute of Android ScrollBar define the horizontal scrollbar track drawable.

How to customize Android ScrollBar track and thumb?


We can define ScrollBar track and thumb in drawable xml.

ScrollBar track drawable I used for this sample is given below:

track.xml

ScrollBar thumb drawable I used for this sample is given below:

thumb.xml

Now, set android:scrollbarThumbVertical and android:scrollbarTrackVertical of ScrollBar in res->values->styles.xml as follows:

Set the style attribute of ScrollBar where you want to add this custom style as follows:

Android ScrollBar demo

Android ScrollView demo

In this example, We discussed different aspect of Android ScrollBar. Now lets implement the demo for Android ScrollBar

Step 1: Create a new project in Android Studio and name it Android ScrollBar.

Step 2: Open res -> values ->colors.xml and replace its code with below given code:

colors.xml

Step 3: Open res -> values ->strings.xml and replace its code with below given code:

strings.xml

Step 4: Open res -> drawable and add a file track.xml, replace its code with below given code:

track.xml

Step 5: Open res -> drawable and add a file thumb.xml, replace its code with below given code:

thumb.xml

Step 6: Open res -> values ->styles.xml and replace its code with below given code:

styles.xml

Step 7: Open res -> layout -> activity_main.xml and add following code

activity_main.xml

Step 8: Open your MainActivity.java and replace its source with source of my MainActivity.java as follows:

MainActivity.java

Step 9: Clean and run the project to see the result.


Share & Like

Tutorial topics

About ApnaTutorials.com

ApnaTutorials.com is for Java, J2ee, Html5 and android developers. All tutorials are well explained and are easy to understand. We have tested every tutorials on our developement environment before posting it in public domain.