In Android, a ScrollView is a view group that is used to make vertically scrollable views. A scroll view contains a single direct child only. In order to place multiple views in the scroll view, one needs to make a view group(like LinearLayout) as a direct child and then we can define many views inside it. A ScrollView supports Vertical scrolling only, so in order to create a horizontally scrollable view, HorizontalScrollView is used.
XML attributes of ScrollView
|android:fillViewport||Defines whether the scrollview should stretch its content to fill the viewport.|
|android:measureAllChildren||Determines whether to measure all children or just those in the VISIBLE or INVISIBLE state when measuring. Defaults to false.|
From View Attributes Description
android:alpha alpha property of the view, as a value between 0 (completely transparent) and 1 (completely opaque). android:background A drawable to use as the background. android:clickable Defines whether this view reacts to click events. android:contentDescription Defines text that briefly describes content of the view. android:id Supply an identifier name for this view, to later retrieve it with View.findViewById() or Activity.findViewById(). android:isScrollContainer Set this if the view will serve as a scrolling container, meaning that it can be resized to shrink its overall window so that there will be space for an input method. android:minHeight Defines the minimum height of the view. android:minWidth Defines the minimum width of the view. android:onClick Name of the method in this View’s context to invoke when the view is clicked. android:padding Sets the padding, in pixels, of all four edges. android:scrollbars Defines which scrollbars should be displayed on scrolling or not.
From ViewGroup Attributes Description
android:addStatesFromChildren Sets whether this ViewGroup’s drawable states also include its children’s drawable states. android:animateLayoutChanges Defines whether changes in layout should cause a LayoutTransition to run. android:clipChildren Defines whether a child is limited to draw inside of its bounds or not. android:clipToPadding Defines whether the ViewGroup will clip its children and resize any EdgeEffect to its padding, if padding is not zero. android:layoutAnimation Defines the layout animation to use the first time the ViewGroup is laid out. android:layoutMode Defines the layout mode of this ViewGroup. android:splitMotionEvents Sets whether this ViewGroup should split MotionEvents to separate child views during touch event dispatch.
This example demonstrates the steps involved to create a ScrollView in Android using Kotlin.
Step 1: Create a new project
- Click on File, then New => New Project.
- Choose “Empty Activity” for the project template.
- Select language as Kotlin.
- Select the minimum SDK as per your need.
Step 2: Modify strings.xml
Add some strings inside the strings.xml file to display those strings in the app.