ScrollView is used to scroll the elements in vertical direction. We use android.widget.ScrollView class functionality for this scroll. Views with scroll bars are useful when the size of the content is greater than the size of the layouts. There can only be one direct child in the android ScrollView. In case there is a requirement to add more than one view to a scroll view, then we must add them to another standard layout. Lets see example of ScrollView
package com.techstackguru.horizontalscroll;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:text="One" />
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:text="Two" />
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:text="Three" />
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:text="Four" />
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:text="Five" />
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:text="Six" />
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:text="Six" />
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:text="Seven" />
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:text="Eight" />
</LinearLayout>
</ScrollView>
</LinearLayout>