Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EditText not scrollable inside ScrollView

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, Whenever i try to scroll the EditText. Below is the code -

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<TextView
    android:id="@+id/b1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="39dp"
    android:text="Title"
    android:textColor="#3bb9ff"
    android:textSize="15sp" />

<EditText
    android:id="@+id/Text1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="Title"
    android:singleLine="true" >

    <requestFocus />

</EditText>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Content"
    android:layout_marginTop="50dp"
    android:textColor="#3bb9ff"
    android:textSize="15sp"
   />


<EditText
    android:id="@+id/newTodoText"
    android:layout_width="fill_parent"
    android:layout_height="match_parent" 
    android:minLines="2"
    android:maxLines="7"
     android:hint="Write something"
     android:scrollbars = "vertical" >
</EditText>
<Button
    android:id="@+id/Add"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="Add" />


</LinearLayout>
</ScrollView>

The EditText with id "newTodoText" is in question here.

like image 832
Naddy Avatar asked May 17 '13 09:05

Naddy


5 Answers

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;
    }
});
like image 91
Amsheer Avatar answered Nov 07 '22 04:11

Amsheer


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;     } } 
like image 23
Sandeep Avatar answered Nov 07 '22 05:11

Sandeep


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.

like image 33
Dhaval Solanki Avatar answered Nov 07 '22 03:11

Dhaval Solanki


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> 
like image 40
HungNM2 Avatar answered Nov 07 '22 04:11

HungNM2


<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

This will make your entire layout scrollable.

For setting your edittext scrollable add android:scrollbars="vertical" to your edittext

In your code its already written

<EditText
    android:id="@+id/newTodoText"
    android:layout_width="fill_parent"
    android:layout_height="match_parent" 
    android:minLines="2"
    android:maxLines="7"
     android:hint="Write something"
     android:scrollbars = "vertical" >

Remove the from code.It will work fine

like image 45
anjaly Avatar answered Nov 07 '22 03:11

anjaly