Open In App

HorizontalScrollView in Kotlin

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

ScrollView in Android allows multiple views that are places within the parent view group to be scrolled. Scrolling in the android application can be done in two ways either Vertically or Horizontally. In this article, we will be discussing how to create a Horizontal ScrollView in Kotlin.

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.

Let’s start by first creating a project in Android Studio. To do so, follow these instructions:

First step is to create a new Project in Android Studio. For this follow these steps:

  • Click on File, then New and then New Project and give name.
  • Then, select Kotlin language Support and click next button.
  • Select minimum SDK (According to the need).
  • Select Empty activity and then click finish.
  • Do the following changes in the activity_main.xml file

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">

    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:orientation="horizontal">


            <ImageView
                android:id="@+id/image1"
                android:layout_width="200dp"
                android:layout_height="match_parent"
                android:layout_marginEnd="20dp"
                android:src="@drawable/gfg_logo" />

            <ImageView
                android:id="@+id/image2"
                android:layout_width="200dp"
                android:layout_height="match_parent"
                android:layout_marginEnd="20dp"
                android:src="@drawable/gfg_logo" />

            <ImageView
                android:id="@+id/image3"
                android:layout_width="200dp"
                android:layout_height="match_parent"
                android:layout_marginEnd="20dp"
                android:src="@drawable/gfg_logo" />

            <ImageView
                android:id="@+id/image4"
                android:layout_width="200dp"
                android:layout_height="match_parent"
                android:layout_marginEnd="20dp"
                android:src="@drawable/gfg_logo" />


        </LinearLayout>
    </HorizontalScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>

Add Images

We need to add some images which can be used for scrolling purpose. So, we have to copy the images from our local computer path to app/res/drawable folder.

Note: You can add the images in the mipmap folder instead of drawable folder if the size of the images is very large.

Output:


Next Article

Similar Reads