2

I use a ViewPager and inside the first fragment of the ViewPager I have a another fragment that is parenting a sub fragment with ScrollView in it. to make it more visual:

┌------------┐
|   1        | 1 is the ViewPager fragment
| ┌---------┐|
| | 2       || 2 is the fragment inside ViewPager fragment
| |┌-------┐||
| ||3      ||| 3 is the sub fragment containing the ScrollView with EditText form
| ||form   |||
| ||here   |||
| ||       |||
| |└-------┘||
| └---------┘|
└------------┘

problem: the view adjust but the scroll view cannot scroll till the end of the view and some of the content remains behind the keyboard. this behaviour changes when I select the edit texts from bottom of the form. in that case, the fragment 1 (viewpager) and fragment 2(sub fragment) move up and allow the scroll view to be show even the last element.

while searching for this behaviour I saw many posts on SO such as:

Scrollview inside a viewpager fragment doesn't scroll when keyboard is shown

and ofcourse the original bug report for fullscreen activities which has been marked as intentional behaviour. note that my case I am not using a full screen activity. but there has been reports of similar behaviour for ScrollView in ViewPager.

my attempt to use the Workaround:

public class AndroidBug5497Workaround {

// For more information, see https://code.google.com/p/android/issues/detail?id=5497
// To use this class, simply invoke assistActivity() on an Activity that already has its content view set.

public static void assistActivity (Activity activity) {
    new AndroidBug5497Workaround(activity);
}

private View mChildOfContent;
private int usableHeightPrevious;
private FrameLayout.LayoutParams frameLayoutParams;

private AndroidBug5497Workaround(Activity activity) {
    FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content);
    mChildOfContent = content.getChildAt(0);
    mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        public void onGlobalLayout() {
            possiblyResizeChildOfContent();
        }
    });
    frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams();
}

private void possiblyResizeChildOfContent() {
    int usableHeightNow = computeUsableHeight();
    if (usableHeightNow != usableHeightPrevious) {
        int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight();
        int heightDifference = usableHeightSansKeyboard - usableHeightNow;
        if (heightDifference > (usableHeightSansKeyboard/4)) {
            // keyboard probably just became visible
            frameLayoutParams.height = usableHeightSansKeyboard - heightDifference;
        } else {
            // keyboard probably just became hidden
            frameLayoutParams.height = usableHeightSansKeyboard;
        }
        mChildOfContent.requestLayout();
        usableHeightPrevious = usableHeightNow;
    }
}

private int computeUsableHeight() {
    Rect r = new Rect();
    mChildOfContent.getWindowVisibleDisplayFrame(r);
    return (r.bottom - r.top);
}

}

The issue is when I use the workaround it works, but it will also create a extra white space that increases when I click on the lower EditTexts in the form inside scrollview. I suspect it has to do with adjust_resize or miscalculating the height in workaround.

here is the result of the of the workaround.

when I select an EditText from top of the form (little extra white space)

enter image description here

when I select an EditText from end of the Form (a lot of extra white space)

enter image description here

your helps are appreciated!

Poorya
  • 1,291
  • 6
  • 27
  • 57
  • did you get any solution? In my case, I have tab layout(at the bottom of screen) inside a view pager fragment. Using workaround class my tab layout is disappearing and white space issue occurring. – BHARADWAJ Marripally Jul 03 '18 at 06:55

1 Answers1

0

Have you tried android:fillViewport="true" inside ScrollView if not then try this.

And use this in your manifiest's Activity which contain your ViewPager

android:windowSoftInputMode="adjustResize"

Hope it will help you.

Vishal Chhodwani
  • 2,567
  • 5
  • 27
  • 40
  • I have tried using `adjustResize` and `adjustPan`. when i use `adjustResize` it will work great with scrolling the `ScrollView`. but in small screens `sub fragment 3` hides behind the `soft keyboard`. when i use 'adjustPan' in small screens it will push the parent fragments 1 and 2 up and show the fragment 3. but `ScrollView` doesn't scroll till the end and some of the content hides behind keyboard. [here I have also posted a question with more details on that subject](https://stackoverflow.com/questions/45330600/adjustpan-and-adjustresize-in-nested-fragment) thanks for helping – Poorya Jul 26 '17 at 15:09
  • to further explain the issue i have uploaded a mini project containing the relevant code to facilitate the process. I would appreciate if you take a look [Github Repo Link](https://github.com/pouriabagheri/ScrollViewAdjustViewPager) – Poorya Jul 28 '17 at 13:36