HorizontalScrollView in Kotlin

18 Feb, 2021
In 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 create a Horizontal ScrollView in Kotlin .
Let’s start by first creating a project in Android Studio. To do so, follow these instructions:
First step is to create a new Project in Android Studio. For this follow these steps:
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.
<?xml version="1.0" encoding="utf-8"?><HorizontalScrollView        xmlns:android="http://schemas.android.com/apk/res/android"        android:layout_width="match_parent"        android:layout_height="match_parent">      <LinearLayout            android:layout_width="200dp"            android:layout_height="200dp"            android:layout_marginTop="20dp"            android:orientation="horizontal">                    <ImageView                android:id="@+id/image1"                android:layout_width="200dp"                android:layout_height="200dp"                android:layout_marginRight="20dp"                android:src="@mipmap/image1"/>          <ImageView                android:id="@+id/image2"                android:layout_width="200dp"                android:layout_height="200dp"                android:layout_marginRight="20dp"                android:src="@mipmap/image2"/>          <ImageView                android:id="@+id/image3"                android:layout_width="200dp"                android:layout_height="200dp"                android:layout_marginRight="20dp"                android:src="@mipmap/image3"/>          <ImageView                android:id="@+id/image4"                android:layout_width="200dp"                android:layout_height="200dp"                android:layout_marginRight="20dp"                android:src="@mipmap/image4"/>        </LinearLayout></HorizontalScrollView>
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/mipmap folder.
Note: We have added the images to mipmap folder instead of drawable folder because the size of the images is very large.
Open app/src/main/java/yourPackageName/MainActivity.kt and do the following changes:
package com.geeksforgeeks.myfirstKotlinapp  import androidx.appcompat.app.AppCompatActivityimport android.os.Bundle  class MainActivity : AppCompatActivity() {      override fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        setContentView(R.layout.activity_main)    }}
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"          package="i.apps.hscrollview">      <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 Add Views Dynamically and Store Data in Arraylist in Android?
Android RecyclerView in Kotlin
Broadcast Receiver in Android With Example
Android SDK and it's Components
Flutter - Custom Bottom Navigation Bar
How to Add Views Dynamically and Store Data in Arraylist in Android?
Android UI Layouts
Android RecyclerView in Kotlin
Broadcast Receiver in Android With Example
How to Communicate Between Fragments in Android?