Home Ask Login Register

Developers Planet

Your answer is one click away!

MultiJ February 2016

WebView zooming with scroll limiting

The basic idea of what I want to achieve is to be able to zoom into the content within a WebView, but limit the scrolling so that the content doesn't show some padding that is at the top and bottom when zoomed in. I know the height of the padding in question.

I've currently extended the WebView class, and overridden onScrollChanged so that I can get information on when we are scrolling and how far. This is all easy enough, and works fine (i.e. I get the info I need).

The problem comes when trying to limit the scrolling itself. I can correctly limit it so that the top padding is never visible, but for some reason I cannot fathom the bottom just will not limit correctly.

Here's the code I currently have within onScrollChanged:

    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        float totalPadding = 250 * scaleFactor;
        float padding = totalPadding / 2;

        float paddingTopStart = padding;
        float paddingBottomStart = (contentHeight * scaleFactor) + paddingTopStart;

        if (t > paddingBottomStart) {
            setScrollY((int) paddingBottomStart);
        } else if (t < paddingTopStart) {
            setScrollY((int) paddingTopStart);

The scale factor is obtained in onScaleChanged and saved as a class variable. totalPadding is the overall amount of padding (padding at the top + padding at the bottom). The padding is equal on both sides, hence padding = totalPadding / 2.

The paddingBottomStart is what I assume is the issue. I was certain that the maths involved was correct, yet it still doesn't work. If anyone has any suggestions they would be most welcome.


MultiJ February 2016

Turns out that the reason my calculations weren't coming out as correct is because value t is actually the scroll value at the top of the screen. Consequently I needed to take this into account when doing the calculations!

Post Status

Asked in February 2016
Viewed 1,391 times
Voted 13
Answered 1 times


Leave an answer

Quote of the day: live life