ScrollView in Android Example​

ScrollView is used to scroll the elements in vertical direction. We use android.widget.ScrollView class functionality for this scroll.

// MainActivity.java
package com.androidaura.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);
    }
}
 
// activity_main.xml

<?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>