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

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);

Related

Unable to load my URL in Android WebView but URL loading in Android Browser

I m trying to load one of my cloud URL from android webview but it returns HTTP Status 401 always. If i tried from Chrome mobile browser its working as expected. Even in iOS WebView its working fine.
Here is my code, Please someone help me.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_xoi);
base_url = "https://vision.xoeye.io/partners/abcd/share?";
String params = "shareId=12345678910111213141516" +
"&serviceTicket=123-123&" +
"customer=Manikanta&" +
"location=3201 S. State St. Chicago IL 60616";
webView = findViewById(R.id.webView);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
webView.getSettings().setDomStorageEnabled(true);
webView.clearHistory();
webView.clearCache(false);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.clearFormData();
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
return false;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
});
webView.loadUrl(base_url);
}
Try This
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_xoi);
base_url = base_url+params;
webView = findViewById(R.id.webView);
WebSettings webSettings=webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setPluginState(WebSettings.PluginState.ON);
webView.setWebViewClient(new HelloWebViewClient());
//add here additional setting which you want
webView.loadUrl(base_url);
}
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
}
Hope this will help you.

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 custom error page

I am creating application that use WebView to access a online website. I am stuck where I have to add code to check availability of page.
public class SpartanWeb extends Activity {
WebView mWebView;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Adds Progrss bar Support
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.main);
// Makes Progress bar Visible
getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
Window.PROGRESS_VISIBILITY_ON);
// Get Web view
mWebView = (WebView) findViewById(R.id.webView1);
WebSettings websettings = mWebView.getSettings();
websettings.setJavaScriptEnabled(true);
mWebView.stopLoading();
mWebView.clearCache(true);
mWebView.loadUrl("http://google.com");
mWebView.setHorizontalScrollBarEnabled(false);
mWebView.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
mWebView.setWebViewClient(new WebViewClient());
mWebView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
}
});
// onProgressChanged
final Activity MyActivity = this;
mWebView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
// bar disappear after URL is loaded, and changes string to
// Loading...
MyActivity.setTitle("Loading...");
MyActivity.setProgress(progress * 100); // Make the bar
// disappear after URL
// is loaded
// Return the app name after finish loading
if (progress == 100)
MyActivity.setTitle(R.string.app_name);
}
});
}// EOM oc
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
I am trying to add onReceivedError but for some reason custom page is not loading.
/** Called when the activity is first created. */
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
mWebView.loadUrl("file:///android_asset/error.html");
}
Please advise what to do.
You can call loadErrorPage(view) function in the onReceivedError function.
The following code will load the error content you need to show.Here i am load the html file with loadDataWithBaseURL.
public void loadErrorPage(WebView webview){
if(webview!=null){
String htmlData ="<html><body><div align=\"center\" >"This is the description for the load fail : "+description+"\nThe failed url is : "+failingUrl+"\n"</div></body>";
webview.loadUrl("about:blank");
webview.loadDataWithBaseURL(null,htmlData, "text/html", "UTF-8",null);
webview.invalidate();
}
}
I added onReceivedError to mWebView.setWebViewClient(new WebViewClient so now it's working. Thanks for tips.
mWebView.setWebViewClient(new WebViewClient() {
#Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
mWebView.loadUrl("file:///android_asset/error.html");
} });
You can use the following code ..
public class TestResultWebclient extends WebViewClient {
ProgressDialog progressDialog;
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
if (progressDialog == null) {
progressDialog = new ProgressDialog(TermsAndCondsMrupeeActivity.this);
progressDialog.setMessage("Loading...");
progressDialog.show();
}
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
if (progressDialog != null)
try {
if (progressDialog.isShowing()) {
progressDialog.dismiss();
progressDialog = null;
}
} catch (Exception exception) {
exception.printStackTrace();
}
super.onPageFinished(view, url);
}
}

unable to load url in android webview

I want to load one url "http://connect.mayoclinic.org/discussions" in webview .i tried lots of ways but i am unable to load it.its showing blank page. I am using the following for this.when i use simple load url by removing other functions,then also its giving the blank page.but when i tested on webbrowser its showing the webpage. its showing on iphone also..only unable to show in android webview
this.wb = (WebView) findViewById(R.id.wb);
WebSettings settings = wb.getSettings();
settings.setJavaScriptEnabled(true);
final AlertDialog alertDialog = new AlertDialog.Builder(BrowserViewHealthAlertActivity.this).create();
progressBar = ProgressDialog.show(BrowserViewHealthAlertActivity.this, "", "Loading...");
progressBar.setCancelable(true);
progressBar.setOnKeyListener(new DialogInterface.OnKeyListener() {
#Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
CommonFunction.startTimer(context);
if (keyCode == KeyEvent.KEYCODE_SEARCH && event.getRepeatCount() == 0) {
return true; // Pretend we processed it
}
if (keyCode == KeyEvent.KEYCODE_MENU && (event.getFlags() & KeyEvent.FLAG_LONG_PRESS) == KeyEvent.FLAG_LONG_PRESS) {
return true;
}
return false; // Any other keys are still
}
});
wb.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
CommonFunction.startTimer(context);
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, String url) {
if (progressBar.isShowing()) {
progressBar.dismiss();
}
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
alertDialog.setTitle("Error");
alertDialog.setMessage(description);
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return;
}
});
alertDialog.show();
}
});
wb.loadUrl(link);
I have tried your URL. Its not woring in browser.
might be problem with the URL itself #asiya.
Is it giving black page still??
Check my code with another URL like www.google.com. Its my working code.
A simple Webview goes here displaying loading until URL take to load.
Webview webview = (WebView) findViewById(R.id.webview);
webview.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
// Log.e("in", "onPageStarted" + isInternetPresent);
if (isInternetPresent) {
// Log.e("in", "if" + isInternetPresent);
progressBar.setVisibility(View.VISIBLE);
noconnection.setVisibility(View.INVISIBLE);
retry.setVisibility(View.INVISIBLE);
} else
noconnection.setVisibility(View.VISIBLE);
}
#Override
public void onLoadResource(WebView view, String url) {
super.onLoadResource(view, url);
// Log.e("in", "onloadresoure");
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar.setVisibility(View.INVISIBLE);
}
});
WebSettings webSettings = webview.getSettings();
webSettings.setBuiltInZoomControls(true);
webSettings.setSupportZoom(true);
webview.getSettings().setDisplayZoomControls(false);
webSettings.setJavaScriptEnabled(true);
// webview.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
webSettings.setLoadWithOverviewMode(true);
webSettings.setUseWideViewPort(true);
webview.setInitialScale(1);
if (isInternetPresent) {
isInternetPresent = true;
webview.loadUrl("http://your url");
} else {
ExitAlertClass.QuizSelectOptionAlert(ctx, "No Internet Connection"); //here goes your alert code
noconnection.setVisibility(View.VISIBLE);
retry.setVisibility(View.VISIBLE);
};
progressBar is a simple progress:
ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar1);
Here, I am getting status of internet usng broadcast receiver. You can make that code.

Android webview :: Refused to display document because display forbidden by X-Frame-Options

I am trying to load twitter widget file in webview .
My webview loads correctly but
When i try to open link inside webview (link inside twitter widget file) , i get the above mentioned error & whole screen turns out blank. Below is the code i am using.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_all);
String url = getIntent().getExtras().getString("url");
webProgress = (ProgressBar) mCustomView.findViewById(R.id.webProgress);
mWebView = (WebView) findViewById(R.id.web_view);
noDataConnection = (LinearLayout) findViewById(R.id.layout_no_network);
detector = new ConnectionDetector(this);
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// Log.e(TAG, "shouldOverrideUrlLoading : " + url);
if (!detector.isConnectingToInternet()) {
noDataConnection.setVisibility(View.VISIBLE);
webProgress.setVisibility(View.GONE);
return true;
} else {
noDataConnection.setVisibility(View.GONE);
webProgress.setVisibility(View.VISIBLE);
return false;
}
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
noDataConnection.setVisibility(View.VISIBLE);
webProgress.setVisibility(View.GONE);
}
});
mWebView.setWebChromeClient(new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
if (newProgress >= 100) {
Log.e(TAG, "onProgressChanged : 100%");
webProgress.setVisibility(View.GONE);
}
}
});
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
if (!detector.isConnectingToInternet()) {
noDataConnection.setVisibility(View.VISIBLE);
webProgress.setVisibility(View.GONE);
} else {
noDataConnection.setVisibility(View.GONE);
webProgress.setVisibility(View.VISIBLE);
mWebView.loadUrl(url);
}
}
Any help is appreciated.
I finally found the solution with this thread response :
https://stackoverflow.com/a/11280814/555153
It is because of iFrames that tweets clicks are not well detected.

Categories

Resources