Open In App

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:

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.






<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/layout"
        tools:context=".MainActivity">
  
  
  
</LinearLayout>

Adding Images

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

linearLayout1?.addView(scrollView)




package com.geeksforgeeks.myfirstkotlinapp 
  
import androidx.appcompat.app.AppCompatActivity 
import android.os.Bundle
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 layoutParams = LinearLayout.LayoutParams(
        ViewGroup.LayoutParams.MATCH_PARENT,
        ViewGroup.LayoutParams.MATCH_PARENT)
        scrollView.layoutParams = layoutParams
   
        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 imageView1 = ImageView(this)
        val params1 =
            LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 
            ViewGroup.LayoutParams.WRAP_CONTENT)
        //setting margin 
        params1.setMargins(0, 30, 0, 30)
        //aligning the layout to center of the screen
        params1.gravity = Gravity.CENTER
        imageView1.setLayoutParams(params1)
        //setting our own downloaded/custom image to the imageView
        imageView1.setImageResource(R.drawable.image1)
        linearLayout.addView(imageView1)
   
        val imageView2 = ImageView(this)
        val params2 =
            LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 
            ViewGroup.LayoutParams.WRAP_CONTENT)
        params2.setMargins(0, 0, 0, 30)
        params2.gravity = Gravity.CENTER
        imageView2.setLayoutParams(params2)
        imageView2.setImageResource(R.drawable.image2)
        linearLayout.addView(imageView2)
   
        val imageView3 = ImageView(this)
        val params3 =
            LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 
            ViewGroup.LayoutParams.WRAP_CONTENT)
        params3.setMargins(0, 0, 0, 30)
        params3.gravity = Gravity.CENTER
        imageView3.setLayoutParams(params3)
        imageView3.setImageResource(R.drawable.image3)
        linearLayout.addView(imageView3)
   
        val imageView4 = ImageView(this)
        val params4 =
            LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 
            ViewGroup.LayoutParams.WRAP_CONTENT)
        params4.setMargins(0, 0, 0, 30)
        params4.gravity = Gravity.CENTER
        imageView4.setLayoutParams(params4)
        imageView4.setImageResource(R.drawable.image4)
        linearLayout.addView(imageView4)
   
        val linearLayout1 = findViewById<LinearLayout>(R.id.layout)
        linearLayout1?.addView(scrollView)
    }
}

AndroidManifest.xml file




<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="i.apps.myapplication">
  
    <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
  
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>
  
</manifest>

Run as Emulator:




Article Tags :