VOOZH about

URL: https://www.geeksforgeeks.org/kotlin/dynamichorizontal-scrollview-in-kotlin/

⇱ Dynamic Horizontal ScrollView in Kotlin - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Dynamic Horizontal ScrollView in Kotlin

Last Updated : 12 Jul, 2025

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 programmatically create a Horizontal ScrollView in Kotlin .

Step by Step Implementation

Step 1: Create a new project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.

Step 2: Modify activity_main.xml file

Second step is to design our layout page. Here, we will use the LinearLayout to get the Horizontal Scroll View from the Kotlin file.

activity_main.xml:


Step 3: Create Scroll View in MainActivity.kt file

Open app > src > main > java > {package-name} > MainActivity.kt. In this file, we declare a variable ScrollView to create the Scroll View widget like this:

val horizontalScrollView = HorizontalScrollView(this)
val layoutParams = LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
)
horizontalScrollView.layoutParams = layoutParams

then add the widget in layout using this

linearLayout.addView(horizontalScrollView)

MainActivity.kt:

Output:

Comment
Article Tags:

Explore