Stop ScrollView from auto-scrolling to an EditText
Solution 1:
After struggling with that problem for quite some time, I've found a solution that seems to work without being too ugly. First, make sure that whatever ViewGroup
(directly) contains your EditText
has descendantFocusability
set to "Before Descendants," focusable
set to "true" and focusableInTouchMode
set to "true." This will not be the ScrollView
itself, but the layout inside where you have your various views. Next add an onTouchListener
to your ScrollView
that removes focus from the EditText
whenever it is touched, like so:
ScrollView scroll = (ScrollView)findViewById(R.id.scrollView1);
scroll.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (myEditText.hasFocus()) {
myEditText.clearFocus();
}
return false;
}
});
Tell me if that doesn't fix it. What should happen is that the Layout gets focus instead of the EditText
, so no scrolling should happen.
Solution 2:
Just create an empty view at the top of linearlayout
<View android:layout_width="fill_parent" android:id="@+id/focus_view" android:layout_height="0dp" android:focusable="true" android:focusableInTouchMode="true"><requestFocus/></View>
Single line solves the problem