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 android horizontalScrollView using kotlin in any android application. We will also learn about different attributes of android horizontalScrollView that can be used to customise this widget.
Output

Tutorialwing Kotlin HorizontalScrollView Output
Getting Started
Android HorizontalScrollView can be defined as below –
HorizontalScrollview is a widget, a subclass of FrameLayout, that acts as container for the view to be scrolled horizontally. It can contains only one direct child. So, if you want to define multiple child views inside horizontalScrollView, you must define a viewGroup(i.e. LinearLayout, RelativeLayout etc.) as a direct child. Then, you can define multiple views inside defined viewGroup.
Different Attributes of Android HorizontalScrollView Widget
Attributes of android horizontalScrollView are same as attributes of scrollView.
Example of Android HorizontalScrollView Using Kotlin
At first, we will create android application. Then, we will use HorizontalScrollView using kotlin in the application.
1. Creating New Project in Kotlin
Follow steps below to create new project. Please ignore the steps if you have already created the project.
Step | Description |
---|---|
1. | Open Android Studio. |
2. | Go to File => New => New Project. Write application name as HorizontalScrollView. 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. | You will get a newly created project successfully if you have followed steps properly. |
Since we have a project now, we will modify xml and other files to use HorizontalScrollView using kotlin in the application.
2. Modify values folder
Open res/values/strings.xml file. Then, add below code into it.
<resources> <string name="app_name">HorizontalScrollView</string> <string name="no_image">No Image</string> </resources>
3. Download Drawable Resources Needed
You will need some drawable images, stored in res/drawable folder, to be used in the application. These drawable images will be used by child views of horizontalScrollView in the application.
4. Use HorizontalScrollView Widget in xml file
Open src/main/res/layout/activity_main.xml file and add below code into it.
<?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="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:orientation="horizontal"> <ImageView android:id="@+id/image1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:contentDescription="@string/no_image" android:src="@drawable/guava"/> <ImageView android:id="@+id/image2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginRight="20dp" android:contentDescription="@string/no_image" android:src="@drawable/jackfruit"/> <ImageView android:id="@+id/image3" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginRight="20dp" android:contentDescription="@string/no_image" android:src="@drawable/mix_fruit"/> <ImageView android:id="@+id/image4" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginRight="20dp" android:contentDescription="@string/no_image" android:src="@drawable/pomegranate"/> <ImageView android:id="@+id/image5" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginRight="20dp" android:contentDescription="@string/no_image" android:src="@drawable/strawberry"/> <ImageView android:id="@+id/image6" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginRight="20dp" android:contentDescription="@string/no_image" android:src="@drawable/zespri_kiwi"/> </LinearLayout> </HorizontalScrollView>
In activity_main.xml file, we have defined horizontalScrollView widget. Then, we have defined a linearLayout as direct child of horizontalScrollView. Note that this widget can contain only one direct child. That’s why we have defined only one linearLayout as direct child. All the imageViews are inside this linearLayout.
5. Access HorizontalScrollView Widget in Kotlin file
Open src/main/java/com.tutorialwing.horizontalscrollview/MainActivity.kt file and add below code into it.
package com.tutorialwing.horizontalscrollview import android.os.Bundle import android.support.v7.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) } }
We have modified nothing in kotlin file. So, it is as it was after creating project.
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 main/AndroidManifest.xml file is as below.
<?xml version="1.0" encoding="utf-8"?> <manifest package="com.tutorialwing.numberpicker" 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 Android HorizontalScrollView using Kotlin.