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, Whene
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.
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
}
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;
}
}
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;
}
});
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.
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;
}
});