0

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

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

19 Comments

Oh, thank you so much. I generate content in Kotlin. How can I change height of linearlayout in code? Or I should do it in other way?
this will help you geeksforgeeks.org/…
Thank you! My problem is, that I have many TextViews, generated by Kotlin, which are inside LinearLayout. How to change global height of all LinearLayout (If I'll change only one it will split up). Thanks.
LinearLayout is a view group that can only align view vertically or horizontally... if you want the LinearLayout tot ake all the width or the height, then you set them to match_parent, if they are set to wrap_content, they won't take the whole of the screen
I have changed the height of your scrollview to match_parent <ScrollView android:layout_width="match_parent" android:layout_height="match_parent"
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.