In Android, a ScrollView is a view group that is used to make vertically scrollable views. A scroll view contains a single direct child only. In order to place multiple views in the scroll view, one needs to make a view group(like LinearLayout) as a direct child and then we can define many views inside it. A ScrollView supports Vertical scrolling only, so in order to create a horizontally scrollable view, HorizontalScrollView is used.
Some Important XML attributes of ScrollView
Attribute | Description |
---|
android:fillViewport | Defines whether the ScrollView should stretch its content to fill the viewport. |
android:measureAllChildren | Determines whether to measure all children or just those in the VISIBLE or INVISIBLE state when measuring. Defaults to false. |
android:alpha | alpha property of the view, as a value between 0 (completely transparent) and 1 (completely opaque). |
android:background | A drawable to use as the background. |
android:isScrollContainer | Set this if the view will serve as a scrolling container, meaning that it can be resized to shrink its overall window so that there will be space for an input method. |
android:minHeight | Defines the minimum height of the view. |
android:minWidth | Defines the minimum width of the view. |
android:scrollbars | Defines which scrollbars should be displayed on scrolling or not. |
Steps to Implement ScrollView in Android
This example demonstrates the steps involved to create a ScrollView in Android using Kotlin.
Step 1: Create a new project
- Click on File, then New => New Project.
- Choose “Empty Activity” for the project template.
- Select language as Kotlin/Java.
- Select the minimum SDK (According to the need).
Step 2: Modify activity_main.xml
Add the ScrollView and inside the ScrollView add a TextView to display a very long string to trigger the scroll.
activity_main.xml:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
tools:context=".MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="-127dp">
<TextView
android:id="@+id/scrolltext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24sp"
android:text="@string/sample_text"
android:textColor="@color/green"/>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
As already mentioned above, Scrollview can only contain one direct child. In this case, the child is TextView. On noticing this TextView you will realize that the text added inside TextView is mentioned as @string/sample_text which refers to a string resource inside the strings.xml file.
Step 3: MainActivity file in the Android Application
The MainActivity will be left untouched since it serves no purpose.
MainActivity.java
import android.os.Bundle;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
MainActivity.kt
package com.example.gfgapp_scrollview
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
Output:
Similar Reads
Zoom Scroll View in Android In this article, we are going to implement Zoom on ScrollView. Most of the time when we create a scroll view then it contains a lot of data and if we want to view any content on zooming then we can implement this feature. This feature can be useful when we are scrolling in an App and that contains d
3 min read
Horizontal Scrollview in Android ScrollView is a very essential part of Android App Development and can be seen in almost every Application. One of the most popular areas where its implementation can be seen is in the stories bar at the top of Instagram. In this article, we will learn how we can implement a Horizontal ScrollView in
2 min read
WheelView in Android In this article, WheelView is added in android. WheelView provides a very impressive UI that allows the developer to create a Wheel and add items according to his need. Some important tags provided by WheelView are wheelArcBackgroundColor, wheelAnchorAngle, wheelStartAngle, wheelMode, wheelCenterIco
3 min read
Croller in Android In this article, we will learn about how to add Croller in android. Croller is used to implement circular Seekbar in android. Seekbar is a type of progress bar. We can drag the seekbar from left to right and vice versa and hence changes the current progress. This is how a Croller look like. Table of
3 min read
Scroll ImageView in Android In this article, we are going to implement a very important feature related to the ImageView. The image will keep on scrolling horizontally by itself. When we click on the image then it will stop scrolling and when we again click it will again keep scrolling. We can use this feature to show animatio
3 min read
Android ViewPager in Kotlin ViewPager adds a sleek, swipeable interface that lets users effortlessly navigate between pages, just like flipping through a book. This feature is common in social media apps; for instance, WhatsApp greets you with three intuitive tabs: chats, status, and calls. This makes the app look cool. You've
5 min read