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.
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 Scroll View from the Kotlin file.
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
</LinearLayout>
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 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
linearLayout.addView(scrollView)MainActivity.kt:
package org.geeksforgeeks.demo
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import android.view.Gravity
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.ScrollView
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val scrollView = ScrollView(this)
val scrollParams = LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT)
scrollView.layoutParams = scrollParams
val linearLayout = LinearLayout(this)
val linearParams = LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT)
linearLayout.orientation = LinearLayout.VERTICAL
linearLayout.layoutParams = linearParams
scrollView.addView(linearLayout)
val params =LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT
)
params.setMargins(32, 32, 32, 32)
val imageView1 = ImageView(this)
imageView1.setLayoutParams(params)
imageView1.scaleType = ImageView.ScaleType.CENTER_CROP
imageView1.setImageResource(R.drawable.k1)
linearLayout.addView(imageView1)
val imageView2 = ImageView(this)
imageView2.setLayoutParams(params)
imageView2.scaleType = ImageView.ScaleType.CENTER_CROP
imageView2.setImageResource(R.drawable.k2)
linearLayout.addView(imageView2)
val imageView3 = ImageView(this)
imageView3.setLayoutParams(params)
imageView3.scaleType = ImageView.ScaleType.CENTER_CROP
imageView3.setImageResource(R.drawable.k3)
linearLayout.addView(imageView3)
val imageView4 = ImageView(this)
imageView4.setLayoutParams(params)
imageView4.scaleType = ImageView.ScaleType.CENTER_CROP
imageView4.setImageResource(R.drawable.k4)
linearLayout.addView(imageView4)
val root: LinearLayout = findViewById(R.id.main)
root.addView(scrollView)
}
}