Skip to content
Related Articles

Related Articles

HorizontalScrollView in Kotlin

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 18 Feb, 2021

In Android ScrollView 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 .

XML AttributesDescription
android:fillViewportIt defines whether the horizontal scrollview should stretch its content to fill the viewport.

android:layout_heightSets the height of the horizontal scroll view
android:layout_widthSets the width of the horizontal scroll view
android:srcSets background of the image
android:idSets unique id of the view

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 whatever you like
  • Then, select Kotlin language Support and click next button.
  • Select minimum SDK, whatever you need
  • Select Empty activity and then click finish.

Modify activity_main.xml file

<?xml version="1.0" encoding="utf-8"?>

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/mipmap folder.

Note: We have added the images to mipmap folder instead of drawable folder because the size of the images is very large.

Create HorizontalScrollView in MainActivity.kt file

Open app/src/main/java/yourPackageName/MainActivity.kt and do the following changes:

package com.geeksforgeeks.myfirstKotlinapp
import android.os.Bundle
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {

AndroidManifest.xml file

<?xml version="1.0" encoding="utf-8"?>
        <activity android:name=".MainActivity">
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>

Run as Emulator:

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!