Scrolling EditText inside ScrollView
Asked Answered
D

3

9

I have a nasty problem. I have EditText(8 lines) inside ScrollView. And when I'm trying to scroll text in EditText it's behavior is not stable. Sometimes it's scrolling, some times it's not taking focus.

This is my layout file, to make my question more clear:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="8dp" >

    <TextView
        android:id="@+id/wo_task_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/no_description" />

    <TextView
        android:id="@+id/wo_task_description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/no_description" />

    <TextView
        android:id="@+id/wo_task_comments_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="@string/comments" />

    <Button
        android:id="@+id/wo_task_select_comment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/select_comment_from_template" />

    <EditText
        android:id="@+id/wo_task_comments"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="top|left"
        android:hint="@string/enter_your_comment_here"
        android:lines="8"
        android:maxLines="10"
        android:minLines="6"
        android:scrollbars="vertical"
        android:singleLine="false" />
</LinearLayout>

I understand that I'm having this issue because of holding one scrollable control inside another, yet I don't know what to do with this. So, please help me if you can.

Thanks in advance.

public void initComments(final View view) {
    EditText comment = (EditText) view.findViewById(R.id.wo_task_comments);

    comment.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(final View v, final MotionEvent motionEvent) {
            if (view.getId() == R.id.wo_task_comments) {
                view.getParent().requestDisallowInterceptTouchEvent(true);
                switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {
                case MotionEvent.ACTION_UP:
                    view.getParent().requestDisallowInterceptTouchEvent(
                            false);
                    break;
                }
            }
            return false;
        }
    });

    comment.setText(currentTask.getComment() + "very very long comment"
            + "very very long comment\n" + "very very long comment\n"
            + "very very long comment\n" + "very very long comment\n"
            + "very very long comment");
}

I've tried this, and no result. I'm still unable to scroll my editbox.

Dynamics answered 19/3, 2012 at 12:31 Comment(0)
M
32

Try:

@Override
public void onCreate(Bundle savedInstanceState) {
    .....  
    EditText et = (EditText)findViewById(R.id.wo_task_comments);
    et.setOnTouchListener(this);
    .....
}

@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    if(view.getId() == R.id.wo_task_comments){
        view.getParent().requestDisallowInterceptTouchEvent(true);
        switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_UP:
                view.getParent().requestDisallowInterceptTouchEvent(false);
                break;
        }
    }
    return false;
}

In your case:

public class MyActivity extends Activity  {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    initComments(findViewById(R.id.YOUR_MAIN_LAYOUT_ID));  
}


public void initComments(final View view) {
    EditText comment = (EditText) view.findViewById(R.id.wo_task_comments);

    comment.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(final View v, final MotionEvent motionEvent) {
            if (v.getId() == R.id.wo_task_comments) {
                v.getParent().requestDisallowInterceptTouchEvent(true);
                switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {
                    case MotionEvent.ACTION_UP:
                        v.getParent().requestDisallowInterceptTouchEvent(
                                false);
                        break;
                }
            }
            return false;
        }
    });

    comment.setText("very very long comment"
            + "very very long comment\n" + "very very long comment\n"
            + "very very long comment\n" + "very very long comment\n"
            + "very very long comment\n" + "very very long comment\n"
            + "very very long comment\n" + "very very long comment\n"
            + "very very long comment\n" + "very very long comment\n"
            + "very very long comment\n" + "very very long comment\n"
            + "very very long comment\n" + "very very long comment\n"
            + "very very long comment\n" + "very very long comment\n"
            + "very very long comment");
}

}

Mashe answered 19/3, 2012 at 12:40 Comment(3)
Was just posting the same answer, as an addition you maybe have to disable other scrolling otherviews.setOnTouchListener(new OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { return true; } });Liebman
thanks for response. For the moment I'm still unable to fix my problem.Dynamics
Yes, I was just to post that it was my mistake. Well, shit happens, I hate such mistakes:) Thanks for help.Dynamics
B
1

Shilkin's code works great.I make it a little more stronger.Now it handle the situation that the EditText's direct parent is not the ScrollView(eg, EditText is wrapped in LinearLayout before it put into the scrollview).

The method code:

public static void handleEditTextScrollable(EditText editText, final int resId) {
    editText.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (v.getId() == resId) {
                ViewParent parent = v.getParent();
                while (!(parent instanceof ScrollView)) {
                    parent = parent.getParent();
                }
                parent.requestDisallowInterceptTouchEvent(true);

                switch (event.getAction() & MotionEvent.ACTION_MASK) {
                    case MotionEvent.ACTION_UP:
                        parent.requestDisallowInterceptTouchEvent(false);
                        break;
                }
            }
            return false;
        }
    });
}

Call it like this:

handleEditTextScrollable(comment, R.id.wo_task_comments);
Bordiuk answered 17/12, 2014 at 7:50 Comment(0)
O
0

One Option is to set the height of the child EditText as the height of the total max lines (max_lines * font_height). So, the internal scroll will be off and the only scroll will be the parent scroll.

Obscure answered 19/3, 2012 at 13:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.