I have a scrollview inside which there is a editext which is multiline. I want to scroll the edittext to see the lower content but it can\'t be done.
Add this method in main activity:
It worked for me in Fragment
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
View v = getCurrentFocus();
if (v instanceof EditText) {
Rect outRect = new Rect();
v.getGlobalVisibleRect(outRect);
if (!outRect.contains((int) event.getRawX(), (int) event.getRawY())) {
v.clearFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
}
}
return super.dispatchTouchEvent(event);
}
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>
Use this code it's working
In XML
android:singleLine="false"
android:overScrollMode="always"
android:scrollbarStyle="insideInset"
android:scrollbars="vertical"
In programming
edittext.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View view, MotionEvent event) {
if (view.getId() == R.id.edittext) {
view.getParent().requestDisallowInterceptTouchEvent(true);
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_UP:
view.getParent().requestDisallowInterceptTouchEvent(false);
break;
}
}
return false;
}
});
Try this..
Add below lines into your EditText
android:overScrollMode="always"
android:scrollbarStyle="insideInset"
android:scrollbars="vertical"
Example
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="15dp"
android:background="#eeeeee"
android:inputType="textMultiLine"
android:singleLine="false"
android:overScrollMode="always"
android:scrollbarStyle="insideInset"
android:scrollbars="vertical"
android:text="Android applications normally run entirely on a single thread by default the “UI thread” or the “main thread”.
android:textAppearance="?android:attr/textAppearanceMedium" >
</EditText>
EDIT
Programmatically
youredittext.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (youredittext.hasFocus()) {
v.getParent().requestDisallowInterceptTouchEvent(true);
switch (event.getAction() & MotionEvent.ACTION_MASK){
case MotionEvent.ACTION_SCROLL:
v.getParent().requestDisallowInterceptTouchEvent(false);
return true;
}
}
return false;
}
});
Building upon the answer by @Ayman Mahgoub and @Hariharan. The solution worked great except while scrolling the edit text there is no scroll momentum. As soon as the finger lifts up, the scrolling immediately stops.
To gain scroll momentum wrap a scrollview around the EditText and let the EditText's height wrap content (set minHeight if you'd like). Remove the following lines from the edit text:
android:scrollbarStyle="insideInset"
android:scrollbars="vertical"
android:overScrollMode="always"
Now the nested ScrollView wrapped around the EditText takes care of the scrolling. Update the onTouch handler to take into account the updated view hierarchy:
public boolean onTouch(View view, MotionEvent event) {
v.getParent().getParent().requestDisallowInterceptTouchEvent(true);
switch (event.getAction() & MotionEvent.ACTION_MASK){
case MotionEvent.ACTION_UP:
v.getParent().getParent().requestDisallowInterceptTouchEvent(false);
break;
}
return false;
Be warned, this solution is now tightly coupled to the view hierarchy.
Gist: https://gist.github.com/atoennis/7549fba2634d0abccddf
This will scroll the EditText
and make it editable:
First in the XML file, add this:
android:overScrollMode="always"
android:scrollbarStyle="insideInset"
android:scrollbars="vertical"
Then in the Java file, add this:
EditText.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
view.getParent().requestDisallowInterceptTouchEvent(true);
switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_SCROLL:
view.getParent().requestDisallowInterceptTouchEvent(false);
return true;
case MotionEvent.ACTION_BUTTON_PRESS:
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(EditText, InputMethodManager.SHOW_IMPLICIT);
}
return false;
}
});