user1412303
user1412303

Reputation:

Android - ScrollView not scrolling, beginner programmer question

I'm beginner student of Android programming

I've added LinearLayout and ScrollView. I've filled LinearLayout with many long TextViews, to ensure, that it is long enough, to be able to scroll. My issue is, that is not scrolling and don't know why. Code below:

<!-- short version -->
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="false"
>
    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:scrollbars="vertical"
    android:id="@+id/description">

    </LinearLayout>
</ScrollView>


<!-- full version -->
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/frame_id"
xmlns:android="http://schemas.android.com/apk/res/android">
    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/relative_id"
    android:gravity="top">
        <androidx.media3.ui.PlayerView
        android:id="@+id/playerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:keepScreenOn="true"
        />

        <ImageButton
        android:id="@+id/fullScreenBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/fullscreen_icon"
        />


        <ImageView
        android:id="@+id/loading_img"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/loading"
        android:visibility="gone"
        />
        <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="false"
        >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:scrollbars="vertical"
            android:id="@+id/description">

        </LinearLayout>
    </ScrollView>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

AVD Preview

I'm newbie to Android, please, make me a hint.

Thank you so much. Mike

Upvotes: 0

Views: 64

Answers (1)

Yakubu
Yakubu

Reputation: 1089

It is not scrolling because you don't have any child inside the view group

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="false"
>
    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:scrollbars="vertical"
    android:id="@+id/description">

    </LinearLayout>
</ScrollView>

to achieve your aim, move the child inside the scroll view

like this

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/frame_id"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:fillViewport="true"> 

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:id="@+id/relative_id"
            android:gravity="top">

            <androidx.media3.ui.PlayerView
                android:id="@+id/playerView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:keepScreenOn="true"
            />

            <ImageButton
                android:id="@+id/fullScreenBtn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/fullscreen_icon"
            />

            <ImageView
                android:id="@+id/loading_img"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:src="@drawable/loading"
                android:visibility="gone"
            />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:scrollbars="vertical"
                android:id="@+id/description">
                <!-- Additional views can be added here -->
            </LinearLayout>

        </LinearLayout>
    </ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>

You can read more here to get more understanding how scrollview work scrollview-in-android

Upvotes: 2

Related Questions