I'm new to Android Studio, and have run into a problem that I can't fix.
I'm trying to run a HTML file in a webView, but for some reason it keeps reloading to my startpage/index file, when I try to call specific HTML that includes a specific JS file.
Note: these files work fine in every desktop and mobile browser, and works on UIWebView in an Apple App.
My MainActivity looks like this:
MyWeb = (WebView) findViewById(R.id.myWebView);
MyWeb.setWebViewClient(new WebViewClient())
MyWeb.getSettings().setJavaScriptEnabled(true);
MyWeb.getSettings().setSaveFormData(true);
MyWeb.getSettings().setDomStorageEnabled(true);
MyWeb.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
MyWeb.loadUrl("URL");
If I remove the "MyWeb.setWebViewClient(new WebViewClient())" part, the file works, but the app launches a browser with an address bar.
Please help - I've allready spend more days trying to fix this, than I care to admit.
As you have already enabled necessary settings, try doing this once:
MyWeb.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
activity.setProgress(progress * 1000);
}
});
MyWeb.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;
}
});
In order for it to not launch in a browser you must add the line
MyWeb.setWebChromeClient(new WebChromeClient());
Related
I have some issues with Android WebView and Javascript.
Some of customers of app said that WebView on app is not showing anything.
As I checked - its probably not showing javascript at all (whole webpage is loaded in javascript by react).
That my code:
public void setupWebView(WebView accessWebView) {
accessWebView.setWebViewClient(new WebViewClient() {
#SuppressWarnings("deprecation")
#Override
public boolean shouldOverrideUrlLoading(WebView webView, String url) {
handleRedirect(accessWebView);
return true;
}
});
accessWebView.getSettings().setJavaScriptEnabled(true);
accessWebView.getSettings().setDomStorageEnabled(true);
accessWebView.loadUrl(URL);
(I have to use WebViewClient, not WebChromeClient, because of the redirect handling)
Is there anything possible to change so the javascript will load on EVERY device with Android +5.0?
Is it possible that updating WebView on device will help some users?
You need to use setWebChromeClient to enable javascript in your WebView. But don't worry, you can use both setWebChromeClient and setWebViewClient in the same time. Just like in official docs:
// Let's display the progress in the activity title bar, like the
// browser app does.
getWindow().requestFeature(Window.FEATURE_PROGRESS);
webview.getSettings().setJavaScriptEnabled(true);
final Activity activity = this;
webview.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
// Activities and WebViews measure progress with different scales.
// The progress meter will automatically disappear when we reach 100%
activity.setProgress(progress * 1000);
}
});
webview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
});
webview.loadUrl("https://developer.android.com/");
https://developer.android.com/reference/android/webkit/WebView.html
I'm having a few issues regarding the WebViewClient on Android.
The site works perfectly on any mobile browser. Including the ChromeViewClient that I have set for debugging purposes.
And the website that I am loading does not have any issues or errors when using any other mobile browser. Using Chrome's inspector and selecting a device, using as mentioned the native Android browser and also tested on an iOS WebView Component to make sure.
The WebViewClient renders "parts" of the website. Images on one page and not the other, buttons that can not be clicked, a slider that does not work, etc. The website that I am loading is very JavaScript and HTML5 intensive. I am completely out of ideas of how to debug this issue further, are there certain JavaScript libraries that the WebViewClient can't load properly? Is there any other method you would recommend I implement while trying to debug this issue? Or am I missing some really small thing that will make me hit my head against the table?
These are the JS files we are using on the website:
bootstrap.min.js;
jquery.min.js;
swiper.jquery.min.js;
slideout.min.js;
owl.carousel.min.js.
Code for the WebView:
this.webview = (WebView)findViewById(R.id.webView);
webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);
webview.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url){
view.loadUrl(url);
firstLoad = true;
return true;
}
// when the page is finished loading
public void onPageFinished(WebView view, String url){}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl){
Toast.makeText(getBaseContext(), "Could Not load. " + description, Toast.LENGTH_SHORT).show();
alertDialog.setTitle("Error");
alertDialog.setMessage(description);
alertDialog.setButton("OK", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which){
return;
}
});
alertDialog.show();
}
});
webview.loadUrl("mywebsite.com");
I got it working by setDomStorageEnabled(true);
You need to set this when using local storage.
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
Been trying to change the User-Agent string in the HTTP request of an Android app. I have tested this together with wireshark and the emulator, and have seen that although I set the useragent string in the webview, the associated loadUrl request does not use this user-agent string. Instead I see the Dalvik useragent string in the wireshark capture.
Here is the code abstract. Any ideas? Or does the emulator not support this?
#Override
public void run() {
assert(context != null);
...
...
webView = new WebView(context);
...
String defaultUserAgent = "betaUAteststring";
// Clear per-application caches etc
webView.clearCache(true);
webView.clearHistory();
webView.getSettings().setAppCacheEnabled(false);
webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
....
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
..
}
#Override
public void onLoadResource(WebView view, String url) {
...
}
});
// Start loading
webView.getSettings().setUserAgentString(defaultUserAgent);
String setUA = webView.getSettings().getUserAgentString();
//--> logging here shows the correct user agent, so the webview does accept the value
// However the following statement does not result in an http request with the webviews user agent
webView.loadUrl(url);
//Alternative doesn't help either (and shouldn't according to javadoc)
//Map<String,String> headerMap = new HashMap<String,String>();
//headerMap.put("User-Agent","uaTestInAMap");
//webView.loadUrl(url, headerMap);
}
Answering my own question. It appears that the emulator for whatever reason is not taking the user agent string from the webview. I have not found out the reason for this however.
The code works fine on a real device.
You miss to override the default Android behavior on open url (launch default browser).
To use your customize browser to navitage, you only have to attach a WebViewClient to WebView
That is achieve adding the following line to your code:
webView.setWebViewClient(new WebViewClient());
Cheers,
Rodrigo
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.