Open In App

ScrollView in Android

Last Updated : 28 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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:fillViewportDefines whether the ScrollView should stretch its content to fill the viewport.
android:measureAllChildrenDetermines whether to measure all children or just those in the VISIBLE or INVISIBLE state when measuring. Defaults to false.
android:alphaalpha property of the view, as a value between 0 (completely transparent) and 1 (completely opaque).
android:backgroundA drawable to use as the background. 
android:isScrollContainerSet 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:minHeightDefines the minimum height of the view.
android:minWidthDefines the minimum width of the view.
android:scrollbarsDefines 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

  1. Click on File, then New => New Project.
  2. Choose “Empty Activity” for the project template.
  3. Select language as Kotlin/Java.
  4. 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: 



Next Article

Similar Reads