Android ScrollView Using Kotlin With Example

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

Output

Tutorialwing Kotlin ScrollView Output Android ScrollView Using Kotlin With Example

Tutorialwing Kotlin ScrollView Output

Getting Started

Android scrollView can be defined as below –

ScrollView is a widget that are used to define vertically scrollable views. It can have only one direct child. So, if you need multiple child views, create a viewGroup as direct child of scrollView. Then, define all the views inside this viewGroup.

Different Attributes of Android ScrollView Widget

Some of the popular attributes of android scrollView widget are –

Sr. XML Attributes Description
1 android:fillViewport Defines whether scrollView should stretch it’s content to fill the viewport

Some of the popular attributes of android ScrollView inherited from FrameLayout are –

Sr. XML Attributes Description
1 android:foregroundGravity Defines gravity of the foreground drawable
2 android:measureAllChildren Defines whether to measure all children or only those in VISIBLE or INVISIBLE state when measuring

Some of the popular attributes of ScrollView inherited from ViewGroup are –

Sr. XML Attributes Description
1 android:animateLayoutChanges Defines whether LayoutTransition should run whenever there is any changes in layout
2 android:animationCache Defines whether layout animations should create a drawing cache for their children.
3 android:clipToPadding Defines whether the ViewGroup will clip its children and resize (but not clip) any EdgeEffect to its padding, if padding is not zero.
4 android:layoutAnimation Defines the layout animation to use the first time the ViewGroup is laid out
5 android:layoutMode Defines the layout mode of this viewGroup

Some of the popular attributes of android ScrollView inherited from View are –

Sr. XML Attributes Description
1 android:alpha Defines the alpha of the view
2 android:background Defines the background of the view
3 android:padding Defines padding of the view for all edges
4 android:tooltipText Defines text displayed in a small popup window on hover or long press
5 android:clickable Defines whether view is clickable or not
6 android:theme Defines a theme override for view
7 android:id Defines id of the view
8 android:padding Defines padding of the view



Example of Android ScrollView Using Kotlin

At first, we will create android application. Then, we will use scrollView 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 ScrollView. 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 scrollView 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">ScrollView</string>
	<string name="no_image">No Image</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 by child view of scrollView to create different views.

4. Use ScrollView 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"?>
<ScrollView
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="match_parent"
	android:layout_height="match_parent">

	<LinearLayout
		android:layout_width="match_parent"
		android:layout_height="wrap_content"
		android:orientation="vertical">

		<ImageView
			android:id="@+id/image1"
			android:layout_width="match_parent"
			android:layout_height="wrap_content"
			android:layout_marginBottom="20dp"
			android:layout_marginTop="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_marginBottom="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_marginBottom="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_marginBottom="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_marginBottom="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_marginBottom="20dp"
			android:contentDescription="@string/no_image"
			android:src="@drawable/zespri_kiwi"/>

	</LinearLayout>
</ScrollView>

In activity_main.xml file, we have defined scrollView widget. Then, we have defined linearLayout as direct child of scrollView. Note that scrollView can have only one direct child. So, all the other views have been defined inside linearLayout that we want to scroll.

5. Access ScrollView Widget in Kotlin file

Open src/main/java/com.tutorialwing.scrollview/MainActivity.kt file and add below code into it.

package com.tutorialwing.scrollview

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 not modified code inside MainActivity.kt 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.scrollview"
		  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 ScrollView using 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