Search code examples
androidscrollviewandroid-linearlayout

Android - Auto Scroll LinearLayout inside ScrollView?


I have a vertical LinearLayout inside ScrollView. LinearLayout grows dynamically. I add fragments to this LinearLayout whenever a button is pressed(that is outside the scrollview), fragments are added, I also can see the scollbar but I want scroll bar to be auto scrolled to the last element that I have added. Currently there is no auto scrolling, I have to scroll it manually to see the last element.

<ScrollView
    android:id="@+id/scrollView2"
    android:layout_width="557dp"
    android:layout_height="440dp"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView16"
    app:layout_constraintVertical_bias="0.131"
    >

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

    </LinearLayout>
</ScrollView>

Solution

  • Please use recyclerView instead of scrollView. Here is the example to use scrollView to last.

    mScrollView.fullScroll(ScrollView.FOCUS_DOWN);
    
    mScrollView.postDelayed(new Runnable() {
    @Override
    public void run() {
        scroll.fullScroll(ScrollView.FOCUS_DOWN);
    }}, 700);