1

I'm coding an ebook reader. For paging I had the smart (stupid ?) idea to use a custom WebView. I changed it's behavior and it scrolls down of 1 page when I swipe left. It works really good expect on one point... The top and bottom text are often cut off. I got the height of my WebView and my font size but then I don't know what to do with that... Is there a way to have no cut off text at the top and at the bottom of my WebView ? It looks like they used the same same technic but with a ScrollView in the opensource PageTurner app but the code is quite complicated, I can't get the code part who could be interesting for me.

Problem with top and bottom lines

public class BookWebView extends WebView {
    private final String LOG_TAG = BookView.class.getSimpleName();
    private final int SWIPE_LIMIT = 80;
    private float startX,diffX;

    public BookWebView(Context context) {
        super(context);
    }

    public BookWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public BookWebView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                startX = event.getX();
                break;
            case MotionEvent.ACTION_UP:

                if(Math.abs(diffX) > SWIPE_LIMIT) {
                    int visibleTextHeight = this.getHeight();
                    if(diffX < 0) {
                        Log.d(LOG_TAG,"SCROLLING DOWN");
                        this.scrollTo(0, getScrollY() + visibleTextHeight);
                    } else {
                        Log.d(LOG_TAG,"SCROLLING UP");
                        this.scrollTo(0, getScrollY() - visibleTextHeight);
                    }

                } else {
                    Log.d(LOG_TAG, "==== NO SCROLL ====");
                    return false;
                }
                break;
            case MotionEvent.ACTION_MOVE:
                float endX = event.getX();
                diffX = endX - startX;
                break;
            case MotionEvent.ACTION_POINTER_UP:

                break;
        }

        return true;
    }
}

MainActivity

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        BookWebView bwvText = (BookWebView)findViewById(R.id.bwv_text);
        String text = "<html><body style=\"text-align:justify\"> %s </body></Html>";

        bwvText.loadData(String.format(text,getString(R.string.lorem_ipsum)),"text/html; charset=utf-8", "utf-8");

        WebSettings webSettings = bwvText.getSettings();
        int fontSize = webSettings.getDefaultFontSize();

        bwvText.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    bwvText.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                } else {
                    bwvText.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                }
                int webViewHeight = bwvText.getMeasuredHeight();
                adjustHeight(webViewHeight,fontSize);
            }
        });
    }


    private void adjustHeight(int webViewHeight, int fontSize) {
        // Is there something I can do here ?
    }

activity_main.xml

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.android.severalpagestext.MainActivity">

    <com.example.android.severalpagestext.BookWebView
        android:id="@+id/bwv_text"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</FrameLayout>
Laurent
  • 1,661
  • 16
  • 29
  • show xml please – IntelliJ Amiya Jan 23 '17 at 09:55
  • use `setOnScrollChangedListener` – IntelliJ Amiya Jan 23 '17 at 10:07
  • read http://www.artit-k.com/dev-custom-webview-for-android/ – IntelliJ Amiya Jan 23 '17 at 10:07
  • Thank you but this link is in Thai, I can't speak Thai. Anyway maybe I missed something but I don't think it can help me, it looks like it just get the WebView scroll position. – Laurent Jan 23 '17 at 10:26
  • Should it be `WebView` anyway? Have you seen [this solution](http://stackoverflow.com/questions/31837840/paginating-text-in-android/32096884#32096884) where `TextView` is used? If the solution is going to help you, please, don't forget to upvote it - it was quite a work for me :) – Onik Jan 23 '17 at 22:12
  • I first started with a TextView in a ScrollView using the same technic I used in my questions with a custom ScrollView instead of a custom WebView. But I had another problem, no text justification with a TextView... I still want to try a way to do this with a WebView and text justification before switching again to a TextView. If your link is useful in any way I'll not forget to upvote. – Laurent Jan 24 '17 at 02:28

0 Answers0