Android: Make the button visible as soon as the webview is scrolled

I have a webview that shows an html file. When the user scrolls to the bottom of this file in webview, I want a button that was previously hidden to show, which the user can then click to perform some activity.

I did something similar in iOS, where I just set the delegate in the ViewController and just set the button as visible. How do I do something like this on Android? I noticed that in iOS there is no callback method.

Edit: Right now, I have an activity with two objects: a webview containing my text, and a button that is currently invisible. I want my activity to receive a message when webview text scrolls to the bottom and makes the button visible.

+5
android scroll delegates android-webview
source share
5 answers

I had to do it myself in order to display the β€œI Agree” button as soon as the user scrolled to the bottom of the EULA. Lawyers, huh?

In fact, when you override WebView (and not ScrollView, as in the answer from @JackTurky), you can call getContentHeight () to get the height of the content, rather than getBottom (), which returns the visible bottom and is not useful.

This is my complete solution. As far as I can see, these are all Level 1 APIs, so it should work anywhere.

public class EulaWebView extends WebView { public EulaWebView(Context context) { this(context, null); } public EulaWebView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public EulaWebView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public OnBottomReachedListener mOnBottomReachedListener = null; private int mMinDistance = 0; /** * Set the listener which will be called when the WebView is scrolled to within some * margin of the bottom. * @param bottomReachedListener * @param allowedDifference */ public void setOnBottomReachedListener(OnBottomReachedListener bottomReachedListener, int allowedDifference ) { mOnBottomReachedListener = bottomReachedListener; mMinDistance = allowedDifference; } /** * Implement this interface if you want to be notified when the WebView has scrolled to the bottom. */ public interface OnBottomReachedListener { void onBottomReached(View v); } @Override protected void onScrollChanged(int left, int top, int oldLeft, int oldTop) { if ( mOnBottomReachedListener != null ) { if ( (getContentHeight() - (top + getHeight())) <= mMinDistance ) mOnBottomReachedListener.onBottomReached(this); } super.onScrollChanged(left, top, oldLeft, oldTop); } } 

I use this to display the "I Agree" button when the user scrolls to the bottom of the WebView, where I call it that (in a class that "implements OnBottomReachedListener":

 EulaWebView mEulaContent; Button mEulaAgreed; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.eula); mEulaContent = (EulaWebView) findViewById(R.id.eula_content); StaticHelpers.loadWebView(this, mEulaContent, R.raw.stylesheet, StaticHelpers.readRawTextFile(this, R.raw.eula), null); mEulaContent.setVerticalScrollBarEnabled(true); mEulaContent.setOnBottomReachedListener(this, 50); mEulaAgreed = (Button) findViewById(R.id.eula_agreed); mEulaAgreed.setOnClickListener(this); mEulaAgreed.setVisibility(View.GONE); } @Override public void onBottomReached(View v) { mEulaAgreed.setVisibility(View.VISIBLE); } 

So, when the bottom is reached (or in this case, when they fall into 50 pixels), the button "I agree" appears.

+3
source share

[I can not comment on the answer, so I leave a comment here as a new answer]

The answer to karora (first) works very well, except in

 protected void onScrollChanged(int left, int top, int oldLeft, int oldTop) 

defiant

 getContentHeight() 

was wildly inaccurate for me. He reported that the value is too small, so my listener was called when the user scrolled, maybe a third of the WebView path. I used

 computeVerticalScrollRange() 

and it’s wonderful. Thanks to this post for this useful hint.

+1
source share

try the following:

 @Override protected void onScrollChanged(int l, int t, int oldl, int oldt) { View view = (View) getChildAt(getChildCount()-1); int diff = (view.getBottom()-(getHeight()+getScrollY()));// Calculate the scrolldiff if( diff == 0 ){ // if diff is zero, then the bottom has been reached Log.d(ScrollTest.LOG_TAG, "MyScrollView: Bottom has been reached" ); yourButton.setVisible(true); } super.onScrollChanged(l, t, oldl, oldt); } 

To implement this, extend the ScrollView and override the onScrollChanged method (inherited from View).

0
source share

Download / Visible button only if the web view is reached / scrolled to the bottom.

Create JavaScript class:

 public class JavaScriptInterface { @android.webkit.JavascriptInterface public void didScrollToBottom() { Log.d(TAG, "Scroll to Bottom"); myHandler.post(new Runnable() { @Override public void run() { btnAccept.setVisibility(View.VISIBLE); } }); } } 

In onCreate ():

 final JavaScriptInterface jsInterface = new JavaScriptInterface(); myWebView.addJavascriptInterface(jsInterface, "AndroidFunction"); 
0
source share

The solutions given above did not fully work for me on a similar problem (hide the button while the webView scrolls, shows after scrolling). The reason I wanted it to be hidden while scrolling is because the button I want to hide is to jump to the very bottom of the web browser, and when it works only for me, when the web view is static but will not jump to the bottom while the view is still scrolling. So I did the following:

added onScrollChanged callback for redefined webView, for example, proposed next:

 private OnScrollChangedCallback mOnScrollChangedCallback; public OnScrollChangedCallback getOnScrollChangedCallback() { return mOnScrollChangedCallback; } public void setOnScrollChangedCallback( final OnScrollChangedCallback onScrollChangedCallback) { mOnScrollChangedCallback = onScrollChangedCallback; } @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.onScrollChanged(l, t); } } /** * Implement in the activity/fragment/view that you want to listen to the * webview */ public static interface OnScrollChangedCallback { public void onScrollChanged(int l, int t); } 

and in my activity class that implements OnScrollChangedCallback

UPDATED:

 Timer timer2showJumpButton; private long lastScrollEventTimestamp; public final static int HIDING_JUMP_BUTTON_ON_SCROLL_DELAY = 500; public void onScrollChanged(int l, int t) { // showing button when scrolling starts if (btnJumpToBottom != null) { btnJumpToBottom.setVisibility(View.VISIBLE); } if (btnJumpToTop!= null) { btnJumpToTop.setVisibility(View.VISIBLE); } if (timer2showJumpButton == null) { final Runnable r2 = new Runnable() { @Override public void run() { if (btnJumpToBottom != null) { btnJumpToBottom.setVisibility(View.GONE); } if (btnJumpToTop!= null) { btnJumpToTop.setVisibility(View.GONE); } } }; TimerTask timerTask = new TimerTask() { @Override public void run() { if (btnJumpToTop.getVisibility() == View.VISIBLE || btnJumpToBottom.getVisibility() == View.VISIBLE){ long currentTimestamp = System.currentTimeMillis(); if (currentTimestamp - lastScrollEventTimestamp > HIDING_JUMP_BUTTON_ON_SCROLL_DELAY1 ){ webView.postDelayed(r2, HIDING_JUMP_BUTTON_ON_SCROLL_DELAY); }else{ //too soon } } } }; try { timer2showJumpButton = new Timer(); timer2showJumpButton.schedule(timerTask, 500, 500); } catch (IllegalStateException e) { logger.warn(TAG + "/onScrollChanged/" + e.getMessage()); } } // adding runnable which will hide button back long currentTimestamp = System.currentTimeMillis(); lastScrollEventTimestamp = currentTimestamp; } 
-one
source share

All Articles