2

I set the ScrollView in my LinearLayout. I want a TextView to scroll. I tried to use ScrollView in xml like this. But, it's not working.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="8dp"
    tools:context=".SubpagesLayout.ReportResult">

    <!-- first scrollView -->
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/edit_corner_blue"
                android:gravity="center"
                android:text="@string/personalInformation"
                android:textColor="@android:color/white" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:orientation="horizontal">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/birthDate" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text=" TestData" />

            </LinearLayout>

            <!-- second scrollView -->
            <ScrollView
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                    <TextView
                        android:id="@+id/textOphthalmoscopy"
                        android:layout_width="match_parent"
                        android:layout_height="80dp"
                        android:text="standard instrth various settings that allow focusing and adjustment of the light source to accommodate the viewer and to evaluate various features of the fundus." />
            </ScrollView>
        </LinearLayout>
    </ScrollView>    
</LinearLayout>

I tried another way like this, but it's still not working too.

android:maxLines = "AN_INTEGER"
android:scrollbars = "vertical"

I used

myTextView.setMovementMethod(new ScrollingMovementMethod());

So how to let TextView to scroll? If it is the second ScrollView.

4
  • 1
    you can not use nested scroll view , try using the NestedScrollView provided by android , however avoid nesting scrolls if u can Commented Jan 23, 2017 at 6:58
  • yes , it's NestedScrollView ! thanks your help , but why should i avoid NestedScrollView ? Commented Jan 23, 2017 at 7:03
  • i dont know the technical answer to this, but physically its not advisable to have one scroll inside another scrolling surface ,since android is based on MATERIAL UI Commented Jan 23, 2017 at 7:05
  • it's ok , you save me a lot , i see know , thanks for your help @Ak9637 Commented Jan 23, 2017 at 7:09

2 Answers 2

1

You might consider using a NestedScrollView along with a ListView for showing the scrollable TextView.

The ListView will have a fixed size so that it'll not take the total height of the items inside.

So the layout may look like this.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="8dp"
    tools:context=".SubpagesLayout.ReportResult">

    <!-- first scrollView -->
    <NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/edit_corner_blue"
                android:gravity="center"
                android:text="@string/personalInformation"
                android:textColor="@android:color/white" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:orientation="horizontal">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/birthDate" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text=" TestData" />
            </LinearLayout>

            <ListView
                android:layout_width="match_parent"
                <!--set a fixed height-->
                android:layout_height="80dp"
                .. Other attributes
            </ListView>
        </LinearLayout>
    </NestedScrollView>    
</LinearLayout>

And the list item of the ListView may contain the TextView you want to scroll. Set the height of the TextView to wrap_content.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="8dp"
    tools:context=".SubpagesLayout.ReportResult">

        <TextView
            android:id="@+id/textOphthalmoscopy"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="standard instrth various settings that allow focusing and adjustment of the light source to accommodate the viewer and to evaluate various features of the fundus." />
</LinearLayout>

Now your ListView will have a single item and this will scroll with the default behaviour of list.

The whole layout will be scrolled via NestedScrollView.

Hope that helps!

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

2 Comments

ha it's you again ! really thanks for your help . This way add a new layout , it's more complicated , i take it anyway , thanks @Reaz Murshed
Glad to find you here again. Please upvote if helpful. :)
1

Having a scrollView inside another scrollView is not a good prictice but ant a you check below links it may fix you problem.

Solution 1

Solution 2

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.