Dynamic ScrollView in Kotlin - GeeksforGeeks

18 Feb, 2021
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.
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.
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.Bundleimport android.view.Gravityimport android.view.ViewGroupimport android.widget.ImageViewimport android.widget.LinearLayoutimport 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)    }}
<?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>
Android-View
Kotlin Android
Picked
Android
Kotlin
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Create and Add Data to SQLite Database in Android?
Broadcast Receiver in Android With Example
Services in Android with Example
Content Providers in Android with Example
Android RecyclerView in Kotlin
Broadcast Receiver in Android With Example
Services in Android with Example
Content Providers in Android with Example
Android UI Layouts
Android RecyclerView in Kotlin