19

i need to create like scrolling TextView inside the ScrollView.. like 1 main ScrollView inside that scrollable TextView i have put this code in RelativeLayout

  <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/img_view_flag"
        android:layout_marginTop="17dp" 
        android:paddingBottom="20dip">

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



                <TextView
                    android:id="@+id/tv_desc"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_below="@+id/rel_layout"
                    android:layout_marginTop="15dp"
                    android:maxLines="7"
                    android:scrollbars="vertical"
                    android:text="Medium Text"
                    android:paddingBottom="10dip"
                     android:textAppearance="?android:attr/textAppearanceSmall"
                    android:textColor="@color/black" />


                <RelativeLayout
                    android:id="@+id/rel_layout2"
                    android:layout_width="match_parent"
                    android:layout_height="4dp"
                    android:layout_alignParentLeft="true"
                    android:layout_below="@+id/tv_desc"
                    android:background="@color/black" >
                </RelativeLayout>

                <TextView
                    android:id="@+id/tv_title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_below="@+id/rel_layout2"
                    android:layout_marginTop="24dp"
                    android:maxLines="5"
                    android:scrollbars="vertical"
                    android:text="Small Text"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:textColor="@color/black" />

                <TextView
                    android:id="@+id/textView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/rel_layout2"
                    android:layout_centerHorizontal="true"
                    android:text="Title"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textColor="@color/black" />

                <RelativeLayout
                    android:id="@+id/rel_layout3"
                    android:layout_width="match_parent"
                    android:layout_height="4dp"
                    android:layout_alignParentLeft="true"
                    android:layout_below="@+id/tv_title"
                    android:background="@color/black" >
                </RelativeLayout>

                <TextView
                    android:id="@+id/textView2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/rel_layout3"
                    android:layout_centerHorizontal="true"
                    android:text="Lyricist"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textColor="@color/black" />

                <TextView
                    android:id="@+id/tv_l"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_below="@+id/textView2"
                    android:text="Small Text"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:textColor="@color/black" />

                <RelativeLayout
                    android:id="@+id/rel_layout4"
                    android:layout_width="match_parent"
                    android:layout_height="4dp"
                    android:layout_alignParentLeft="true"
                    android:layout_below="@+id/tv_l"
                    android:background="@color/black" >
                </RelativeLayout>

                <TextView
                    android:id="@+id/textView3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignLeft="@+id/textView2"
                    android:layout_below="@+id/rel_layout4"
                    android:text="Composer"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textColor="@color/black" />

                <TextView
                    android:id="@+id/tv_composer"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_below="@+id/textView3"
                    android:text="Small Text"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:textColor="@color/black" />

                <RelativeLayout
                    android:id="@+id/rel_layout5"
                    android:layout_width="match_parent"
                    android:layout_height="4dp"
                    android:layout_alignParentLeft="true"
                    android:layout_below="@+id/tv_composer"
                    android:background="@color/black" >
                </RelativeLayout>

                <TextView
                    android:id="@+id/textView4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignLeft="@+id/textView3"
                    android:layout_below="@+id/rel_layout5"
                    android:text="Adopted"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textColor="@color/black" />

                <TextView
                    android:id="@+id/tv_adopted"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_below="@+id/textView4"
                    android:text="Small Text"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:textColor="@color/black" />

                <RelativeLayout
                    android:id="@+id/rel_layout6"
                    android:layout_width="match_parent"
                    android:layout_height="4dp"
                    android:layout_alignParentLeft="true"
                    android:layout_below="@+id/tv_adopted"
                    android:background="@color/black" >
                </RelativeLayout>

                <TextView
                    android:id="@+id/textView5"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Lyrics"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textColor="@color/black" />

                <TextView
                    android:id="@+id/tv_lyri"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_below="@+id/textView5"
                    android:maxLines="10"
                    android:scrollbars="vertical"
                    android:text="Small Text"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:textColor="@color/black" />

        </LinearLayout>
    </ScrollView>

but not working for me.... main scrollbar working

The Heist
  • 1,444
  • 1
  • 16
  • 32
Android
  • 8,995
  • 9
  • 67
  • 108
  • For `EditText` see https://stackoverflow.com/questions/24428808/how-to-scroll-the-edittext-inside-the-scrollview/. – CoolMind Sep 22 '20 at 12:15

5 Answers5

23

This problem is a little complicated and I don't have enough time to explain, but you can easily get rid of it by adding these onTouch classes at the end of onCreat class or onCreatView class (if you are using fragment).

scrollview1.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                tv_desc.getParent().requestDisallowInterceptTouchEvent(false);

                return false;
            }
        });

tv_desc.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                tv_desc.getParent().requestDisallowInterceptTouchEvent(true);

                return false;
            }
        });

and also for other textviews.

  • 1
    I've only had to call `requestDisallowInterceptTouchEvent(true)` on the `MotionEvent.ACTION_DOWN` in order to allow the scrolling. – Anonsage Feb 26 '16 at 17:38
12

Just set the

android:maxLines = "AN_INTEGER"

android:scrollbars = "vertical"

properties of your TextView in your layout's xml file.

Then use:

tv_desc.setMovementMethod(new ScrollingMovementMethod());


scrollview.setOnTouchListener(new OnTouchListener() { 

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            tv_desc.getParent().requestDisallowInterceptTouchEvent(false);

            return false;
        }
    });

tv_desc.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            tv_desc.getParent().requestDisallowInterceptTouchEvent(true);

            return false;
        }
    });

Hope it will work

jinkal
  • 1,622
  • 16
  • 21
10

Actually, the only method (that worked for me) to make a TextView/EditText to scroll inside a ScrollView is

public class Utils {
    public static void enableScroll(View view) {
        if (view instanceof TextView) {
            TextView textView = (TextView) view;
            textView.setMovementMethod(new ScrollingMovementMethod());
        }

        view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                v.getParent().requestDisallowInterceptTouchEvent(true);
                switch (event.getAction() & MotionEvent.ACTION_MASK) {
                    case MotionEvent.ACTION_UP:
                        v.getParent().requestDisallowInterceptTouchEvent(false);
                        break;
                }
                return false;
            }
        });
    }
}

with

<TextView
    android:id="@+id/textView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scrollbars="vertical" />

(No need to set an android:maxLines attributes)

use

TextView textView = findViewById(R.id.textView);
Utils.enableScroll(textView);
6

First, set android:scrollbars="vertical":

<AppCompatTextView
    android:id="@+id/actv"
    ...
    android:scrollbars="vertical" />

Also, add this extension function:

/**
 * If this [AppCompatTextView] is placed inside ScrollView then we allow it get scrolled inside
 * that ScrollView
 */
fun AppCompatTextView.makeScrollableInsideScrollView() {
    movementMethod = ScrollingMovementMethod()
    setOnTouchListener { v, event ->
        v.parent.requestDisallowInterceptTouchEvent(true)
        when (event.action and MotionEvent.ACTION_MASK) {
            MotionEvent.ACTION_UP -> {
                v.parent.requestDisallowInterceptTouchEvent(false)
                //It is required to call performClick() in onTouch event.
                performClick()
            }
        }
        false
    }

Then call:

actv.makeScrollableInsideScrollView()
Malwinder Singh
  • 6,644
  • 14
  • 65
  • 103
1

You can also simply add a little elevation to the textView.

    <TextView
        android:id="@+id/tv_composer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:elevation="1dp"
        android:layout_below="@+id/textView3"
        android:text="Small Text"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="@color/black" />

It did the trick for me. :)

Mallock
  • 515
  • 1
  • 4
  • 12