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.|
|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.|
|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.
Step 3: Modify activity_main.xml
Add the ScrollView and inside the ScrollView add a TextView to display the strings that are taken in the strings.xml file.
As already mentioned above, Scrollview can only contain one direct child. In this case, the child is textview. On noticing this textview you will realize that the text added inside textview is mentioned as @string/scrolltext which refers to a string resource inside the strings.xml file.
Step 4: MainActivity.kt file
There is nothing to do with the MainActivity.kt file, so keep it as it is.
Output: Run on Emulator
- DynamicHorizontal ScrollView in Kotlin
- Dynamic ScrollView in Kotlin
- Android | AdMob Banner Ads for Android Studio
- Android | Android Application File Structure
- Android | AdMob Interstitial Ads for Android Studio
- Android | How to Create/Start a New Project in Android Studio?
- Android | Running your first Android app
- Android | How to add Radio Buttons in an Android Application?
- Android | How to Request permissions in Android Application?
- Difference between Android 1.1 and Android 4.0.1
- Difference between Android 1.1 and Android 2.3.4
- Difference between Android 1.0 and Android 1.1
- Difference between Android 1.0 and Android 7.1
- Difference between Android 1.0 and Android 2.3
- Difference between Android 1.0 and Android 2.2
- Difference between Android 1.0 and Android 1.6
- Difference between Android 1.0 and Android 2.1
- Difference between Android 1.0 and Android 2.0.1
- Difference between Android 1.0 and Android 2.0
- Difference between Android 1.0 and Android 1.5
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.