Stop ScrollView from setting focus on EditText
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;
}
});
ScrollView disable focus move
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.
Remove Focus While Scrolling Multiline EditText
I found a solution (only works on short texts). You can use the ScrollView
onScrollChange event.
This is how I did it:
ScrollView scroll = findViewById(R.id.scrollView1);
scroll.setOnScrollChangeListener(new View.OnScrollChangeListener() {
@Override
public void onScrollChange(View view, int i, int i1, int i2, int i3) {
et.clearFocus();
}
});
The variable et
is an EditText object.
How to disable default scroll behavior of an EditText?
Set your EditText
's height to wrap_content
so it expands instead of scrolling. Set your LinearLayout
's height to wrap_content
so it's the size of its contents, i.e. the TextView
and EditText
.
The ScrollView
should be whatever height you want for your "window" into the contents layout, the LinearLayout
that moves up and down "behind" the ScrollView
.
The scrolling happens if that contents layout is too big to display in the visible ScrollView
, which is why that contents layout should always be wrap_content
(the size of the contents) and never match_parent
(the size of the scroll view "window"). If the contents are always the same size as the scroll view, they'll never need to scroll, right?
RecyclerView: Disable scrolling caused by change in focus
I ended up with this solution from @pskink:
recylerView.setLayoutManager(new LinearLayoutManager(this) {
@Override
public boolean requestChildRectangleOnScreen(RecyclerView parent, View child, Rect rect, boolean immediate) {
return false;
}
});
It seems to work perfectly, but @pskink has mentioned that this could have problems when using arrow keys. He's posted another solution here: https://pastebin.com/8JLSMkF7. If you have problems with the above solution, you may try the alternative solution at the link. For now, I'm sticking with the one I just posted here.
UPDATE
Since support-library v25.3.0 you should also override another requestChildRectangleOnScreen method in LayoutManager:
@Override
public boolean requestChildRectangleOnScreen(RecyclerView parent, View child, Rect rect, boolean immediate, boolean focusedChildVisible) {
return false;
}
How to stop EditText from gaining focus when an activity starts in Android?
Adding the tags android:focusableInTouchMode="true"
and android:focusable="true"
to the parent layout (e.g. LinearLayout
or ConstraintLayout
) like in the following example, will fix the problem.
<!-- Dummy item to prevent AutoCompleteTextView from receiving focus -->
<LinearLayout
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="0px"
android:layout_height="0px"/>
<!-- :nextFocusUp and :nextFocusLeft have been set to the id of this component
to prevent the dummy from receiving focus again -->
<AutoCompleteTextView android:id="@+id/autotext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:nextFocusUp="@id/autotext"
android:nextFocusLeft="@id/autotext"/>
Related Topics
Server-Side Verification of Google Play In-App Billing Version 3 Purchase
Single Observable with Multiple Subscribers
How to Display Both Icon and Title of Action Inside Actionbar
Android 4.0, Text on the Action Bar Never Shows
Android Toolbar: Small Title Text in Landscape Mode
Tiled Drawable Sometimes Stretches
Android Marshmallow: Test Permissions with Espresso
No Resource Identifier Found for Attribute '...' in Package 'Com.App....'
Running Emulator After Building Android from Source
How to Change App Name Per Gradle Build Type
How to Cast Action Provider to Share Action Provider
Layout Weights Do Not Work Inside a Scrollview
How to Implement Google Maps Search by Address in Android
Download and Extract Zip File in Android
Android - How to Display a Dialog Over a Native Screen
"No Enclosing Instance of Type" Error While Calling Method from Another Class in Android
How to Read/Write a Boolean When Implementing the Parcelable Interface