Ravibhushan
Ravibhushan

Reputation: 171

EditText scroller and ScrollView

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>

Upvotes: 1

Views: 6435

Answers (5)

Amin Keshavarzian
Amin Keshavarzian

Reputation: 3963

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"

Upvotes: 0

Amsheer
Amsheer

Reputation: 7141

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> 

Upvotes: 6

Muz
Muz

Reputation: 5980

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.

Upvotes: 0

Alex
Alex

Reputation: 1101

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.

Upvotes: 0

WarrenFaith
WarrenFaith

Reputation: 57702

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).

Upvotes: 2

Related Questions