Website not loading in WebView (javascript issue maybe) - android

:)
So i'm trying to design an app, and i'm facing a problem i can't find the solution of..
Basically, i designed a Browser app, to load one particular website --> http://www.medishala.org/moonstone13/
But instead of loading the whole page, its giving me only the left corner logo and nothing else..
Other websites are loading fine, facing this problem in only this one. Can anyone please help me out??
The issue might be that the javascript is not compatible with android... Can anybody suggest a way around it??
Thanks. :)

i think JavaScript is disabled in a WebView by Default.
Try to enable it:
//set JavaScript enabled
mywebview.getSettings().setJavaScriptEnabled(true);

This code I used and got the complete website. Here is the code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.placement_layout);
showResults();
}
#SuppressLint("SetJavaScriptEnabled")
private void showResults() {
getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
Window.PROGRESS_VISIBILITY_ON);
mWebView = (WebView) findViewById(R.id.MyWebview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setBuiltInZoomControls(false);
String url = "http://www.medishala.org/moonstone13/";
Log.d("Link", url);
mWebView.loadUrl(url);
final Activity MyActivity = this;
mWebView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
MyActivity.setTitle("Loading...");
MyActivity.setProgress(progress * 100);
if (progress == 100)
MyActivity.setTitle(R.string.app_name);
}
});
}
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}

Related

My webview is opening the webpage in the default browser

So I just implemented a simple webview application in which i was loading the stackoverflow main page. Earlier it was working just fine but now as I click on some link it opens that link in the default browser. I have implemented and override the shouldoverrideUrlLoading method by creating my custom webViewClient class.
I know that there are various question ask like these but I am writing this question only because they don't work for me.
public class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(Uri.parse(url).getHost().endsWith(".com"))
return false;
Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new MyWebViewClient());
final customEditText editText = findViewById(R.id.urlEditText);
Button button = findViewById(R.id.enterButtonId);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
webView.loadUrl("https://"+editText.getText().toString().trim().toLowerCase());
}
});
}
You can use this:
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("url");
Just implement the web client and set it before loadUrl. The simplest way is:
WebView.setWebViewClient(new WebViewClient());
So When I was reading a tutorial https://www.journaldev.com/9333/android-webview-example-tutorial in this it is given that when shouldOverrideUrlLoading( ) method provides false then it url opens in our webview and if it returns true, then it will not load the page at all. So I think that earlier my code was working was because I was opening .com extensioned websites but when i open other extension website then it redirect to the default browser.

HTTP-EQUIV="REFRESH" isn't working on android web view

I have a web page with a basic form (like a newsletter subscription).
The user sends its information and gets a success page with a link to the form submission page. If he doesn't click on the link the page will be automatically redirected after 5 seconds.
<meta http-equiv="refresh" content="5;url=www.site.com" />
So far so good! It works as expected in both desktop and mobile browsers.
Then I created an android app (min API SDK 21) with a web view to load the site.
The problem is that the auto refresh isn't working inside the web view...
Am I missing something? I'm trying to avoid the javascript hack...
Thanks for your time!
EDIT
I notice that when I click on the new form submission page link it opens the default browser instead of rendering it on the web view!
After searching for the problem, I came to a solution by resetting the URL on URL loading.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
mWebView = findViewById(R.id.webview);
mWebView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
mWebView.loadUrl(url);
return true;
}
});
mWebView.loadUrl(url);
}
It's working, but I think it can be improved.
My initial problem was that I wasn't setting a WebViewClient.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
mWebView = findViewById(R.id.webview);
mWebView.loadUrl(url);
}
Thus, I think I just need to add a WebViewClient and it'll work
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
mWebView = findViewById(R.id.webview);
mWebView.setWebViewClient(new WebViewClient());
mWebView.loadUrl(url);
}
Note, and if we want javascript to work we need to enable it
mWebView.getSettings().setJavaScriptEnabled(true);

Hide the browser address bar in Android

I've created a WebView app and everything works fine.
I'm new to Android.The only remaining feature I need for the app is to hide the address bar. Because I want the app to look more like a regular app and not a webpage within a web browser window.
My code is like this,
package com.Mobi.ebookread;
public class Mobile extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final WebView webview = (WebView) findViewById(R.id.helloWebView);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("http://www.google.com");
}
}
How do I do it?
i think there is a problem. webview does not have an address bar, seems that the browser app is getting opened. It could be possible that there is a redirection happening and that causes the browser app to open up, and you did not intercept that redirect using a WebViewClient and shouldOverrideURLLoading()
Below is how the webview looks like.
Finally I Try with this. Its worked for me..
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
//webview use to call own site
webview =(WebView)findViewById(R.id.webView1);
webview.setWebViewClient(new WebViewClient()); //use to hide the address bar
webview .getSettings().setJavaScriptEnabled(true);
webview .getSettings().setDomStorageEnabled(true); //to store history
webview.loadUrl("http://www.google.com");
}
and your entire main.xml(res/layout) look should like this:
`<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>`
don't go to add layouts.
try the following code:
webView=(WebView) findViewById(R.id.helloWebView);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
webview.loadUrl("http://www.google.com");

Slower page loading while invoking WebViewClient for the firsttime

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.

Problem in using WebView?

I have created a page which has link to a page of a website. So for showing that on I have used a WebView and it works fine.
My Problem is that when I click on any link given on that webpage, the link opens in phone's default browser view. But I want all the links to be opened in my created WebView.
Have I made any mistake or it is right..
Please Help Me
My code is as follows...
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Log.e("------------", ".........................................................................................");
setContentView(R.layout.terms_of_use_web_view_page);
btn_back = (Button) findViewById(R.id.terms_of_use_button_back);
btn_back.setOnClickListener(this);
webview = (WebView)findViewById(R.id.terms_of_use_webview);
webview.getSettings().setJavaScriptEnabled(false);
webview.loadUrl("http://www.oomphlink.com/terms-of-use/");
}
Try specifying your own WebViewClient:
WebView webView = (WebView)findViewById( R.id.terms_of_use_webview );
webView.setWebViewClient( new WebViewClient()
{
#Override
public boolean shouldOverrideUrlLoading( WebView view, String url )
{
view.loadUrl( url );
return true;
}
});
To further understand why this is necessary have a look at the documentation for the shouldOverrideUrlLoading method.
wv.setWebViewClient(new MyWebViewClient());
public class MyWebViewClient extends WebViewClient{
}
link for more info...

Categories

Resources