DynamicHorizontal ScrollView in Kotlin
Android ScrollView allows multiple views that are places within the parent view group to be scrolled. Scrolling in the android application can be done in two ways either Vertically or Horizontally. In this article, we will be discussing how to programmatically create a Horizontal ScrollView 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.
Modify activity_main.xml file
Here, we will use the RelativeLayout to get the Scroll View from the Kotlin file.
XML
<? xml version="1.0" encoding="utf-8"?> < RelativeLayout 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" tools:context=".MainActivity"> < RelativeLayout android:layout_width="match_parent" android:layout_height="200dp" android:id="@+id/layout" android:layout_centerInParent="true"/> </ RelativeLayout > |
Update the strings.xml file
XML
< resources > < string name="app_name">DynamicHorizontal ScrollView</ string > </ resources > |
Add Images
We need to add some images which can be used for scrolling purpose. So, we have to copy the images from our local computer path to app/res/drawable folder.
Create Horizontal ScrollView in MainActivity.kt file
Open app/src/main/java/yourPackageName/MainActivity.kt. In this file, we declare a variable horizontalScrollView to create the Horizontal ScrollView widget like this:
val horizontalScrollView = HorizontalScrollView(this) //setting height and width val layoutParams = LinearLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT) horizontalScrollView.layoutParams = layoutParams
then, add the widget in layout using this
val linearLayout1 = findViewById(R.id.layout) linearLayout1?.addView(horizontalScrollView)
Java
package com.geeksforgeeks.myfirstkotlinapp import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.view.ViewGroup import android.widget.HorizontalScrollView import android.widget.ImageView import android.widget.LinearLayout import android.widget.RelativeLayout class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super .onCreate(savedInstanceState) setContentView(R.layout.activity_main) val horizontalScrollView = HorizontalScrollView( this ) //setting height and width val layoutParams = LinearLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT) horizontalScrollView.layoutParams = layoutParams val linearLayout = LinearLayout( this ) //setting height and width val linearParams = LinearLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) linearLayout.layoutParams = linearParams //adding horizontal scroll view to the layout horizontalScrollView.addView(linearLayout) val image1 = ImageView( this ) //setting height and width val params1 = LinearLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) image1.layoutParams = params1 //accessing images that we downloaded and copied to // drawable folder and setting it to imageview image1.setImageResource(R.drawable.img1) linearLayout.addView(image1) val image2 = ImageView( this ) //setting height and width val params2 = LinearLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) image2.layoutParams = params2 //accessing images that we downloaded and copied to // drawable folder and setting it to imageview image2.setImageResource(R.drawable.img2) linearLayout.addView(image2) val image3 = ImageView( this ) //setting height and width val params3 = LinearLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) image3.layoutParams = params3 //accessing images that we downloaded and copied to // drawable folder and setting it to imageview image3.setImageResource(R.drawable.img3) linearLayout.addView(image3) val image4 = ImageView( this ) //setting height and width val params4 = LinearLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) image4.layoutParams = params4 //accessing images that we downloaded and copied to // drawable folder and setting it to imageview image4.setImageResource(R.drawable.img4) linearLayout.addView(image4) val image5 = ImageView( this ) //setting height and width val params5 = LinearLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) image5.layoutParams = params5 //accessing images that we downloaded and copied to // drawable folder and setting it to imageview image5.setImageResource(R.drawable.img5) linearLayout.addView(image5) //accessing the relative layout where the scrollview will be active val linearLayout1 = findViewById<RelativeLayout>(R.id.layout) linearLayout1?.addView(horizontalScrollView) } } |
AndroidManifest.xml file
Java
<?xml version=" 1.0 " encoding="utf- 8 "?> <manifest xmlns:android="http: //schemas.android.com/apk/res/android" package ="com.geeksforgeeks.myfirstkotlinapp"> <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:
Here, is the video of scrolling horizontally in the android application.
Please Login to comment...