Dynamic ScrollView in Kotlin
In Android ScrollView incorporates multiple views within itself and allows them to be scrolled.
In this article we will be discussing how to programmatically create a Scroll view 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
Second step is to design our layout page. Here, we will use the LinearLayout to get the Scroll View from the Kotlin file.
We will be needing some images to be used in application. You can use the images that you like but the images need to be copied from our local computer path to app/res/drawable folder.
Create Scroll View in MainActivity.kt file
Open app/src/main/java/yourPackageName/MainActivity.kt. In this file, we declare a variable ScrollView to create the Scroll View widget like this:
val scrollView = ScrollView(this) val layoutParams = LinearLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT) scrollView.layoutParams = layoutParams
then add the widget in layout using this