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)
Related
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
I'm trying to do a webview based application for this website to show it in a mobile application.
when I put any different site the application work great, but in this specific site never show the second page when I clicked in the button to go to the desk page. However, I put a Log statement in the onPageFinished method and log that the page is loaded completely.
My Code Here
final WebView myWebView = (WebView) rootView.findViewById(R.id.webview);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.setWebViewClient(new WebViewClient() {
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(getActivity(), "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
#Override
public void onPageFinished(WebView view, String url) {
Log.d("WEBSITE", "Page Loaded.");
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.d("WEBSITE", url);
myWebView.loadUrl(url);
return false;
}
});
myWebView.loadUrl("https://demo.frappecloud.com/");
I believe the problem is with your shouldOverrideUrlLoading. If you check the documentation you will see that :
This method is not called for requests using the POST "method"
When you are submitting the Form, you are making a POST request, which is basically ignored.
Here is the link: WebViewClient
Also check this reference where it says how you could load a URL with POST data: LOAD A POST REQUEST INTO A WEBVIEW IN ANDROID
Consider checking this thread: Android - how to intercept a form POST
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.
Below is my code for WebViewClient. I have invoked the class by clicking a button.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.webview);
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
activity.setTitle("Loading...");
activity.setProgress(progress * 100);
if (progress == 100)
activity.setTitle(R.string.app_name);
}
});
webView.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;
}
});
webView.loadUrl("http://mylink.com");
// String customHtml =
// "<html><body><h1>Hello, WebView</h1></body></html>";
// webView.loadData(customHtml, "text/html", "UTF-8");
}
}
Its taking more time to load the WebViewClient. I need to speed it up. Any suggestions
Nothing wrong with your code as far as I can see. I would strongly suggest you check out the performance of the web server you are connecting to as this will most likely be the cause of your problems. Particularly look at the servers response times. To test the performance of your web view try setting the url to a fast responding web site, something like google.com.
I think WebView creates a database for all sorts of reasons (like web cache) in your app's temp dir.
This DB is created the first time you call WebView from the app. In subsequent calls it is re-used.
Therefore it is possible that this creation process is what you're experiencing.
I have loaded an external URL in my WebView. Now what I need is that when the user clicks on the links on the page loaded, it has to work like a normal browser and open the link in the same WebView. But it's opening the default browser and loading the page there?
I have enabled JavaScript. But still it's not working. Have I forgotten something?
If you're using a WebView you'll have to intercept the clicks yourself if you don't want the default Android behaviour.
You can monitor events in a WebView using a WebViewClient. The method you want is shouldOverrideUrlLoading(). This allows you to perform your own action when a particular URL is selected.
You set the WebViewClient of your WebView using the setWebViewClient() method.
If you look at the WebView sample in the SDK there's an example which does just what you want. It's as simple as:
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
in some cases you might need an override of onLoadResource if you get a redirect which doesn't trigger the url loading method. in this case i tried the following:
#Override
public void onLoadResource(WebView view, String url)
{
if (url.equals("http://redirectexample.com"))
{
//do your own thing here
}
else
{
super.onLoadResource(view, url);
}
}
Official documentation says, click on a link in a WebView will launch application that handles URLs. You need to override this default behavior
myWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
});
or if there is no conditional logic in the method simply do this
myWebView.setWebViewClient(new WebViewClient());
Add this 2 lines in your code -
mWebView.setWebChromeClient(new WebChromeClient());
mWebView.setWebViewClient(new WebViewClient());
The method boolean shouldOverrideUrlLoading(WebView view, String url) was deprecated in API 24. If you are supporting new devices you should use boolean shouldOverrideUrlLoading (WebView view, WebResourceRequest request).
You can use both by doing something like this:
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
newsItem.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl(request.getUrl().toString());
return true;
}
});
} else {
newsItem.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
}
Arulx Z's answer was exactly what I was looking for.
I'm writing an app with Navigation Drawer with recyclerview and webviews, for keeping the web browsing inside the app regardless of hyperlinks clicked (thus not launching the external web browser). For that it will suffice to put the following 2 lines of code:
mWebView.setWebChromeClient(new WebChromeClient());
mWebView.setWebViewClient(new WebViewClient());
exactly under your WebView statement.
Here's a example of my implemented WebView code:
public class WebView1 extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView wv = (WebView) findViewById(R.id.wv1); //webview statement
wv.setWebViewClient(new WebViewClient()); //the lines of code added
wv.setWebChromeClient(new WebChromeClient()); //same as above
wv.loadUrl("http://www.google.com");
}}
this way, every link clicked in the website will load inside your WebView.
(Using Android Studio 1.2.2 with all SDK's updated)