Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Properly Scrolling an EditText into view when focused

I've got an EditText, which is ultimately inside of a ScrollView. I've implemented a comment feature which takes you to a new activity, and automatically places focus in the edit text so that the user can immediately start writing his comment.

Unfortunately, it doesn't quite scroll the edittext into view, as you can see in the screenshot below:

Improper Scrolling

I would like to see something more like this, where the EditText comes completely into view (see below). I already looked at the android:WindowSoftInputMode, and it seems like the default values should work ... and indeed, it does mostly work because it does scroll, just not enough.

What I'd like to see

So is there anything I can do to get the desired behavior? Thanks!

like image 533
Joel Martinez Avatar asked Mar 16 '12 14:03

Joel Martinez


People also ask

Why can't I scroll through the edittext in a ScrollView?

There's quite a few fields on this view, including a large EditText for the post content field. It appears that when an EditText is in a ScrollView, the ScrollView takes over the scrolling action, so the user can't scroll within the EditText area if they are writing a large post.

How to scroll an element into view using JavaScript?

While this is by far the most reliable and resilient option to scroll an element into view, it is not always possible and you have to rely on a bit of JavaScript instead. For a long time, the most reliable way to do this was to use jQuery and scroll the body to the top offset of the element you want to scroll into view:

Why edittext is not shown in the screen?

When user presses the edittext, keyboard becames visible and the visible area of the scrollview becames small. Because of that edittext does not shown in the screen. (it does not scroll the scrollview so that edit text will be shown.) After user presses any key in the keyboard, scrollview scrolls so that edittext becames visible again.

How to scroll the body to the top of the page?

For a long time, the most reliable way to do this was to use jQuery and scroll the body to the top offset of the element you want to scroll into view: If you want to have a smooth scroll animation as well, you could use jQuery’s animate () function like so: $ ('html, body').animate ( { scrollTop: $ (element).offset ().top }, 500);


2 Answers

is your min SDK 3? check this

Hope you have tried android:windowSoftInputMode="adjustPan" and check this.

I would give this a go.

like image 184
san Avatar answered Sep 28 '22 05:09

san


You could also try to programatically use this on the onCreate() method of you activity.

getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

"SOFT_INPUT_ADJUST_PAN" adjustment option is set to have a window pan when an input method is shown, so it doesn't need to deal with resizing but just panned by the framework to ensure the current input focus is visible.

like image 38
B.B. Avatar answered Sep 28 '22 05:09

B.B.