PDF opening inside a Current WebView Page - android

My application current view is In web view..inside which if user clicks on a pdf link.it should get installed on sd card and then available options for viewing must be shown ..if no Viewer available then a message should be shown
protected void onStart()
{
super.onStart();
myWebView.loadUrl("URl");
myWebView.setWebViewClient(
new WebViewClient()
{
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.endsWith("pdf"))
{
// do eet return true; } return false;
} });
myWebView.getSettings().setJavaScriptEnabled(true);
/*WebSettings webSettings = myWebView.getSettings(); \
webSettings.setBuiltInZoomControls(true); webSettings.setSupportZoom(true);*/
}

Overide the function shouldOverrideUrlLoading()
youWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.endsWith("pdf")) {
// do eet
return true;
}
return false;
}
});

Related

Detect when a WebView loads all its images

I am using a WebView that loads a page with some images in it..
I need to detect exactly when it finishes loading and rendering all images in the page.
I tried to use "onPageFinished" but it gets called before all images are completely downloaded and visible.
Same thing goes for "onProgressChanged"..
Is there a method where I can know when exactly does my WebView completely load all its views and images and they are visible on the screen??
In my working code I am using next variant:
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setLoadWithOverviewMode(true);
// try to use this pieсe of code
webView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
// put here your code
System.out.println("All images are downloaded and displayed");
}
});
webView.addJavascriptInterface(this, "android");
webView.loadDataWithBaseURL(null, content, "text/html", "UTF-8", null);
if you have several redirects it may fail. This approach solves most problems though
boolean loadingFinished = true;
boolean redirect = false;
mWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(
WebView view, WebResourceRequest request) {
if (!loadingFinished) {
redirect = true;
}
loadingFinished = false;
webView.loadUrl(request.getUrl().toString());
return true;
}
#Override
public void onPageStarted(
WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
loadingFinished = false;
//SHOW LOADING IF IT ISNT ALREADY VISIBLE
}
#Override
public void onPageFinished(WebView view, String url) {
if (!redirect) {
loadingFinished = true;
}
if (loadingFinished && !redirect) {
//HIDE LOADING IT HAS FINISHED
} else {
redirect = false;
}
}
});

Ajax not working in android webview

I am loading a website in webview,we have used Ajax in website and its working fine on web browser and mobile browser also but in android webview ajax is not working no error is there in the console.Here is my code:-
public class Activity_WebView extends AppCompatActivity implements
ConnectivityReceiver.ConnectivityReceiverListener {
WebView webview;
ProgressDialog pro_dialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setPluginState(WebSettings.PluginState.ON);
webview.setWebViewClient(new loadinsame());
pro_dialog = new ProgressDialog(Activity_WebView.this);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setDomStorageEnabled(true);
webview.getSettings().setAllowUniversalAccessFromFileURLs(true);
boolean connection = checkConnection();
if (connection) {
webview.loadUrl("website url");
} else {
Toast.makeText(Activity_WebView.this, "Sorry! Not connected to
internet", Toast.LENGTH_SHORT).show();
dialog_Show(webview, "Please check you Inernet connect and Reload.",
false);
}
}
#Override
public void onNetworkConnectionChanged(boolean isConnected) {
if (!isConnected) {
Toast.makeText(Activity_WebView.this, "Sorry! Not connected to
internet", Toast.LENGTH_SHORT).show();
}
}
private class loadinsame extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
pro_dialog.setCancelable(false);
pro_dialog.setMessage("Loading...");
pro_dialog.show();
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
pro_dialog.dismiss();
}
#Override
public void onReceivedError(final WebView webview, WebResourceRequest
request, WebResourceError error) {
super.onReceivedError(webview, request, error);
pro_dialog.dismiss();
// dialog_Show(webview, "Error Occur, Do you want to Reload?", true);
}
}
#Override
public void onBackPressed() {
if (webview.canGoBack()) {
webview.goBack();
} else {
super.onBackPressed();
}
}
private boolean checkConnection() {
boolean isConnected = ConnectivityReceiver.isConnected();
return isConnected;
}
#Override
protected void onResume() {
super.onResume();
MyApplication.getInstance().setConnectivityListener(this);
}
}
when i inspect the website in chrome using emulator, find that my ajax remain pending and then cancel after sometime.
Thanks in advance.
Just promoting #Jeff Thomas comment, by setting
mWebView.getSettings().setDomStorageEnabled(true);
worked!!
When i enabled the java script, its working.
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("");
myWebView.getSettings().setDomStorageEnabled(true);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.setWebViewClient(new WebClient());
Solutions
If you want to load web url in WebView so you need follow my code It's code working fine and In web any type of script language used in web page.
public void webviewCallBack(String coverUrl) {
WebView WebView webView = (WebView) findViewById(R.id.web_view);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setPluginState(WebSettings.PluginState.ON);
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setDatabaseEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.clearView();
webView.measure(100, 100);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.setWebViewClient(new WebViewClient() {
// For api level bellow 24
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.d("weburl__", url);
if (url.startsWith("http") || url.startsWith("https")) {
// Return false means, web view will handle the link
return false;
} else if (url.startsWith("tel:")) {
// Handle the tel: link
handleTelLink(url);
// Return true means, leave the current web view and handle the url itself
return true;
}
return false;
}
// From api level 24
#RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
// Get the tel: url
String url = request.getUrl().toString();
Log.d("weburl_____", url);
if (url.startsWith("http") || url.startsWith("https")) {
// Return false means, web view will handle the link
return false;
} else if (url.startsWith("tel:")) {
// Handle the tel: link
handleTelLink(url);
// Return true means, leave the current web view and handle the url itself
return true;
}
return false;
}
});
// Load the url into web view
webView.loadUrl(coverUrl);
hoadler();
}
public void hoadler() {
Runnable task = new Runnable() {
public void run() {
material_design_progressbar.setVisibility(View.VISIBLE);
}
};
worker.schedule(task, 5, TimeUnit.SECONDS);
}
I hope this code is helpful for you!
Here is how to edit the AndroidManifes.xml:
Find AndroidManifest.xml file in application folder at:
android/app/src/main/AndroidManifest.xml
Locate the application subelement.
Add the following tag:
android:usesCleartextTraffic=”true”
The application subelement should now look like:
<application
android:name=”io.flutter.app.Test”
android:label=”bell_ui”
android:icon=”#`enter code
here`mapmap/ic_launcher”
android:usesCleartextTraffic=”true”>
Save the AndroidManifest.xml file.

i have a webview app and facing that when i click on Hyperlink then it open in default browse

I have a webview app and facing a problem that
when I click on Hyperlink then it opens in default browser.
But I want to open that link in same webview.
How to open it in same webview.
Here is my code.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView myWebView = (WebView) findViewById(R.id.myWebView);
myWebView.loadUrl("file:///android_asset/abc/index.html");
myWebView.setWebViewClient(new MyWebViewClient());
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
}
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("file:///android_asset/abc/index.html")) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
Do it like,
myWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
Return false in all cases.
ok.. you have added a WebViewClient also..
then try it in this way...
myWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
this should work for you..

Don't navigate to other pages in WebView,disable links and references

I have a webView in Android, and I open a html webpage in it. But it's full of links and images, and when I click one of them, it loads in my webview. I want to disable this behaviour, so if I click on a link, don't load it. I've tried this solution and edited a bit for myselft, but not worked.
My webviewclient code:
private boolean loaded = false;
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(loaded == false){
view.loadUrl(url);
loaded = true;
return true;
}else{
return false;
}
}
My webview implementation and settings.
WebView wv = (WebView) findViewById(R.id.recipeWv);
ourWebViewClient webViewClient = new ourWebViewClient();
webViewClient.shouldOverrideUrlLoading(wv, URLsave);
wv.setWebViewClient(webViewClient);
wv.setFocusableInTouchMode(false);
wv.setFocusable(false);
wv.setClickable(false);
WebSettings settings = wv.getSettings();
settings.setDefaultTextEncodingName("utf-8");
settings.setLoadWithOverviewMode(true);
settings.setBuiltInZoomControls(true);
Example: If the user open in my webview the StackOverflow homepage and clicks on one of the links(like "Questions") then the webview should stay on the StackOverflow homepage.
You should save in a class variable your current url and check if it's the same with the loaded one. When the user clicks on a link the shouldOverrideUrlLoading is called and check the website.
Something like this:
private String currentUrl;
public ourWebViewClient(String currentUrl) {
this.currentUrl = currentUrl;
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.equals(currentUrl)) {
view.loadUrl(url);
}
return true;
}
Important: don't forget set the WebViewClient to your WebView.
ourWebViewClient webViewClient = new ourWebViewClient(urlToLoad);
wv.setWebViewClient(webViewClient);
Implement a WebViewClient and Just return true from this method of WebView Client
webView.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return true;
}
});
If you want to open inner link of a web page in different window then
Don't use
WebView webView;//Your WebView Object
webView.setWebViewClient(new HelpClient());// Comment this line
B'coz setWebViewClient() method is taking care of opening a new page in the same webview. So simple comment this line.
private class HelpClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.example.com")) {
// This is my web site, so do not override; let my WebView load the page
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
Hope this will work.
Even I had been facing the same issue. I solved this issue like below
webView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
With lambda expressions
webView.setOnTouchListener((v, event) -> true);
Note that the method signature changed in API 24.
For APIs earlier than 24 the second parameter is String and that signature is now deprecated.
For APIs 24 and later, the second parameter is WebResourceRequest.
If your app supports both pre and post API 24 and you want to disable all links you can use this:
webView.setWebViewClient(new WebViewClient(){
#Override //for APIs 24 and later
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request){
return true;
}
#Override //for APIs earlier than 24
public boolean shouldOverrideUrlLoading(WebView view, String url){
return true;
}
});
You need to add an extra line of code, like so-
webview.loadUrl(url);
webview.setWebViewClient(new MyWebViewClient());
And add an additional class MyWebViewClient like this-
/* Class for webview client */
class MyWebViewClient extends WebViewClient {
// show the web page in webview but not in web browser
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public void onLoadResource(WebView view, String url) {
super.onLoadResource(view, url);
}
}
Using inject javascript On #Override onPageFinished
view.loadUrl("javascript: (function () {document.addEventListener('click', function (e) {e.stopPropagation();}, true);})()");
Very similar to Amit Gupta's answer, but I found a shorter way to do it.
private String currentUrl;
public CustomWebViewClient(String currentUrl) {
this.currentUrl = currentUrl;
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return ! url.equals(currentUrl);
}
After this CustomWebViewClient is defined, set the client to the webview.
CustomWebViewClient webViewClient = new CustomWebViewClient(urlToLoad);
wv.setWebViewClient(webViewClient);

WebView and mailto link

Problem resolved, I forgot to add this line:
myWebView.getSettings().setJavaScriptEnabled(true);
I havew a WebView with a email link.
Here is my HTML email link:
href="javascript:location.href='mailto:'+String.fromCharCode(97,110,105,109,97,108,115,64,109,100,112,105,46,99,111,109)+'?'">animals#mdpi.com</a>.</p>
I am trying to catch when the user clicks on this link with this code:
myWebView.setWebViewClient(new WebViewClient()
{
#Override
public boolean shouldOverrideUrlLoading(WebView wv, String url) {
if(url.contains("mailto")){
System.out.println("mailto");
}
return true;
}
});
But I am never entering in the if.
Here is the solution
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.setWebViewClient(new WebViewClient()
{
#Override
public boolean shouldOverrideUrlLoading(WebView wv, String url) {
if(url.contains("mailto")){
System.out.println("mailto");
}
return true;
}
});

Categories

Resources