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 .
Let’s start by first creating a project in Android Studio. To do so, follow these instructions:
- 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
Here, we will use the RelativeLayout to get the Scroll View from the Kotlin file.
Update the strings.xml file
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.
Create Horizontal ScrollView in MainActivity.kt file
Open app/src/main/java/yourPackageName/MainActivity.kt. In this file, we declare a variable horizontalScrollView to create the Horizontal ScrollView widget like this:
val horizontalScrollView = HorizontalScrollView(this) //setting height and width val layoutParams = LinearLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT) horizontalScrollView.layoutParams = layoutParams
then, add the widget in layout using this
val linearLayout1 = findViewById(R.id.layout) linearLayout1?.addView(horizontalScrollView)
Run as Emulator:
Here, is the vedio of scrolling horizontally in the android application.
- Dynamic ScrollView in Kotlin
- ScrollView in Android
- Kotlin Data Types
- Hello World program in Kotlin
- Kotlin | Retrieve Collection Parts
- Destructuring Declarations in Kotlin
- DatePicker in Kotlin
- Kotlin labeled continue
- Introduction to Kotlin
- Kotlin Type Conversion
- Kotlin Exception Handling | try, catch, throw and finally
- Kotlin if-else expression
- Kotlin Environment setup for Command Line
- Kotlin constructor
- Kotlin Environment setup with Intellij IDEA
- Kotlin Nested class and Inner class
- Kotlin Variables
- Kotlin Operators
- Kotlin Standard Input/Output
- Kotlin Expression, Statement and Block
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.