android webview rendering is showing a White Page - android

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.

Related

Android, How to retrieve redirect URL from a WebView?

First i surfed the web but no answer was found matching my case.
I want to grab a specific redirect url from a webview and then keep the webview from opening it. The case is an Instagram redirect url after the user authorized my app. How to do it? Here is my code:
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("https://api.instagram.com/oauth/authorize/?client_id=my_client_id&redirect_uri=my_redirect_uri&response_type=code");
You will have to set your custom WebviewClient overriding shouldOverrideUrlLoading method for your webview before loading the url.
mWebView.setWebViewClient(new WebViewClient()
{
#SuppressWarnings("deprecation")
#Override
public boolean shouldOverrideUrlLoading(WebView webView, String url)
{
return shouldOverrideUrlLoading(url);
}
#TargetApi(Build.VERSION_CODES.N)
#Override
public boolean shouldOverrideUrlLoading(WebView webView, WebResourceRequest request)
{
Uri uri = request.getUrl();
return shouldOverrideUrlLoading(uri.toString());
}
private boolean shouldOverrideUrlLoading(final String url)
{
Log.i(TAG, "shouldOverrideUrlLoading() URL : " + url);
// Here put your code
return true; // Returning True means that application wants to leave the current WebView and handle the url itself, otherwise return false.
}
});
mWebView.loadUrl("Your URL");
Checkout the example code for handling redirect urls and open PDF without download, in webview.
https://gist.github.com/ashishdas09/014a408f9f37504eb2608d98abf49500
You should override shouldOverrideUrlLoading:
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView wView, String url) {
if (url.indexOf('api.instagram.com') > -1) //check if that's a url you want to load internally
{
webView.loadUrl(url);
return true;
}
else
{
return false; //Let the system handle it
}
}
});
Finally thanks to the answer given to my question by Udi I, with a little change, i managed to find a solution to the problem. Here is the code which worked for me:
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView wView, String url) {
return (url.indexOf("some part of my redirect uri") > -1);
}
});
webView.loadUrl(myUrl); //myUrl is the initial url.
Using the above code, if there will be any url containing redirect uri, webView won't load it. Else, webView will load it. Also thanks to Kamil Kaminski.
Simply set WebViewClient for your WebView, and override shouldOverrideUrlLoading in WebViewClient. In that method you can grab your url, and decide whether or not your WebView should follow redirection url. Here's example:
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// here you can assign parameter "url" to some field in class
// return true if you want to block redirection, false otherwise
return true;
}
});
You can compare URL strings / particular word, and take actions accordingly -
public boolean shouldOverrideUrlLoading(WebView view, String url) {
super.shouldOverrideUrlLoading(view, url);
if (url.contains(".pdf")
openPDF(url); // handle pdf opening
if(url.startsWith("tel:")
callNumber(url); // handle call processing
...
}
Just to add more, you can prevent webview from showing traditional error message screens, or bypass known errors -
#Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
if (errorCode == -10) {
System.out.println("Error occured: Error code: "
+ errorCode + "..Ignoring this error");
return;
}
else
{
// Show your custom screen to notify error has occured
}
...
}

HTML Preloading of native Webview in Android

I just want to show queue of Webview on screen. To avoid black screen during loading of html in webview, I want to preload webview using z-index or using send to back upcoming webview.....
this is not the best solution but what with showing Progres dialog for the moment he load the new url ? i
WebView mWeb;
ProgressDialog mProgress;
mWeb = your web view id
// set Javascript
WebSettings settings = mWeb.getSettings();
settings.setJavaScriptEnabled(true);
// the init state of progress dialog
mProgress = ProgressDialog.show(this, "Loading", "Please wait for a moment...");
// add a WebViewClient for WebView, which actually handles loading data from web
mWeb.setWebViewClient(new WebViewClient() {
// load url
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
// when finish loading page
public void onPageFinished(WebView view, String url) {
if(mProgress.isShowing()) {
mProgress.dismiss();
}
}
});
// set url for webview to load
mWeb.loadUrl("http://www.saharplus.com");
}
e.g: you could use like that.
Add a imageview with full-parent measurement
Set webview visibility GONE
Add sample code your activity
// OTHER CODE STUFF
mWeb.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
Webview wView=(Webview) view;
Imageview.setVisibility(View.GONE);
wView.setvisibility(View.VISIBLE);
}
});
*typing on phone apoligize for any typos :/

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)

Android WebView, how to handle redirects in app instead of opening a browser

So right now in my app the URL I'm accessing has a redirect, and when this happens the WebView will open a new browser, instead of staying in my app. Is there a way I can change the settings so the View will redirect to the URL like normal, but stay in my app instead of opening a new browser?
Edit:
I want the redirecting URL, I just don't know how to create it, so the only way to get to that URL is through one that will cause a redirect to the one I want.
For example: When you go here: http://www.amazon.com/gp/aw/s/ref=is_box_/k=9780735622777 notice how it will redirect the URL to the actual product. In my app, if I open it in a new browser, it will do that just fine, however if I keep it in my app with a WebView, it will show up as though it's doing a search for k=9780735622777, like this: http://www.amazon.com/gp/aw/s/ref=is_s_?k=k%3D9780735622777&x=0&y=0 . OR, it will open the view in the browser and show what is appropriate. However, I want to keep everything in my app.
Create a WebViewClient, and override the shouldOverrideUrlLoading method.
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);
return false; // then it is not handled by default action
}
});
According to the official documentation, a click on any link in WebView launches an application that handles URLs, which by default is a browser. You need to override the default behavior like this
myWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
});
Just adding a default custom WebViewClient will do. This makes the WebView handle any loaded urls itself.
mWebView.setWebViewClient(new WebViewClient());
You will have to set your custom WebviewClient overriding shouldOverrideUrlLoading method for your webview before loading the url.
mWebView.setWebViewClient(new WebViewClient()
{
#SuppressWarnings("deprecation")
#Override
public boolean shouldOverrideUrlLoading(WebView webView, String url)
{
return shouldOverrideUrlLoading(url);
}
#TargetApi(Build.VERSION_CODES.N)
#Override
public boolean shouldOverrideUrlLoading(WebView webView, WebResourceRequest request)
{
Uri uri = request.getUrl();
return shouldOverrideUrlLoading(uri.toString());
}
private boolean shouldOverrideUrlLoading(final String url)
{
Log.i(TAG, "shouldOverrideUrlLoading() URL : " + url);
// Here put your code
return true; // Returning True means that application wants to leave the current WebView and handle the url itself, otherwise return false.
}
});
Checkout the example code for handling redirect urls and open PDF without download, in webview.
https://gist.github.com/ashishdas09/014a408f9f37504eb2608d98abf49500
Create a class that implements webviewclient and add the following code that allows ovveriding the url string as shown below.
You can see these [example][1]
public class myWebClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
On your constructor, create a webview object as shown below.
web = new WebView(this); web.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
Then add the following code to perform loading of urls inside your app
WebSettings settings=web.getSettings();
settings.setJavaScriptEnabled(true);
web.loadUrl("http://www.facebook.com");
web.setWebViewClient(new myWebClient());
web.setWebChromeClient(new WebChromeClient() {
//
//
}
Please use the below kotlin code
webview.setWebViewClient(object : WebViewClient() {
override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
view.loadUrl(url)
return false
}
})
For more info click here
In Kotlin, to navigate within same webView we needed to override the shouldOverrideUrlLoading for webview
If return type is true then navigation will be blocked If return
type is false then navigation will happen
object : WebViewClient() {
override fun shouldOverrideUrlLoading(view: WebView, request: WebResourceRequest): Boolean {
return true
}
}.also { webView.webViewClient = it }
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
if (url.equals("your url")) {
Intent intent = new Intent(view.getContext(), TransferAllDoneActivity.class);
startActivity(intent);
}
}

Clicking URLs opens default browser

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)

Categories

Resources