Zak-Dev
Zak-Dev

Reputation: 85

ScrollView, I can't scroll to see top of the content

I have a LinearLayout with layout_gravity="bottom" inside a ScrollView

The problem is when the content reach the top of the ScrollView I can't scroll to see the top of this content.

Problem 1


I only can scroll to see the bottom of the screen but i can see only empty space .

problem 2


This is my Layout XML Code :

<android.support.constraint.ConstraintLayout 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="com.projects.zak_dev.errors.MainActivity">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:layout_editor_absoluteX="8dp"
    tools:layout_editor_absoluteY="8dp">

    <LinearLayout
        android:layout_gravity="bottom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/content" />
    </LinearLayout>
</ScrollView>
</android.support.constraint.ConstraintLayout>

Can someone help me please !

Upvotes: 1

Views: 1787

Answers (4)

Mehul Vasa
Mehul Vasa

Reputation: 51

Use This

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.oss.myapplication.MainActivity">

    <ScrollView
        android:id="@+id/src"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="8dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <RelativeLayout
                android:id="@+id/layout"
                android:layout_width="match_parent"
                android:layout_height="0dp">

            </RelativeLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                android:orientation="vertical"
                android:layout_below="@+id/layout">

                <TextView
                    android:id="@+id/textView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="@string/content" />
            </LinearLayout>

        </RelativeLayout>

    </ScrollView>

</RelativeLayout>

Upvotes: 0

Luca H
Luca H

Reputation: 133

You have to remove android:layout_gravity="bottom".

To get to the bottom of your ScrollView, add android:id="@+id/scrollView" to your ScrollView xml tag and simply add the below code to your onCreate function or any other place where you need it after you've set the text.

final ScrollView scrollView = ((ScrollView) findViewById(R.id.scrollView));
        scrollView.postDelayed(new Runnable() {
            @Override
            public void run() {
                scrollView.fullScroll(ScrollView.FOCUS_DOWN);
            }
        },100);

You can try to use post() instead of postDelayed() like this:

final ScrollView scrollView = ((ScrollView) findViewById(R.id.scrollView));
        scrollView.post(new Runnable() {
            @Override
            public void run() {
                scrollView.fullScroll(ScrollView.FOCUS_DOWN);
            }
        });

It does not have a delay, but this can lead to problems in some circumstances.

Upvotes: 1

Sharath kumar
Sharath kumar

Reputation: 4132

Add this to the scrollview.

android:fillViewport="true"

Upvotes: 0

Ashokkumar Adichill
Ashokkumar Adichill

Reputation: 238

See this

ScrollView scr = (ScrollView) findViewById(R.id.scr);
scr.setNestedScrollingEnabled(true);

Upvotes: 0

Related Questions