Morton
Morton

Reputation: 5760

android textview scrollview in layout scrollview

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.

Upvotes: 2

Views: 176

Answers (2)

Anil
Anil

Reputation: 1615

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

Upvotes: 1

Reaz Murshed
Reaz Murshed

Reputation: 24211

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!

Upvotes: 1

Related Questions