Skip to content

Instantly share code, notes, and snippets.

@ManzzBaria
Created November 4, 2015 09:29
Show Gist options
  • Save ManzzBaria/bb9e4dda407ddfd474dc to your computer and use it in GitHub Desktop.
Save ManzzBaria/bb9e4dda407ddfd474dc to your computer and use it in GitHub Desktop.
Webview with showing page percentage
package org.bethalevi.sidur.objects;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.webkit.WebView;
public class ObservableWebView extends WebView
{
private OnScrollChangedCallback mOnScrollChangedCallback;
public ObservableWebView(final Context context)
{
super(context);
}
public ObservableWebView(final Context context, final AttributeSet attrs)
{
super(context, attrs);
}
public ObservableWebView(final Context context, final AttributeSet attrs, final int defStyle)
{
super(context, attrs, defStyle);
}
@Override
protected void onScrollChanged(final int l, final int t, final int oldl, final int oldt)
{
// http://www.gaanza.com/blog/measure-view-android/
int height = (int) Math.floor(this.getContentHeight() * this.getScale());
int webViewHeight = this.getBottom() - this.getTop();
int totalHieght = t+webViewHeight;
int percent=0;
percent = 100 * totalHieght;
percent = percent / height;
Log.e("TAG","Percentage: "+percent);
super.onScrollChanged(l, t, oldl, oldt);
if(mOnScrollChangedCallback != null) mOnScrollChangedCallback.onScroll(l, t);
}
public OnScrollChangedCallback getOnScrollChangedCallback()
{
return mOnScrollChangedCallback;
}
public void setOnScrollChangedCallback(final OnScrollChangedCallback onScrollChangedCallback)
{
mOnScrollChangedCallback = onScrollChangedCallback;
}
/**
* Impliment in the activity/fragment/view that you want to listen to the webview
*/
public static interface OnScrollChangedCallback
{
public void onScroll(int l, int t);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment