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.
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>