![]() |
VOOZH | about |
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 .
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.
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:
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: