45

I have a ScrollView inside which is an EditText which is set to scroll vertically. But it does not scrolls. Instead the whole layout scrolls, Whenever i try to scroll the EditText. Below is the code -

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<TextView
    android:id="@+id/b1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="39dp"
    android:text="Title"
    android:textColor="#3bb9ff"
    android:textSize="15sp" />

<EditText
    android:id="@+id/Text1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="Title"
    android:singleLine="true" >

    <requestFocus />

</EditText>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Content"
    android:layout_marginTop="50dp"
    android:textColor="#3bb9ff"
    android:textSize="15sp"
   />


<EditText
    android:id="@+id/newTodoText"
    android:layout_width="fill_parent"
    android:layout_height="match_parent" 
    android:minLines="2"
    android:maxLines="7"
     android:hint="Write something"
     android:scrollbars = "vertical" >
</EditText>
<Button
    android:id="@+id/Add"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="Add" />


</LinearLayout>
</ScrollView>

The EditText with id "newTodoText" is in question here.

Yasin Kaçmaz
  • 5,867
  • 4
  • 34
  • 54
Naddy
  • 2,584
  • 6
  • 23
  • 38
  • possible duplicate of [Scrolling editbox inside scrollview](http://stackoverflow.com/questions/9770252/scrolling-editbox-inside-scrollview) – Elltz Jun 05 '15 at 17:11

12 Answers12

103
EditText EtOne = (EditText) findViewById(R.id.EditText01);
EtOne.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (v.getId() == R.id.EditText01) {
            v.getParent().requestDisallowInterceptTouchEvent(true);
            switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_UP:
                v.getParent().requestDisallowInterceptTouchEvent(false);
                break;
            }
        }
        return false;
    }
});
Prakash Pazhanisamy
  • 967
  • 1
  • 13
  • 25
Amsheer
  • 6,666
  • 8
  • 39
  • 74
  • 4
    Yes really..This code worked ..Straight and simple...Awesome Amsheer...Great Thanks – John Aug 07 '15 at 09:58
  • 3
    Top notch answer but one question, is it possible to only register this touch interception(scroll the edit text) when there is enough content in the edit text for it to be scrollable? – Jack.Ramsden Nov 20 '15 at 09:29
  • @Jack.Ramsden in that case you would wrap the whole content inside the onTouch, except the return statment inside a if conditions, that gets the EditText length end checks of its bigger than a certains amount of characters (50 for example!). – Anaximandro Andrade Oct 16 '16 at 15:45
  • is it necessary to remove the disallow intercept on ACTION_UP? I thought this is naturally reset at the end of the gesture. – hmac Dec 02 '20 at 14:59
37

You have to just replace your <ScrollView ></ScrollView> with this Custom ScrollView like <com.example.VerticalScrollview > </com.example.VerticalScrollview >

package com.example;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.ScrollView;

public class VerticalScrollview extends ScrollView{

    public VerticalScrollview(Context context) {
        super(context);
    }

     public VerticalScrollview(Context context, AttributeSet attrs) {
            super(context, attrs);
        }

        public VerticalScrollview(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        final int action = ev.getAction();
        switch (action)
        {
            case MotionEvent.ACTION_DOWN:
                    Log.i("VerticalScrollview", "onInterceptTouchEvent: DOWN super false" );
                    super.onTouchEvent(ev);
                    break;

            case MotionEvent.ACTION_MOVE:
                    return false; // redirect MotionEvents to ourself

            case MotionEvent.ACTION_CANCEL:
                    Log.i("VerticalScrollview", "onInterceptTouchEvent: CANCEL super false" );
                    super.onTouchEvent(ev);
                    break;

            case MotionEvent.ACTION_UP:
                    Log.i("VerticalScrollview", "onInterceptTouchEvent: UP super false" );
                    return false;

            default: Log.i("VerticalScrollview", "onInterceptTouchEvent: " + action ); break;
        }

        return false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        super.onTouchEvent(ev);
        Log.i("VerticalScrollview", "onTouchEvent. action: " + ev.getAction() );
         return true;
    }
}
Sandeep
  • 2,708
  • 20
  • 27
  • 2
    same I tried for Fragment view..it's not working..any suggestion? – CoDe Apr 16 '14 at 11:12
  • Sorry... But this Answer cost me an Hour The right answer is at:http://stackoverflow.com/questions/24428808/how-to-scroll-the-edittext-inside-the-scrollview Look for: Ayman Mahgoub Answer – Gal Rom Jun 16 '16 at 17:25
  • As commented by @Shubh this is not working with fragment, this is also not working with imageview and button. This is just working with textview. – Anand Kumar Jha Aug 08 '16 at 08:16
18
mEdtText1.setOnTouchListener(new View.OnTouchListener() { 
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                mScrlMain.requestDisallowInterceptTouchEvent(true);
                return false;
            }
        });

// change hear mEdtText1 with your edit text object and also change mScrlMain with your scroll view object they work definitely.

Dhaval Solanki
  • 4,093
  • 1
  • 21
  • 33
7

you should use NestedScrollView class. This class support child scrolling inside parent scrolling. This class can be a child or a parent.

<android.support.v4.widget.NestedScrollView         
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#d6d8d9">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:maxLines="512"
                android:text=" your content"/>
        <android.support.v4.widget.NestedScrollView
            android:layout_below="@id/ll_button"
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:background="#d6d8d9">

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content" 
                android:text="your content"
                android:maxLines="512"/>    
        </android.support.v4.widget.NestedScrollView>       
    </LinearLayout>

</android.support.v4.widget.NestedScrollView> 
HungNM2
  • 1,370
  • 18
  • 15
6
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

This will make your entire layout scrollable.

For setting your edittext scrollable add android:scrollbars="vertical" to your edittext

In your code its already written

<EditText
    android:id="@+id/newTodoText"
    android:layout_width="fill_parent"
    android:layout_height="match_parent" 
    android:minLines="2"
    android:maxLines="7"
     android:hint="Write something"
     android:scrollbars = "vertical" >

Remove the from code.It will work fine

tasomaniac
  • 9,559
  • 5
  • 45
  • 79
anjaly
  • 508
  • 5
  • 11
2

Use this Code Its Working

in Xml

android:singleLine="false"
android:overScrollMode="always"
android:scrollbarStyle="insideInset"
android:scrollbars="vertical"

in Pragraming

et_feedback.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View view, MotionEvent event) {

                if (view.getId() == R.id.et_feedback) {
                    view.getParent().requestDisallowInterceptTouchEvent(true);
                    switch (event.getAction() & MotionEvent.ACTION_MASK) {
                        case MotionEvent.ACTION_UP:
                            view.getParent().requestDisallowInterceptTouchEvent(false);
                            break;
                    }
                }
                return false;
            }
        });
Keshav Gera
  • 8,200
  • 1
  • 56
  • 43
1
noteEdit.setMovementMethod(new ScrollingMovementMethod());
    ScrollingMovementMethod.getInstance();


    noteEdit.setOnTouchListener(new View.OnTouchListener() {

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

            noteEdit.getParent().requestDisallowInterceptTouchEvent(true);

            return false;
        }


    });
1

A simple way to do so is mentioned below with some description

            myEditText.setOnTouchListener(new View.OnTouchListener() {  //Register a callback to be invoked when a touch event is sent to this view.
            @Override
            public boolean onTouch(View v, MotionEvent event) {     
//Called when a touch event is dispatched to a view. This allows listeners to get a chance to respond before the target view.
                mScrollView.requestDisallowInterceptTouchEvent(true);
                return false;
            }
        });
Summved Jain
  • 1,126
  • 19
  • 21
0
  • You have set ScrollView as your parent most layout. That's reason your whole layout gets scrolled.
  • As, far as your EditText is concerned, you have used android:scrollbars = "vertical" which means if the EditText grows a vertical srollBar will appear. You will get the scrollbar and scrolling effect only if edittext has data high enough..

Hope this helps...

CRUSADER
  • 6,189
  • 3
  • 31
  • 60
  • Even if the data is high enough,the EditText is not scrollable. how can i make it scrollable? – Naddy May 17 '13 at 09:42
0

XML code

    android:gravity="top"
    android:inputType="text|textMultiLine"
    android:lines="5"

Use below code for Kotlin

    editText.setOnTouchListener { v, event ->
        if (v.id == R.id.editText) {
            v.parent.requestDisallowInterceptTouchEvent(true)
            when (event.action and MotionEvent.ACTION_MASK) {
                MotionEvent.ACTION_UP -> v.parent.requestDisallowInterceptTouchEvent(false)
            }
        }
        false
    }
Pratik Dodiya
  • 559
  • 4
  • 8
0

I had a similar problem inside a bottom sheet that was swiping to close, I suspect this will work in general though:

@SuppressLint("ClickableViewAccessibility")
fun View.blockAllAncestorsInterceptingTouch() {
    setOnTouchListener { _, _ ->
        parent?.requestDisallowInterceptTouchEvent(true)
        false
    }
}

The suppress Lint is legitimate, there is no need to call performClick if you always return false.

hmac
  • 222
  • 2
  • 9
-2

You have set "Scroll View" to your parent - this means it scrolls the whole layout.

If you want to scroll a particular "Edit text" then you have to put another Scroll view to that edit text.

Pavan tej
  • 171
  • 1
  • 2
  • 12