How to Make a Scroll Listener for Webview in Android

How to make a Scroll Listener for WebView in Android

Something like:

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)
{
super.onScrollChanged(l, t, oldl, oldt);
if(mOnScrollChangedCallback != null) mOnScrollChangedCallback.onScroll(l, t, oldl, oldt);
}

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, int oldl, int oldt);
}
}

Should work, this is untested but this works for almost every other view in Android.

You would implement like:

wv = (ObservableWebView) findViewById(R.id.scorllableWebview);
wv.setOnScrollChangedCallback(new OnScrollChangedCallback(){
public void onScroll(int l, int t, int oldl, int oldt){
if(t> oldt){
//Do stuff
System.out.println("Swipe UP");
//Do stuff
}
else if(t< oldt){
System.out.println("Swipe Down");
}
Log.d(TAG,"We Scrolled etc...");
}
});

onScrollListener for WebViewClient Android

This is a potential duplicate of How to make a Scroll Listener for WebView in Android. But perhaps some guidance on how to implement the solution in your situation might be more helpful.

First off, you create class, call it ObservableWebView which extends WebView. You can look at the class definition here.

Secondly, as stated here you need to "upgrade" your Webview XML into:

 <com.YourPackageName.ObservableWebView
android:id="@+id/activity_main_webview"
android:layout_width="fill_parent"
android:clickable="false"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_above="@id/buttonBar"
android:visibility="gone" />

Finally, you implement the actual scroll listener like this:

//First, lookup the "custom" `ObservableWebView` **instead** of `WebView`
ObservableWebView yourObservableWebView = (ObservableWebView) findViewById(R.id.activity_main_webview);
//then set the scroll callback
yourObservableWebView.setOnScrollChangedCallback(new OnScrollChangedCallback(){
public void onScroll(int l, int t){
String testString = Integer.toString(t);
Log.d("Testing", testString);
}
});

I hope this helps you understand what you need to do to implement the suggested solutions.

Detect End of scroll position custom webView

see this post it may be useful

you can also use
setOnScrollChange method event for api23

Android webview : detect scroll

I found a solution which work for me, I check the source code of class WebView in 2.3 API and find how to do it with a 2.1 API. Maybe it can work with older API:

public class CustomWebView extends WebView {

private float oldX;

// indicate if horizontal scrollbar can't go more to the left
private boolean overScrollLeft = false;

// indicate if horizontal scrollbar can't go more to the right
private boolean overScrollRight = false;

// indicate if horizontal scrollbar can't go more to the left OR right
private boolean isScrolling = false;

public CustomWebView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}


public CustomWebView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}



public CustomWebView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}

@Override
public boolean onTouchEvent(MotionEvent event) {
// width of the vertical scrollbar
int scrollBarWidth = getVerticalScrollbarWidth();

// width of the view depending of you set in the layout
int viewWidth = computeHorizontalScrollExtent();

// width of the webpage depending of the zoom
int innerWidth = computeHorizontalScrollRange();

// position of the left side of the horizontal scrollbar
int scrollBarLeftPos = computeHorizontalScrollOffset();

// position of the right side of the horizontal scrollbar, the width of scroll is the width of view minus the width of vertical scrollbar
int scrollBarRightPos = scrollBarLeftPos + viewWidth - scrollBarWidth;

// if left pos of scroll bar is 0 left over scrolling is true
if(scrollBarLeftPos == 0) {
overScrollLeft = true;
} else {
overScrollLeft = false;
}

// if right pos of scroll bar is superior to webpage width: right over scrolling is true
if(scrollBarRightPos >= innerWidth) {
overScrollRight = true;
} else {
overScrollRight = false;
}

switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: // when user touch the screen
// if scrollbar is the most left or right
if(overScrollLeft || overScrollRight) {
isScrolling = false;
} else {
isScrolling = true;
}
oldX = event.getX();
break;

case MotionEvent.ACTION_UP: // when user stop to touch the screen
// if scrollbar can't go more to the left OR right
// this allow to force the user to do another gesture when he reach a side
if(!isScrolling) {
if(event.getX() > oldX && overScrollLeft) {
// left action
}

if(event.getX() < oldX && overScrollRight) {
// right actio
}
}

break;
default:
break;
}
return super.onTouchEvent(event);
}

}

I want to check my webview is scrolled down or not

ok try this

view.setOnScrollChangeListener(new View.OnScrollChangeListener() {
@Override
public void onScrollChange(View v, int scrollX, int scrollY, int
oldScrollX, int oldScrollY) {

}
});

here view is your WebView object. :) its native method so it will work fine.



Related Topics



Leave a reply



Submit