1

have EditText widget along with some widgets placed in ScrollView.

I have set the property of EditText with android:scrollbars = "vertical" to enable vertical scrolling inside editText.

Now when i launched the activity, editText has focus and it shows vertical scrollbars for some seconds.

The issue here is when i try to scroll within EditText, ScrollView moves.

How to enable scrolling within EditText and not within scrollview.

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/main"
        android:layout_width="match_parent"            
        android:layout_height="wrap_content"
        android:orientation="vertical" >
        .
        .
        .
        <EditText
            android:id="@+id/smset"
            android:layout_width="match_parent"
            android:gravity="top|left"
            android:height="100dip"
            android:inputType="textMultiLine" >
        </EditText>
        .
        .
        .
   </LinearLayout>
</ScrollView>
Ravibhushan
  • 171
  • 3
  • 14

5 Answers5

6

In your java file

     EditText dwEdit = (EditText) findViewById(R.id.DwEdit);       
     dwEdit.setOnTouchListener(new OnTouchListener() {
           public boolean onTouch(View view, MotionEvent event) {
                // TODO Auto-generated method stub
                if (view.getId() ==R.id.DwEdit) {
                    view.getParent().requestDisallowInterceptTouchEvent(true);
                    switch (event.getAction()&MotionEvent.ACTION_MASK){
                    case MotionEvent.ACTION_UP:
                        view.getParent().requestDisallowInterceptTouchEvent(false);
                        break;
                    }
                }
                return false;
            }
        });

In xml

   <EditText
    android:id="@+id/DwEdit"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:minLines="10"
    android:scrollbarStyle="insideInset"
    android:scrollbars="vertical" 
    android:overScrollMode="always"
    android:inputType="textCapSentences">
    </EditText> 
Amsheer
  • 7,046
  • 8
  • 47
  • 81
2

You can't place something that can scroll inside something that scrolls, too.

Android can't say for sure which element you want to be scrolling when you touch the screen so it isn't supported at all. (ignoring some hacks which are pretty bad practice).

WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
  • i need u help on scroll view can u help me on that – Developer Aug 06 '13 at 10:22
  • @Gaurav make a question, show what you have tried, explain your problem and use proper English (not "u" when it should be "your" etc). – WarrenFaith Aug 06 '13 at 10:24
  • i have posted this at http://stackoverflow.com/questions/18077726/scroll-view-is-not-working-properly-android please help me on this – Developer Aug 06 '13 at 10:33
  • @Gaurav no. You created your question and you will get your help there. Beside that you still use sms style typing which I personally hate... – WarrenFaith Aug 06 '13 at 10:41
0

One such hack is:

scroll.setOnTouchListener(new OnTouchListener()
{ 
    public boolean onTouch(View v, MotionEvent event) 
    {
        return true; 
    }
});

Note that to enable scrolling in ScrollView, you will have to run the same function, but with "return false;" instead.

Niall C.
  • 10,878
  • 7
  • 69
  • 61
Alex
  • 947
  • 1
  • 8
  • 25
0

I wouldn't say it's bad practice if done properly, but it's not something naturally expected.

I regularly put scrollable views inside something that scrolls, but you have to lock the outside layer and enable scrolling on the inside. Ideally, you should do something else, but it is possible.

You can modify ScrollView to be lockable, as in here.

Then programatically lock LockableScrollView scrolling when the internal thing is touched.

smset.setOnTouchListener(new OnTouchListener()
{
    public boolean onTouch(View v, MotionEvent event)
    {
        // Disables LockableScrollView when the EditText is touched
        if (event.getAction() == MotionEvent.ACTION_DOWN)
        {
            scrollView1.setScrollingEnabled(false);
        }

        // Enables LockableScrollView when the EditText is touched
        if (event.getAction() == MotionEvent.ACTION_UP)
        {
            scrollView1.setScrollingEnabled(true);
        }
        return false;
    }
});

The downside of this is that it will disable scrolling when the EditText is touched, but it shouldn't cause any other side-effects.

Community
  • 1
  • 1
Muz
  • 5,866
  • 3
  • 47
  • 65
0

In kotlin, you can write an extension function for all ExitTextViews and use them everywhere :

fun EditText.setTouchForScrollBars() {
    setOnTouchListener { view, event ->
        view.parent.requestDisallowInterceptTouchEvent(true)
        when (event.action and MotionEvent.ACTION_MASK) {
            MotionEvent.ACTION_UP -> view.parent.requestDisallowInterceptTouchEvent(false)
        }
        false
    }
}

but add these lines in your EditTextView XML too:

android:scrollbarStyle="insideInset"
android:scrollbars="vertical"
android:overScrollMode="always"
android:inputType="textMultiLine"
Amin Keshavarzian
  • 3,646
  • 1
  • 37
  • 38