Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop ScrollView from setting focus on EditText

Android's ScrollView (when scrolled or fling'd) likes to set the focus of an EditText when it's one of it's children. It happens when you scroll and then release. Is there anyway to stop this behavior? I've tried nearly everything I can think of and everything I've read on StackOverflow. Nothing has worked for me. Below is an example layout If you'd like to test it out. I'm desperate to stop this dumb behavior.

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" >  <ScrollView      android:id="@+id/scrollView"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     >  <LinearLayout      android:orientation="vertical"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:layout_marginRight="50dp"     android:focusable="true"     android:focusableInTouchMode="true"     >      <EditText          android:layout_width="fill_parent"          android:layout_height="100dp"          android:text="TestApp 1"         />       <Button          android:id="@+id/btn"         android:layout_width="fill_parent"          android:layout_height="wrap_content"          android:text="TestApp 1"         />      <Button          android:id="@+id/btn"         android:layout_width="fill_parent"          android:layout_height="wrap_content"          android:text="TestApp 1"         />      <Button          android:id="@+id/btn"         android:layout_width="fill_parent"          android:layout_height="wrap_content"          android:text="TestApp 1"         />      <EditText          android:layout_width="fill_parent"          android:layout_height="100dp"          android:text="Bleh...."         />      <Button          android:id="@+id/btn"         android:layout_width="fill_parent"          android:layout_height="wrap_content"          android:text="TestApp 1"         />     <EditText          android:layout_width="fill_parent"          android:layout_height="100dp"          android:text="TestApp 1"         />       <Button          android:id="@+id/btn"         android:layout_width="fill_parent"          android:layout_height="wrap_content"          android:text="TestApp 1"         />      <Button          android:id="@+id/btn"         android:layout_width="fill_parent"          android:layout_height="wrap_content"          android:text="MeeEEEEEEEEEEEEE"         />      <Button          android:id="@+id/btn"         android:layout_width="fill_parent"          android:layout_height="wrap_content"          android:text="TestApp 1"         />    </LinearLayout> </ScrollView>  </LinearLayout> 
like image 361
user123321 Avatar asked Nov 11 '11 22:11

user123321


2 Answers

This works. Not sure if it's the best solution or not.

// SCROLL VIEW HACK     // BOGUS     ScrollView view = (ScrollView)findViewById(R.id.scrollView);     view.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);     view.setFocusable(true);     view.setFocusableInTouchMode(true);     view.setOnTouchListener(new View.OnTouchListener() {         @Override         public boolean onTouch(View v, MotionEvent event) {             v.requestFocusFromTouch();             return false;         }     }); 
like image 155
user123321 Avatar answered Sep 18 '22 11:09

user123321


First, you can remove the parent of the ScrollView. Second, add focus options for the child(only one child per ScrollView):

android:descendantFocusability="beforeDescendants" android:focusable="true" android:focusableInTouchMode="true" 

This is how your xml should look like:

<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"     android:id="@+id/scrollView"     android:layout_width="match_parent"     android:layout_height="match_parent" >      <LinearLayout         android:layout_width="match_parent"         android:layout_height="match_parent"         android:layout_marginRight="50dp"         android:descendantFocusability="beforeDescendants"         android:focusable="true"         android:focusableInTouchMode="true"         android:orientation="vertical" >          <EditText             android:id="@+id/editText_one"             android:layout_width="match_parent"             android:layout_height="100dp"             android:text="TestApp 1" />          <Button             android:id="@+id/btn_one"             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:text="TestApp 1" />          <Button             android:id="@+id/btn_two"             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:text="TestApp 1" />          <Button             android:id="@+id/btn_three"             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:text="TestApp 1" />          <EditText             android:id="@+id/editText_two"             android:layout_width="match_parent"             android:layout_height="100dp"             android:text="Bleh...." />          <Button             android:id="@+id/btn_four"             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:text="TestApp 1" />          <EditText             android:id="@+id/editText_three"             android:layout_width="match_parent"             android:layout_height="100dp"             android:text="TestApp 1" />          <Button             android:id="@+id/btn_five"             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:text="TestApp 1" />          <Button             android:id="@+id/btn_six"             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:text="MeeEEEEEEEEEEEEE" />          <Button             android:id="@+id/btn_seven"             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:text="TestApp 1" />     </LinearLayout>  </ScrollView> 

Also, notice that you have multiple id's with the same name. Try giving them unique names.


If you want only to hide the soft-keyboard, and let the EditText still have it's focus, you can do this by adding the following property to your activity in manifest:

android:windowSoftInputMode="stateHidden" 
like image 35
Ionut Negru Avatar answered Sep 18 '22 11:09

Ionut Negru