Create An Android ScrollView Programmatically in Kotlin

Greetings!
We have recently published 100+ articles on android tutorials with kotlin and java. If you need, you may visit Android Tutorial for beginners page. You can also check Kotlin Tutorial for beginners. Also, if you are interested in content writing, you can mail us at tutorialwing@gmail.com.

Hello Readers! In this post, we are going to learn about how to use android scrollView programmatically in kotlin in the application. We will also learn how to add scrollView to linearLayout programmatically in android application.

Output

Tutorialwing Kotlin Dynamic ScrollView Output Android ScrollView Programmatically in Kotlin

Tutorialwing Kotlin Dynamic ScrollView Output

Getting Started

At first, we will create android project. Then, we will create and use scrollView programmatically in kotlin file.

1. Creating New Project in Kotlin

Follow the steps below to create new project. Please ignore the steps if you have already created a new project.

Step Description
1. Open Android Studio.
2. Go to File => New => New Project. Write application name as DynamicScrollView. Then, check Include Kotlin Support and click next button.
3. Select minimum SDK you need. However, we have selected 17 as minimum SDK. Then, click next button
4. Then, select Empty Activity => click next => click finish.
5. At this point, You will get a newly created android project successfully.

Now, we will modify xml and kotlin file to use scrollView dynamically.

2. Modify Values Folder

Open res/values/strings.xml file. Then, add below code into it.

<resources>
	<string name="app_name">DynamicScrollView</string>
</resources>

3. Download Drawable Resources Needed

You will need some images, stored in res/drawable folder, that will be used in the application. These drawable images will be used by child views of scrollView to create different views.

4. Modify Layout Folder

Open res/layout/activity_main.xml file. Then, add below code into it.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	android:id="@+id/rootContainer"
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="match_parent"
	android:layout_height="wrap_content"
	android:orientation="vertical">

</LinearLayout>

In activity_main.xml file, we have defined linearLayout, with id rootContainer, that will act as container for the scrollView created dynamically in the application.

5. Create Android ScrollView Programmatically / Dynamically in Kotlin

Open app/src/main/java/com.tutorialwing.dynamicscrollview/MainActivity.kt file. Then, add below code into it.

package com.tutorialwing.dynamicscrollview

import android.os.Bundle
import android.support.v7.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 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)
        params1.setMargins(0, 30, 0, 30)
        params1.gravity = Gravity.CENTER
        imageView1.layoutParams = params1
        imageView1.setImageResource(R.drawable.guava)
        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.layoutParams = params2
        imageView2.setImageResource(R.drawable.jackfruit)
        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.layoutParams = params3
        imageView3.setImageResource(R.drawable.mix_fruit)
        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.layoutParams = params4
        imageView4.setImageResource(R.drawable.pomegranate)
        linearLayout.addView(imageView4)

        val imageView5 = ImageView(this)
        val params5 = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        params5.setMargins(0, 0, 0, 30)
        params5.gravity = Gravity.CENTER
        imageView5.layoutParams = params5
        imageView5.setImageResource(R.drawable.strawberry)
        linearLayout.addView(imageView5)

        val imageView6 = ImageView(this)
        val params6 = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        params6.setMargins(0, 0, 0, 30)
        params6.gravity = Gravity.CENTER
        imageView6.layoutParams = params6
        imageView6.setImageResource(R.drawable.zespri_kiwi)
        linearLayout.addView(imageView6)

        val linearLayout1 = findViewById<LinearLayout>(R.id.rootContainer)
        linearLayout1?.addView(scrollView)
    }
}

We have defined scrollView programmatically in kotlin file (i.e. in MainActivity.kt file). Then, we have set layout params etc. in it. After that, we have created a linearLayout and added it as direct child of scrollView. Then, we have created 6 imageViews as added it as direct child of linearLayout. Then, we have added scrollView into linearLayout, having id rootContainer.

Since AndroidManifest.xml file is very important in any android application, we are also going to see the content inside this file.

AndroidManifest.xml file

Code inside src/main/AndroidManifest.xml file is –

<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.tutorialwing.dynamicscrollview"
		  xmlns:android="http://schemas.android.com/apk/res/android">

	<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>

When we run the program, we will get output as shown above.

That’s end of our tutorial on Creating Android ScrollView Programmatically in Kotlin.

Support Us

If you like Tutorialwing and would like to contribute, you can email an article on any educational topic at tutorialwing@gmail.com. We would love to publish your article. See your article on Tutorialwing and help others with your knowledge. Follow Facebook, LinkedIn, Google+, Twitter, Youtube for latest updates.
Greetings!
We have recently published 100+ articles on android tutorials with kotlin and java. If you need, you may visit Android Tutorial for beginners page. You can also check Kotlin Tutorial for beginners