Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ScrollView disable focus move

I have a simple input form; it's a vertical LinearLayout with EditTexts inside a ScrollView.

<ScrollView android:layout_width="fill_parent"     android:layout_height="wrap_content">     <LinearLayout android:layout_width="fill_parent"             android:layout_height="fill_parent"             android:orientation="vertical">             <LinearLayout android:layout_width="fill_parent"                   android:layout_height="wrap_content"                   android:padding="10dip"                   android:gravity="center_vertical"                   android:orientation="horizontal">                   <TextView style="@style/Text"                          android:text="Name"/>                   <EditText style="@style/EditBox"/>             </LinearLayout>             <View style="@style/Divider"/>             <LinearLayout android:layout_width="fill_parent"                   android:layout_height="wrap_content"                   android:padding="10dip"                   android:gravity="center_vertical"                   android:orientation="horizontal">                   <TextView style="@style/Text"                          android:text="Password"/>                   <EditText style="@style/EditBox"/>             </LinearLayout>                            ...       </LinearLayout> </ScrollView> 

When the user scrolls the form, it automatically moves its focus to the visible EditText. It is possible to disable such behavior and always keep focus on the EditText currently selected by touch?

I understand that this may be a feature, but I need to disable it.

Thanks!

like image 678
AlexKorovyansky Avatar asked Mar 21 '11 09:03

AlexKorovyansky


2 Answers

Just thought I'd share my solution to this. Even though some of the other answer's comments state that you cannot override this behavior, that is not true. This behavior stops as soon as you override the onRequestFocusInDescendants() method. So simply create your ScrollView extension to do this:

public class NonFocusingScrollView extends ScrollView {    public NonFocusingScrollView(Context context) {     super(context);   }    public NonFocusingScrollView(Context context, AttributeSet attrs) {     super(context, attrs);   }    public NonFocusingScrollView(Context context, AttributeSet attrs, int defStyle) {     super(context, attrs, defStyle);   }    @Override   protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {     return true;   }  } 

And you're done. The ScrollView will mess with your focus no more.

like image 167
dmon Avatar answered Sep 27 '22 23:09

dmon


I have had such a problem too. The only way that helped me is to extend scroll view and to override neigher

@Override public ArrayList<View> getFocusables(int direction) {     return new ArrayList<View>(); } 

or

@Override protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {     return true; } 

but to override ViewGroup.requestChildFocus(View child, View focused) method as following:

    @Override     public void requestChildFocus(View child, View focused) {         // avoid scrolling to focused view         // super.requestChildFocus(child, focused);     } 
like image 25
rus1f1kat0R Avatar answered Sep 28 '22 01:09

rus1f1kat0R