0

I have an activity that hosts a fragment, and several others.

This activity already has fitsSystemWindows=false so that it will extend it's boundaries towards the very bottom of the screen, I am hiding the Navigation Bar btw.

The problem is, it has a fragment that has a RichEditor view inside it which means users should be able to put multiple lines of text inside that fragment. SO I made that fragment fitsSystemWindows=true so that when users start clicking on the RichEditor and types, the adjustResize flag, which I have set in the styles, will fire up and allow the user to scroll the page.

It resizes and allows the user to scroll yes.

The only problem is, when the fragment resizes, it gets additional padding on the top and bottom and since I have a custom toolbar on the main activity, it is very awkwardly visibly being added on screen. The funny thing is, the top padding is exactly the same as the height of the device's status bar and the bottom padding is exactly the same as the height of the Navigation Bar.

I tried making the clipToPadding attribute in the fragment's layout into true and false but it does not stop the fragment from getting the additional paddings. I also searched around but I can't get anything useful to get around this as most of the questions I encounter are about how to ALLOW the resize to happen and not anything about how to remove or stop the padding.

Is there even any way to know when the fragment has been resized or the resize has been triggered? If ever I can use that listener to remove the additional paddings as needed.

Any thoughts on this?

EDIT I already tried putting fitsystemwindows=true in the main activity, but it gets some padding on the bottom for the navigation bar. And I need the activity to fill the whole screen as the navigation bar is hidden

John Ernest Guadalupe
  • 6,379
  • 11
  • 38
  • 71
  • maybe fitsSystemWindows=true in activity. – tiny sunlight Nov 15 '15 at 15:17
  • To detect fragment resizes you could use `view.getViewTreeObserver().addOnGlobalLayoutListener()` and remove the padding there. – Matej Snoha Nov 15 '15 at 17:23
  • @Matej I have thought about that. And I think the easiest way to do it is to put that listener on the fragment and maybe just add the padding on the bottom that is the same height as the keyboard and remove the fitsSystemWindows attr on the fragment layout. Is it possible to get the height of the keyboard on screen? – John Ernest Guadalupe Nov 15 '15 at 23:19
  • I think you can only get the keyboard height by measuring the difference in your views, like [this](http://stackoverflow.com/a/16789324/3522053). Do you see this behaviour with padding also if you set `fitsSystemWindows=true` everywhere? – Matej Snoha Nov 17 '15 at 20:47
  • Actually I have already implemented the listener that adds padding whenever the keyboard shows so the user can scroll. It will also remove it when the keyboard is hidden. The curious thing is, this only happens in Fragments. Putting fitsSystemWindows=true on Activitiess does not result to this behavior – John Ernest Guadalupe Nov 18 '15 at 00:31

1 Answers1

0

I have solved this by following Matej Snoha's comment. This method attaches a listener to the fragment and when it sees the heightDifference, which is the size of the keyboard as well, is more than the height of the navigation bar, it puts a padding to the bottom of the view thus allowing it to scroll while the keyboard is open.

public class FragmentResizeUtil {

public static void setListenerforResize(final View root, Fragment fragment) {
    final int navigationbarHeight = getNavigationBarHeight(fragment);
    root.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        public void onGlobalLayout() {
            Rect r = new Rect();
            root.getWindowVisibleDisplayFrame(r);

            int screenHeight = root.getRootView().getHeight();
            int heightDifference = screenHeight - (r.bottom - r.top);

            if (heightDifference > navigationbarHeight) {
                root.setPadding(0, 0, 0, heightDifference);
            } else {
                root.setPadding(0, 0, 0, 0);
            }
        }
    });
}

private static int getNavigationBarHeight(Fragment fragment) {
    Resources resources = fragment.getResources();
    int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
    if (resourceId > 0) {
        return resources.getDimensionPixelSize(resourceId);
    }
    return 0;
}
}
John Ernest Guadalupe
  • 6,379
  • 11
  • 38
  • 71