onPageFinished() sometimes doesn't get called - android

I have a custom implementation of WebViewClient that overrides onPageFinished(), where I swap my loading view with the WebView. Sometimes everything works great, but other times onPageFinished() doesn't get called at all. It's completely random.
onPageStarted() always gets called.
public class LoaderWebViewClient extends WebViewClient {
private final TableLayout swapTableLayout;
public LoaderWebViewClient(TableLayout swapTableLayout) {
this.swapTableLayout = swapTableLayout;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
// Sometimes doesn't get called
#Override
public void onPageFinished(WebView view, String url) {
swapTableLayout.removeAllViews();
swapTableLayout.addView(view);
super.onPageFinished(view, url);
}
}
I am instantiating a WebView from a custom class:
WebView webView = new WebView(context);
WebViewClient webViewClient = new LoaderWebViewClient(swapTableLayout);
webView.setWebViewClient(webViewClient);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.setVerticalScrollBarEnabled(false);
webView.setHorizontalScrollBarEnabled(false);
webView.getSettings().setJavaScriptEnabled(true);
webView.setOverScrollMode(WebView.OVER_SCROLL_NEVER);
webView.loadUrl(url);

I have found the problem. In onPageFinished() I am removing all the views from the layout inside which the WebView itself would reside (as seen in the next line).
And that somehow disrupts the loading of the WebView. I have just replaced removing and reinserting views with hiding and showing them (they are always inflated in the layout) and everything seems to work fine.

Related

Why did my WebView didn't load the owl-carousel content?

I have a webview that will visit a website, the problem here is that
[it didn't load the entire website content
][1]
Then I tried to use Chrome to access the website, and [it loads all the websites content][2](even though a bit messed up since it viewed in mobile).
When I tried to see the content of the missing things (things that didn't showed up in my WebView), I learned it didn't load the content that use owl-carousel,
Is there a way to let my WebView load the owl-carousel content?
P.s I have activated the javascript too
Here's the codes I create that I think lead to the problem
My Webview settings
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setAllowFileAccessFromFileURLs(true);
webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setSupportMultipleWindows(true);
webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
webView.getSettings().setPluginState(WebSettings.PluginState.ON);
webView.getSettings().setBuiltInZoomControls(true);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.setScrollbarFadingEnabled(false);
And here's my Webview procedure
public class myWebclient extends WebViewClient {
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return super.shouldOverrideUrlLoading(view, url);
}
#Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.proceed();
}
}
Progress Update
I have tried using AdvancedWebview like what Sudheesh R said, but it's still like previous condition nothing changed, it still don't load the owl-carousel content
Update #2
I̶ ̶t̶r̶i̶e̶d̶ ̶u̶s̶i̶n̶g̶ ̶m̶y̶ ̶f̶r̶i̶e̶n̶d̶ ̶W̶e̶b̶V̶i̶e̶w̶ ̶a̶p̶p̶,̶ ̶a̶n̶d̶ ̶i̶t̶ ̶l̶o̶o̶k̶s̶ ̶l̶i̶k̶e̶ ̶i̶t̶ ̶w̶o̶r̶k̶e̶d̶ ̶o̶n̶ ̶h̶i̶s̶ ̶a̶p̶p̶ ̶b̶u̶t̶ ̶I̶ ̶d̶o̶n̶'̶t̶ ̶k̶n̶o̶w̶ ̶w̶h̶y̶,̶ ̶I̶ ̶w̶i̶l̶l̶ ̶u̶p̶d̶a̶t̶e̶ ̶i̶f̶ ̶I̶ ̶f̶o̶u̶n̶d̶ ̶o̶u̶t̶ ̶h̶o̶w̶.
Alright, I have compared his code with mine, nothing seems comes to light...
Update 3
The only resource I found is this question, but as you see in my code above, I have set the Dom Storage to become enabled... Any help really appreciated
Update 4
Alright, I found out that there's a Jquery method in the website that I haven't initialize yet from my Logcat
So I initialize it but keeping it empty
#JavascriptInterface
public void getxId(){
}
[1]: https://i.stack.imgur.com/3lPNX.png
[2]: https://i.stack.imgur.com/FS6tV.png

How to add onCompletionListener on WebView.loadUrl() method?

WebView is now showing but before that a blank white screen is appearing which might be due to loading of the url.
// webview to load url
webView.loadUrl("https://www.google.com/");
I want to add onCompletionListener when the url is fully loaded and webview is ready to display the conent.
try this one
How can I know that my WebView is loaded 100%?
Or this Url
https://android--code.blogspot.com/2016/03/android-detect-when-webview-finish.html
// Set a WebViewClient for WebView
mWebView.setWebViewClient(new WebViewClient(){
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon){
// Page loading started
}
/*
public void onPageFinished (WebView view, String url)
Notify the host application that a page has finished loading. This
method is called only for main frame. When onPageFinished() is called,
the rendering picture may not be updated yet. To get the notification
for the new Picture, use onNewPicture(WebView, Picture).
Parameters
view WebView: The WebView that is initiating the callback.
url String: The url of the page.
*/
#Override
public void onPageFinished(WebView view, String url){
// Page loading finished
Toast.makeText(mContext,"Page Loaded.",Toast.LENGTH_SHORT).show();
}
});
// Set a WebChromeClient for WebView
// Another way to determine when page loading finish
mWebView.setWebChromeClient(new WebChromeClient(){
/*
public void onProgressChanged (WebView view, int newProgress)
Tell the host application the current progress of loading a page.
Parameters
view WebView: The WebView that initiated the callback.
newProgress int: Current page loading progress, represented by an
integer between 0 and 100.
*/
public void onProgressChanged(WebView view, int newProgress){
mTextView.setText("Page loading : " + newProgress + "%");
if(newProgress == 100){
// Page loading finish
mTextView.setText("Page Loaded.");
}
}
});
// Enable JavaScript
mWebView.getSettings().setJavaScriptEnabled(true);
// Load the url in the WebView
mWebView.loadUrl(mURL);
}
You just need to implement WebViewClient and use onPageFinished() as follows:
mWebView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
// do what you want
}
});
Read more about this here.
Just add this one
webView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
// do whatever you want
}
});
refrance here

How to make a WebView visible only after it finished displaying content?

I have a webView, where I display a HTML I fetch from the backend.My problem is that I am trying to display a loading message, and show the content only after the page is done.
For doing that, I tried to use onPageFinished, which winda works, but not entirely, because it is called after the data is fetched, but BEFORE the page is displayed, so I'm still displaying a blank screen for about 1 second, before finally displaying the HTML.
From the oficial docs:
public void onPageFinished (WebView view, String url)
Added in API level 1
Notify the host application that a page has finished loading. This method is called only for main frame. When onPageFinished() is called, the rendering picture may not be updated yet. To get the notification for the new Picture, use onNewPicture(WebView, Picture).
The problem is that onNewPicture(WebView, Picture) is deprecated
So my question is, is there a way to know when the page is finished, but also fully displayed?
Here is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.messageId = this.getIntent().getStringExtra(ITEM_ID_KEY);
this.setUpActionBar();
this.setContentView(R.layout.activity_inbox_item_detail);
WebView view = (WebView) this.findViewById(R.id.webview);
view.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
view.setVisibility(View.VISIBLE);
}
});
this.fetchInboxDetailsTask(this.messageId);
}
I have a WebView in a fragment and, just for your reference, I have set it up like this.
May be you are missing something. "onPageFinished" works just fine for me.
In onViewCreated method:
mWebView = (WebView) view.findViewById(R.id.appWebView);
mWebView.setWebViewClient(new WebViewClient()
{
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
// Handle the error
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url)
{
webViewProgressBar.setVisibility(View.GONE);
super.onPageFinished(view, url);
}
});
and then:
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.getSettings().setSupportZoom(true);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.loadUrl("http://play.google.com/store/apps/");
You may consider using the Advanced Webview Library for android it provides an interface to reaspond to when page start loading and when finishes! here (AdvancedWebView)

WebView does not resize correctly after being loaded

I'm having trouble resizing my WebView after loading JQuery Flot graph API.
So, here's my code:
webview.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
// view.setVisibility(View.INVISIBLE);
progressBar1.setVisibility(View.VISIBLE);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar1.setVisibility(View.INVISIBLE);
webview.setInitialScale(100);
webview.getSettings().setLoadWithOverviewMode(true);
webview.getSettings().setUseWideViewPort(true);
}
});
webview.getSettings().setJavaScriptEnabled(true);
webview.addJavascriptInterface(this, "webConnector");
webview.loadUrl("file:///android_asset/graph_test_json.html");
Sometimes, the WebView is correctly displayed. The graph is fully zoommed out, so we can see it all.
Sometimes, the WebView is "too much zoomed" (a double tap on the WebView makes it zoom out correctly and the graph fits the screen).
How comes the WebView cannot be always correctly zoomed out? Is there a trick to force the WebView zooming out?
I tried to reload the page but it does not work.
I tried to apply those three lines:
webview.setInitialScale(100);
webview.getSettings().setLoadWithOverviewMode(true);
webview.getSettings().setUseWideViewPort(true);
after onResume() of the activity and it doesn't work either.
Can we programmatically zoom out a WebView?

android webview rendering is showing a White Page

Sometimes, when I load my webview with loadUrl, the website is not showing up until I touch the screen or scroll.
It's like I have a webview drawing problem.
Context context = ctx;
final Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.website);
WebView webView = (WebView) dialog.findViewById(R.id.weburl);
webView.setScrollbarFadingEnabled(false);
//Disable the horizontal scroll bar
webView.setHorizontalScrollBarEnabled(false);
//Enable JavaScript
webView.getSettings().setJavaScriptEnabled(true);
//Set the user agent
webView.getSettings().setUserAgentString("AndroidWebView");
//Clear the cache
webView.clearCache(true);
webView.loadUrl("http://" + WebUrl);
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url){
// do your handling codes here, which url is the requested url
// probably you need to open that url rather than redirect:
view.loadUrl(url);
view.setVisibility(View.VISIBLE);
return false; // then it is not handled by default action
}
#Override
public void onPageFinished(WebView view, String url) {
view.refreshDrawableState();
Log.v("FINISH","FINISH");
}
});
Do anybody have an idea why I have this kind of problem.
What version of Android are you testing on? Pre-4.1 versions of Android seem to have this sort problem with WebViews sometimes.
Add this to the manifest for that Activity to fix the problem:
android:hardwareAccelerated="false"
Make WebView invisible in your layout:
<WebView
...
android:visibility="invisible"
.../>
Now, show it back when onPageFinished occurs for the fist time:
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
if (webView.getVisibility() != View.VISIBLE) {
webView.setVisibility(View.VISIBLE);
}
}
});
I'am not sure, I don't have the whole code, but I think is related to the webViewClient implemented in this function:
public boolean shouldOverrideUrlLoading(WebView view, String url){
// do your handling codes here, which url is the requested url
// probably you need to open that url rather than redirect:
view.loadUrl(url);
view.setVisibility(View.VISIBLE);
return false; // then it is not handled by default action
}
here is the officiel definition:
public boolean shouldOverrideUrlLoading (WebView view, String url)
Try to test with your code without implementing shouldOverrideUrlLoading, or make it return true.

Categories

Resources