Create An Android HorizontalScrollView 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 horizontalScrollView programmatically in kotlin in the application. We will also learn how to add horizontalScrollView to linearLayout programmatically in android application.

Output

Tutorialwing Kotlin Dynamic HorizontalScrollView Output Android HorizontalScrollView Programmatically in Kotlin

Tutorialwing Kotlin Dynamic HorizontalScrollView Output

Getting Started

At first, we will create android project. Then, we will create and use horizontalScrollView 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 DynamicHorizontalScrollView. 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 horizontalScrollView dynamically.

2. Modify Values Folder

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

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

3. Download Drawable Resources Needed

You will need some images, stored in res/drawable folder, to be used in the application. These drawable images will be used to create views to be scrolled inside horizontalScrollView.

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 a linearLayout, with id rootContainer, that will act as container for the horizontalScrollView created dynamically in the application.

5. Create Android HorizontalScrollView Programmatically / Dynamically in Kotlin

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

package com.tutorialwing.dynamichorizontalscrollview

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.Gravity
import android.view.ViewGroup
import android.widget.HorizontalScrollView
import android.widget.ImageView
import android.widget.LinearLayout

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val horizontalScrollView = HorizontalScrollView(this)
        val layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
        horizontalScrollView.layoutParams = layoutParams

        val linearLayout = LinearLayout(this)
        val linearParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        linearLayout.orientation = LinearLayout.HORIZONTAL
        linearLayout.layoutParams = linearParams

        horizontalScrollView.addView(linearLayout)

        val imageView1 = ImageView(this)
        val params1 = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        params1.setMargins(30, 20, 30, 0)
        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, 20, 30, 0)
        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, 20, 30, 0)
        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, 20, 30, 0)
        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, 20, 30, 0)
        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, 20, 30, 0)
        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(horizontalScrollView)
    }
}

We have defined horizontalScrollView 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 horizontalScrollView. Since, horizontalScrollView can have only one direct child, all the defined imageViews have been added in linearLayout. Finally, we have added horizontalScrollView 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.dynamichorizontalscrollview"
		  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 HorizontalScrollView 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