Daniyal Javaid
Daniyal Javaid

Reputation: 1636

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>

Upvotes: 1

Views: 1990

Answers (2)

Umer Farooq
Umer Farooq

Reputation: 180

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);

Upvotes: 2

Salman Aziz
Salman Aziz

Reputation: 201

you can auto scroll on adding fragments.

scrollView.smoothScrollTo(0,scrollView.getBottom);

Upvotes: 1

Related Questions