Whenever I start this activity, it always starts bottomed out--scrolled all the way to the bottom. I am not doing anything weird in the activity OnCreate (or anywhere for th
Instead of polluting items in your layout, you can add this into your Activity/Class:
ScrollView scrollView = (ScrollView) findViewById(R.id.scrollView);
scrollView.setFocusableInTouchMode(true);
scrollView.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
This way it isn't an element within your scrollView getting focus, it's your scrollView as a container which get's the first focus, moving onto the child view's after this.
In my case I had a VideoView
inside a ConstraintLayout
inside a ScrollView
. The VideoView
was always stealing focus, and there are issues with it. Solution is to use a TextureView
. Hopefully this helps someone, it cost me an hour at least :)
Just make it not focusable in onCreate()
editText.setFocusable(false);
and use
editText.setOnClickListener(new ....){
......
editText.setFocusable(true);
editText.setFocusableInTouchMode(true);
editText.requestFocus();
showSoftKeyboard(); //Use InputMethodManager to open soft Keyboard
......
};
and it will not scroll down to Edittext next time, a fragment or activity is created.
scrollView.requestFocus(View.FOCUS_UP)
solved my problem when I wanted the top view to be visible.
Specifying direction parameter to the method requestFocus(int direction)
is a good way to control the ScrollView
position. More details here.
Have you tried use fullScroll method ?http://developer.android.com/reference/android/widget/ScrollView.html#fullScroll(int)
yourScrollView.fullScroll(ScrollView.FOCUS_UP);
When Android starts an activity, some control needs to take focus. When there's no designated control to take focus, the system chooses the first eligible control that wants focus.
If you set the following property in your LinearLayout - android:focusableInTouchMode="true"
your LinearLayout
will be focused on start and your activity won't scroll to EditText
in the bottom.