Android web view blank page - android

I searched for two days for a solution - including all stackover posts - and couldn't solve it ..
My web view can open sites like Google and Facebook but can't open the site in the code !
here is my full code
webView = findViewById(R.id.wv_pray_time);
webView.setWebChromeClient(new WebChromeClient());
webView.loadUrl("http://ramadanco.noursal.com/");
webView.setBackgroundColor(Color.TRANSPARENT);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setDomStorageEnabled(true);
webSettings.setDisplayZoomControls(false);
// No net error
webView.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
webView.loadUrl("file:///android_asset/error.html");
}
});
// Pull to refresh
mySwipeRefreshLayout = this.findViewById(R.id.swipeContainer);
mySwipeRefreshLayout.setOnRefreshListener(
new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
webView.reload();
if (mySwipeRefreshLayout.isRefreshing()) {
mySwipeRefreshLayout.setRefreshing(false);
}
}
}
);
}
#Override
public void onBackPressed() {
if (webView.canGoBack()) {
webView.goBack();
} else {
super.onBackPressed();
}
}

Related

Android web view can't open this site

I searched for Six days for a solution - including all stackoverflow posts and couldn't solve it .
My web view can open sites like Google and Facebook but can't open the site in the code !. I got a blank page ...
here is my full code
webView = findViewById(R.id.wv_pray_time);
webView.setWebChromeClient(new WebChromeClient());
webView.loadUrl("http://ramadanco.noursal.com/");
webView.setBackgroundColor(Color.TRANSPARENT);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setDomStorageEnabled(true);
webSettings.setDisplayZoomControls(false);
// No net error
webView.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
webView.loadUrl("file:///android_asset/error.html");
}
});
// Pull to refresh
mySwipeRefreshLayout = this.findViewById(R.id.swipeContainer);
mySwipeRefreshLayout.setOnRefreshListener(
new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
webView.reload();
if (mySwipeRefreshLayout.isRefreshing()) {
mySwipeRefreshLayout.setRefreshing(false);
}
}
}
);
}
#Override
public void onBackPressed() {
if (webView.canGoBack()) {
webView.goBack();
} else {
super.onBackPressed();
}
}
can anyone open this site inside a webview ??
I would recommend the following things -
Very first use the https:// instead of http://.
You might be getting the SSL error while loading the content, so you can use it in this way.
#Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
// ignore ssl error
if (handler != null){
handler.proceed();
} else {
super.onReceivedSslError(view, null, error);
}
}
Check you have added the internet permission
<uses-permission android:name="android.permission.INTERNET"/>
If you don't want to use https (which I wouldn't recommend) you also have to add android:usesCleartextTraffic="true" inside the application tag in the manifest.

Webview: how to open link in html loaded via "loadDataWithBaseURL"

I'm opening a WebView with static html in my Android app. The html contains a link as well which currently gets opened in the system browser, i.e. the app is put in the background. How can I achieve opening that link in the WebView itself? I tried it with
webView.setWebViewClient(new WebViewclient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
// gets never called
}
}
but that method is never called.
Try this it works for you.
private void loadWebView() {
webView = (WebView) view.findViewById(R.id.webView);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setDomStorageEnabled(true);
final ProgressDialog pd = ProgressDialog.show(getActivity(), "", "Loading...", true);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://yourWeb.com/");
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
pd.show();
}
#Override
public void onPageFinished(WebView view, String url) {
try {
pd.dismiss();
} catch (Exception e) {
}
}
});
}

Android Webview vs Android Chrome Browser

I am trying to load a web page in a web view, I am able to do that, however there is an a button that allows you to upload a file. This button works fine when I load the page in the Chrome app on android and it opens the file browser, how ever on web view nothing happens. is it possible to use a chrome for web view, or some how get this button to trigger the file chooser in from the web view?
I have tried changing the web view options to enable javascript ect, below is the code for the webactivity:
public class WebActivity extends Activity {
Button b1;
WebView wv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);
b1=(Button)findViewById(R.id.button);
wv=(WebView)findViewById(R.id.webView);
//wv1.setWebViewClient(new MyBrowser());
wv.setWebChromeClient(new WebChromeClient());
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
WebSettings webSettings = wv.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setAppCacheEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setAllowFileAccess(true);
wv.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
wv.loadUrl("MY WEBSITE URL");
wv.setWebChromeClient(new WebChromeClient());
wv.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
if (url.startsWith("tel:") || url.startsWith("mailto:")) {
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse(url));
startActivity(intent);
}else if(url.startsWith("http:") || url.startsWith("https:")) {
view.loadUrl(url);
}
return true;
}
});
}
});
}
}
Try the below code.
WebView wv;
WebSettings webSettings = wv.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setAppCacheEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setAllowFileAccess(true);
wv.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
wv.loadUrl("YOUR URL HERE");
wv.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
if (url.startsWith("tel:") || url.startsWith("mailto:")) {
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse(url));
startActivity(intent);
}else if(url.startsWith("http:") || url.startsWith("https:")) {
view.loadUrl(url);
}
return true;
}
});
wv.setWebChromeClient(new WebChromeClient());
Hope this will help you.
There are some options that are not enabled by default, most likely you don't have javascript.
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);

Ignore ssl certificate requests in webview

I make small app which involve aspx pages , I try open it throw my webview I got blank screen .
I try open it with chrome browser it give me certificate needed
my code is very simple I add some documentation to explain what is going on
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);
Bundle bundle = getIntent().getExtras();
if(bundle != null){
urlSite = bundle.getString("passed_url");
}
_toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(_toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
_toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
webView = (WebView) findViewById(R.id.webView);
WebSettings webSettings = webView.getSettings();
// enable javascript
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setUseWideViewPort(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setPluginState(WebSettings.PluginState.ON_DEMAND);
webView.setWebChromeClient(new WebChromeClient());
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
new ParseURl().execute(new String[]{urlSite});
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
if(urlSite != null){
webView.clearCache(true);
webView.loadUrl(urlSite);
// webView.loadUrl("file:///android_asset/af.html");
}
}
After some research I found the answer, just override onReceivedSslError method and skip it.
#Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.proceed(); // Ignore SSL certificate errors
}

PDF links doesn't appears in android WebView API below 19

I am trying to load a URL in WebView having .pdf link.
Link is not appearing on JELLY_BEAN devices [Tested on 4.1, 4.1.1 & 4.2]
However, the link is visible on all KITKAT and above devices.
Here's the code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.directory_browser);
webView = (ExtendedWebView) findViewById(R.id.webBrowser);
webSettings = webView.getSettings();
progressBar = (ProgressBar) findViewById(R.id.browserProgressBar);
progressTextView = (TextView) findViewById(R.id.browserProgressText);
preparewebView();
webView.loadUrl(getIntent().getStringExtra("medicalUrl"));
}
private void preparewebView() {
webSettings.setJavaScriptEnabled(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setAllowFileAccess(true);
webSettings.setDomStorageEnabled(true);
webView.requestFocusFromTouch();
webView.setInitialScale(getResources().getInteger(R.integer.medical_webview_initialScale));
webSettings.setBuiltInZoomControls(false);
webView.setPadding(0, 0, 0, 0);
webView.getSettings().setTextZoom(100);
webView.getSettings().setRenderPriority(RenderPriority.HIGH);
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return super.shouldOverrideUrlLoading(view, url);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
//Decide whether to set a download listener
String prefix = com.theteamie.android.uwc.Webservice.URL.getUwcseaWebappDownloadUrl();
if (!url.startsWith(prefix)){
Log.v("WebViewClient", "Appending downloader");
view.setDownloadListener(new Downloader());
}
super.onPageStarted(view, url, favicon);
}
});
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%
try {
String progressText = getResources().getString(R.string.loading) + "... ";
if (progress != 100) {
progressBar.setVisibility(ProgressBar.VISIBLE);
progressBar.setProgress(progress);
progressTextView.setVisibility(TextView.VISIBLE);
progressTextView.setText(progressText + Integer.toString(progress));
} else {
progressBar.setVisibility(ProgressBar.GONE);
progressTextView.setVisibility(TextView.GONE);
}
} catch (IllegalStateException e) {
Log.e(TAG, Log.getStackTraceString(e));
}
}
#Override
public void onReceivedTitle(WebView view, String title) {
super.onReceivedTitle(view, title);
}
});
}
Any suggestions what could be the reason why the link is not visible in WebView for those devices.
Thanks
try this way may this help you
webview.getSettings().setJavaScriptEnabled(true);
String pdf = "YourLink.pdf";
webview.loadUrl("http://docs.google.com/gview?embedded=true&url=" + pdf);
try like this :-
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
webview.loadUrl(your_url);

Categories

Resources