Asked  6 Months ago    Answers:  5   Viewed   29 times

I have a WebView that is loading a page from the Internet. I want to show a ProgressBar until the loading is complete.

How do I listen for the completion of page loading of a WebView?

 Answers

61

Extend WebViewClient and call onPageFinished() as follows:

mWebView.setWebViewClient(new WebViewClient() {

   public void onPageFinished(WebView view, String url) {
        // do your stuff here
    }
});
Tuesday, June 1, 2021
 
Wookai
answered 6 Months ago
94

The framework itself does not provide this for you (as far as I know). Translating true/false into yes/no does not strike me as more common than other potential translations (such as on/off, checked/unchecked, read-only/read-write or whatever).

I imagine that the easiest way to encapsulate the behavior is to make an extension method that wraps the construct that you suggest yourself in your question:

public static class BooleanExtensions
{
    public static string ToYesNoString(this bool value)
    {
        return value ? Resources.Yes : Resources.No;
    }
}

Usage:

bool someValue = GetSomeValue();
Console.WriteLine(someValue.ToYesNoString());
Friday, June 11, 2021
 
JakeGR
answered 6 Months ago
18

After visiting the above links, I come up with this code and hope this helps.

wv.getSettings().setSupportMultipleWindows(true);
wv.setWebChromeClient(new WebChromeClient() {
    @Override
    public boolean onCreateWindow(WebView view, boolean dialog, boolean userGesture, android.os.Message resultMsg)
    {
        WebView.HitTestResult result = view.getHitTestResult();
        String data = result.getExtra();
        Context context = view.getContext();
        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(data));
        context.startActivity(browserIntent);
        return false;
    }
});
Wednesday, June 23, 2021
 
SheppardDigital
answered 6 Months ago
52

Don't include the method body in the class body. Write both classes, and after both classes are complete, write the method implementations:

class Y;
class X {
  …
  void Connect(Y* Y_in);
  …
};
class Y {
  …
  void Connect(X* X_in);
  …
};
inline void X::Connect(Y* Y_in) {
  pY = Y_in;
  Y_in->SetXPointer(this);
}
inline void Y::Connect(X* X_in) {
  pX = X_in;
  X_in->SetXPointer(this);
}

That way, full information about how the objects of the class will be layed out in memory is available by the time the Connect method is implemented. And as a method in the class body and a method declared inline will both be inlined the same way, performance will be the same as well.

The only downside is that you won't be able to split these two classes over two headers in a reasonable way.

Wednesday, July 28, 2021
 
Manju
answered 4 Months ago
90

In onPageFinished():

view.loadUrl("javascript:"
            + "var FunctionOne = function () {"
            + "  var r = $.Deferred();"
            + "  try{document.getElementsByClassName('header')[0].style.display='none';}catch(e){}"
            + "  try{document.getElementById('section_0').style.display='none';}catch(e){}"
            + "  try{document.getElementById('page-actions').style.display='none';}catch(e){}"
            + "  try{document.getElementsByClassName('languageSelector')[0].style.display='none';}catch(e){}"
            + "  try{document.getElementById('mw-mf-last-modified').style.display='none';}catch(e){}"
            + "  try{document.getElementById('footer').style.display='none';}catch(e){}"
            + "  setTimeout(function () {"
            + "    r.resolve();"
            + "  }, 2500);"
            + "  return r;"
            + "};"
            + "var FunctionTwo = function () {"
            + "  window.CallToAnAndroidFunction.setVisible();"
            + "};"
            + "FunctionOne().done(FunctionTwo);");

In MainActivity.onCreate():

this.webView.addJavascriptInterface(new JsObject(webView, loadingView), "CallToAnAndroidFunction");

In MainActivity():

public class JsObject {
    private View loadingView;
    private View view;
    JsObject(View view, View loadingView){this.view = view;this.loadingView = loadingView;}
    @JavascriptInterface
    public void setVisible(){
        runOnUiThread(new Runnable() {

           @Override
           public void run() {
               view.setVisibility(View.VISIBLE);  
                loadingView.setVisibility(View.INVISIBLE);               
           }
       });
    }
}

So, it was a combination of making a JavascriptInterface and making a JS function to wait for the JS calls to finish before calling the interface (with the visibility settings).

Wednesday, September 8, 2021
 
0skar
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share