In this article, we will learn about android HorizontalScrollView using Kotlin. We will go through various example that demonstrates how to use different attributes of HorizontalScrollView. For example,
In this article, we will get answer to questions like –
- What is HorizontalScrollView?
- Why should we consider HorizontalScrollView while designing ui for any app?
- What are possibilities using HorizontalScrollView while designing ui? etc.
Let’s have a quick demo of things we want to cover in this tutorial –
Output
Getting Started
We can define android HorizontalScrollView widget 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.
Now, how do we use HorizontalScrollView in android application ?
Creating New Project
At first, we will create an application.
So, follow steps below to create any android project in Kotlin –
Step | Description |
---|---|
1. | Open Android Studio (Ignore if already done). |
2. | Go to File => New => New Project. This will open a new window. Then, under Phone and Tablet section, select Empty Activity. Then, click Next. |
3. | In next screen, select project name as HorizontalScrollView. Then, fill other required details. |
4. | Then, clicking on Finish button creates new project. |
Newbie in Android ?
Some very important concepts (Recommended to learn before you move ahead)
Before we move ahead, we need to setup for viewBinding to access Android HorizontalScrollView Using Kotlin file without using findViewById() method.
Setup ViewBinding
Add viewBinding true in app/build.gradle file.
android { // OTHER CODE... buildFeatures { viewBinding true } }
Now, set content in activity using view binding.
Open MainActivity.kt file and write below code in it.
class MainActivity : AppCompatActivity() { private lateinit var binding: ActivityMainBinding override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = ActivityMainBinding.inflate(layoutInflater) val view = binding.root setContentView(view) } }
Now, we can access view in Kotlin file without using findViewById() method.
Using HorizontalScrollView in Kotlin
Follow steps below to use HorizontalScrollView in newly created project –
- 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.
- 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>
- Open res/layout/activity_main.xml file. Then, add below code in 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_marginStart="20dp" android:layout_marginEnd="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_marginEnd="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_marginEnd="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_marginEnd="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_marginEnd="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_marginEnd="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.
-
We can also access it in Kotlin File, MainActivity.kt, as below –
package com.tutorialwing.horizontalscrollview import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import com.tutorialwing.horizontalscrollview.databinding.ActivityMainBinding class MainActivity : AppCompatActivity() { private lateinit var binding: ActivityMainBinding override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = ActivityMainBinding.inflate(layoutInflater) val view = binding.root setContentView(view) } }
Now, run the application. We will get output as below –
Different Attributes of HorizontalScrollView in XML
Now, we will see how to use different attributes of Android HorizontalScrollView using Kotlin to customise it –
Set Id of HorizontalScrollView
Many a time, we need id of View to access it in kotlin file or create ui relative to that view in xml file. So, we can set id of HorizontalScrollView using android:id attribute like below –
<HorizontalScrollView android:id="@+id/horizontalScrollView_ID" />
Here, we have set id of HorizontalScrollView as horizontalScrollView_ID using android:id=”” attribute. So, if we need to reference this HorizontalScrollView, we need to use this id – horizontalScrollView_ID.
Learn to Set ID of HorizontalScrollView Dynamically
Set Width of HorizontalScrollView
We use android:layout_width=”” attribute to set width of HorizontalScrollView.
We can do it as below –
<HorizontalScrollView android:id="@+id/horizontalScrollView_ID" android:layout_width="wrap_content" />
Width can be either “MATCH_PARENT” or “WRAP_CONTENT” or any fixed value (like 20dp, 30dp etc.).
Learn to Set Width of HorizontalScrollView Dynamically
Set Height of HorizontalScrollView
We use android:layout_height=”” attribute to set height of HorizontalScrollView.
We can do it as below –
<HorizontalScrollView android:id="@+id/horizontalScrollView_ID" android:layout_width="wrap_content" android:layout_height="wrap_content" />
Height can be either “MATCH_PARENT” or “WRAP_CONTENT” or any fixed value.
Learn to Set Height of HorizontalScrollView Dynamically
Set Padding of HorizontalScrollView
We use android:padding=”” attribute to set padding of HorizontalScrollView.
We can do it as below –
<HorizontalScrollView android:id="@+id/horizontalScrollView_ID" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp" />
Here, we have set padding of 10dp in HorizontalScrollView using android:padding=”” attribute.
Learn to Set Padding of HorizontalScrollView Dynamically
Set Margin of HorizontalScrollView
We use android:layout_margin=”” attribute to set margin of HorizontalScrollView.
We can do it as below –
<HorizontalScrollView android:id="@+id/horizontalScrollView_ID" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" />
Here, we have set margin of 10dp in HorizontalScrollView using android:layout_margin=”” attribute.
Learn to Set Margin of HorizontalScrollView Dynamically
Set Background of HorizontalScrollView
We use android:background=”” attribute to set background of HorizontalScrollView.
We can do it as below –
<HorizontalScrollView android:id="@+id/horizontalScrollView_ID" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#ff0000" />
Here, we have set background of color #ff0000 in HorizontalScrollView using android:background=”” attribute.
Learn to Set Background of HorizontalScrollView Dynamically
Set Visibility of HorizontalScrollView
We use android:visibility=”” attribute to set visibility of HorizontalScrollView.
We can do it as below –
<HorizontalScrollView android:id="@+id/horizontalScrollView_ID" android:layout_width="wrap_content" android:layout_height="wrap_content" android:visibility="gone" />
Here, we have set visibility of HorizontalScrollView using android:visiblity=”” attribute. Visibility can be of three types – gone, visible and invisible
Learn to Set Visibility of HorizontalScrollView Dynamically
Till now, we have seen how to use android HorizontalScrollView using Kotlin. We have also gone through different attributes of HorizontalScrollView to perform certain task. Let’s have a look at list of such attributes and it’s related task.
Different Attributes of Android HorizontalScrollView Widget
Below are the various attributes that are used to customise android HorizontalScrollView Widget. However, you can check the complete list of attributes of HorizontalScrollView in it’s official documentation site. Here, we are going to list some of the important attributes of this widget –
Attributes of android horizontalScrollView are same as attributes of scrollView.
We have seen different attributes of HorizontalScrollView and how to use it. If you wish to visit post to learn more about it
Thus, we have seen what is HorizontalScrollView, how can we use android HorizontalScrollView using Kotlin ? etc. We also went through different attributes of android HorizontalScrollView.
You must be logged in to post a comment.