ScrollView inside ScrollView Scrolling problem

While designing rich layouts you might need to use two scrollview in your app.
Well ideally its not advised to use two scrollview in a view. So try to avoid it.

Why this problem occurs ? :

When you put two scrollview android just get confused which scroll view is touched. So sometimes it gets unable to deliver touch event.

But even if the requirement forces you to make such layouts. Try this…

Say case is somewhat like this….

<ScrollView android:id=”@+id/parent_scroll”
            android:layout_width=”fill_parent”
            android:layout_height=”wrap_content”
            android:layout_weight=”1″
            android:background=”@drawable/dotted_bg”
            android:focusableInTouchMode=”false”>
                        <LinearLayout   />
                        <LinearLayout   />
                        <LinearLayout  >
                        <ScrollView android:id=”@+id/child_scroll”  
                        android:layout_width=”fill_parent”
                        android:layout_height=”fill_parent”
                        android:background=”@drawable/text_box_bg”>
                    <TextView android:id=”@+id/text_description”
                        android:layout_width=”fill_parent”
                        android:layout_height=”fill_parent”
                        android:textColor=”@color/gray”
                        android:textSize=”12dip”
                        android:padding=”5dip”
                        android:scrollbars=”vertical”/>
                    <!–ScrollView>
                  </LinearLayout>
</ScrollView>

Step 1 : Provide unique id to both the scrollview.
Step 2 : get reference of that two scrollview in your activity.

     parentScroll=(ScrollView)findViewById(R.id.parent_scroll);
     childScroll=(ScrollView)findViewById(R.id.child_scroll);

Step 3: Now set touch listeners for both.

            parentScroll.setOnTouchListener(new View.OnTouchListener() {

                public boolean onTouch(View v, MotionEvent event) {
                    Log.v(TAG,”PARENT TOUCH”);
                    findViewById(R.id.child_scroll).getParent().requestDisallowInterceptTouchEvent(false);
                    return false;
                }
            });
            childScroll.setOnTouchListener(new View.OnTouchListener() {

                public boolean onTouch(View v, MotionEvent event)
                {
                    Log.v(TAG,”CHILD TOUCH”);
                                        // Disallow the touch request for parent scroll on touch of child view
                    v.getParent().requestDisallowInterceptTouchEvent(true);
                    return false;
                }
            });

Done …

59 comments

  1. emotions · October 3, 2011

    This is very interesting! Great information and it is also very well written. I will bookmark and comeback soon.

    • satellitemundo · October 7, 2011

      Keep visiting for more… You can mail me your queries too.

      • Kamaldeep Singh · June 1, 2012

        Can you just send me a sample project where it is working fine because I am getting exception ….

        • satellitemundo · June 2, 2012

          Actually I dnt have any working example. This snippet is part of one of my project. But you can send me your project. I will resolve your error and give back to you.:)

  2. Anal Trivedi · December 30, 2011

    Thanks Hardik

    • satellitemundo · January 5, 2012

      Mention Not Anal, You can write me on hardik.trivedi_11@yahoo.com if you are having any doubts in android. keep visiting.

  3. Girish · February 6, 2012

    Thanks a lot. That solved my problem of scrolling textview inside scrollview. Was scratching my head to get this working. Didn’t find this info anywhere else. Thanks again

    • satellitemundo · February 7, 2012

      wc:)

  4. Jospeh Amiot · February 20, 2012

    This informative article was definitely a really wonderful examine. My partner and i was impressed through the items of the post. I cannot wait to see what in addition you’ve waiting for you for individuals. We are surely getting excited about your next article. Cheers with regard to revealing!

  5. Tashina Cascio · February 20, 2012

    Thanks , I’ve recently been searching for information about this subject for ages and yours is the best I have discovered till now. But, what about the conclusion? Are you sure about the source?

  6. Saggy · March 16, 2012

    THANKS A TON!

    I had exactly the same problem and your solution solved it. God bless you!:-)

  7. cppdev · November 21, 2012

    Hi Hardik,

    This approach is not working for me. If I use the same layout as you have described, it throws an exception saying java.lang.IllegalStateException: ScrollView can host only one direct child. I am working on Jelly Bean SDK.

    Please let me know if I am doing anything wrong.

    • Hardik Trivedi · November 22, 2012

      Hi,

      Yes you can not use more than one direct child for scrollview.
      Means your scrollview should not be like

      But it should be

  8. Laith · November 22, 2012

    Thanks, that was really awsome

    • Hardik Trivedi · November 22, 2012

      Thanks Laith

  9. vahid · December 6, 2012

    Thank you for your article, it is perfect.

    • Hardik Trivedi · December 6, 2012

      Thanks Vahid.

  10. Michael Herzberg (@DonMichihi) · January 8, 2013

    I wonder why it took me hours of searching on google to find your brilliant post. Works like a charm!

    • Hardik Trivedi · January 9, 2013

      Thanks buddy for the comment. Keep Visiting:)

  11. IKartik Shah · January 10, 2013

    works totally fine ,thanks:)

    • Hardik Trivedi · January 10, 2013

      Welcome Kartik:)

  12. Lynn Gobin · January 14, 2013

    Thanks much for helping me solve a problem with a scrollable TextView within a ScrollView!

    • Hardik Trivedi · January 16, 2013

      Welcome:)

  13. Developer · January 16, 2013

    Thanks much, this is simply great.

    • Hardik Trivedi · January 16, 2013

      Yea. Thanks:)

  14. Gideon · January 21, 2013

    Thank You, this was very helpful !

    • Hardik Trivedi · January 23, 2013

      Welcome:)

  15. Jules · February 13, 2013

    You are a superstar! Many thanks for that!

    • Hardik Trivedi · February 14, 2013

      Ha ha,
      Keep Visiting the blog, and let live the stardom😉

  16. Kets · February 14, 2013

    Hi.. I need to use a ScrollView inside a ListView. So that, whenever I scroll the screen the whole layout should scroll including EditText.

    My layout looks like

    Could you please suggest me the way to use parent scroll rather then list scroll.

    • Kets · February 14, 2013
    • Hardik Trivedi · February 14, 2013

      ScrollView should not have a listview as a child. Totally wrong approach.

      • Kets · February 14, 2013

        I have to do it forcefully.
        I am trying with your solution to scroll Listview and EditText simultaneously. Could you please guide me, will it work for me or not?

        • Hardik Trivedi · February 15, 2013

          Umm…. Can you send me that class and layout file or any needed things. And mention your problem in detail.
          Mail me on hardik.trivedi_11@yahoo.com

          • sumanthbvss · March 2, 2015

            is this problem needing a listview inside a scroll view and both needs to have scroll individually working solved?

            • Hardik Trivedi · March 2, 2015

              This is only for scrollviews, when there are multiple scrollviews inside one scrollview. For Listview inside scrollview I would prefer not to use ListView, you should use LinearLayout as container then inflate and add views dynamically inside that container.

  17. laxmi · March 29, 2013

    thanks for the code:)

    • Hardik Trivedi · March 30, 2013

      😀 You are welcome

  18. Muthu Lalapet · April 18, 2013

    Thank you very much. This helped me a lot

    • Hardik Trivedi · April 19, 2013

      Thanks Muthu

  19. Praveen · September 5, 2013

    Its working great lol… Thank you so much….

    • Hardik Trivedi · September 5, 2013

      Thanks Praveen

  20. charu · September 12, 2013

    hi,
    i hv a child scroll view inside parent scroll. The child scroll view id displayed at runtime on some click event. while it is visible on screen it does not scroll….
    i tried using ur method, bt it is not going inside onToch event of child… evn if i try to scroll the child, it goes inside touch event of parent….

    • Hardik Trivedi · September 12, 2013

      Can u post your xml and Java code here ?

      • charu · September 12, 2013
  21. charu · September 12, 2013

    here is the xml-

    scrollParent=(ScrollView)findViewById(R.id.scrollParent);
    tblDropdown = (TableLayout) findViewById(R.id.tblDropdown);
    linearDropdown = (LinearLayout) findViewById(R.id.linearDropdown);
    scrollDropdown = (ScrollView) findViewById(R.id.scrollDropdown);

    scrollParent.setOnTouchListener(new View.OnTouchListener() {

    public boolean onTouch(View v, MotionEvent event) {
    Log.v(TAG,”PARENT TOUCH”);
    findViewById(R.id.scrollDropdown).getParent().requestDisallowInterceptTouchEvent(false);
    return false;
    }
    });

    scrollDropdown.setOnTouchListener(new View.OnTouchListener() {

    public boolean onTouch(View v, MotionEvent event)
    {
    Log.v(TAG,”CHILD TOUCH”);
    // Disallow the touch request for parent scroll on touch of child view
    v.getParent().requestDisallowInterceptTouchEvent(true);
    return false;
    }
    });

    i hv even tried applying the touch event on tableDropdown and linearDropdown…..
    but even if i click on child scrollView , it goes inside parent onTouch event.

    • Hardik Trivedi · September 13, 2013

      Hm.. code seems fine. Have you tested in different OS versions ?

      • charu · September 20, 2013

        finally got it working…. bt the same does not work in jellybean….😦

        • charu · September 20, 2013

          actually it does not go inside onTouchListener of parent scoll as well as child scroll…

          • Hardik Trivedi · September 24, 2013

            Ok, as you are saying that code is working below 4.0. | also need to test that case. Let me check and get back to you.

  22. Zubair · September 13, 2013

    Thanks buddy!!!

    • Hardik Trivedi · September 13, 2013

      Welcome

  23. Pavan Parekh · November 1, 2013

    Thanks Dude!!!

  24. random · June 18, 2014

    Hi, I’m having problem with your code😦 I really need help on this.

    • Hardik Trivedi · June 18, 2014

      Let me know your problem.I would be happy to help

  25. rohitdroid · February 5, 2015

    best solution hardik.

  26. Praveen · February 23, 2015

    Reblogged this on My Blog.

  27. MICAH · June 6, 2015

    i guess its not too late to say thank you! it worked for me.

  28. Manmohan Singh · March 2

    So much thanks …It really works fine for me

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s