Android WebView Links Does not Open - android

How can I click on a link that is provided in the WebView data that I load?
The WebView data that is load is in HTML format as:
<P CLASS="western" ALIGN=JUSTIFY STYLE="margin-bottom: 0.14in">Further
here</P>
The WebView is placed inside a scroll view and its webClient is as:
webViewClient = new WebViewClient()
{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
#Override
public void onLoadResource(WebView view, String url)
{
}
};
webView = (WebView) findViewById(R.id.tos); // Defining the WebView for the terms and conditions and loading the required data
webView.setWebViewClient(webViewClient);
webView.loadData(getResources().getString(R.string.terms_of_service), "text/html", "UTF-8");
The problem is that when I click on the Link no action takes place. How can this be resolved?

use this code to open
WebView webView;//make sure to initialize
webView.setWebViewClient(webViewClient);
WebViewClient webViewClient= new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
return true;
}
#Override
public void onLoadResource(WebView view, String url){
if( url.equals("http://yoururl.com") ){
// do something
}
}
}

Related

Access remote pdf file with login (authentication)

I am facing problem with loading URL in web-view. Web-view shows blank because it is not logged in. How to access course file without login into moodle2.6 by using url.
Using this url format
http://example.com/pluginfile.php/4418/mod_resource/content/10/xx-t2.pdf
Here is my web-view code
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//progDailog.show();
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, final String url) {
//progDailog.dismiss();
}
});
webView.loadUrl("http://example.com/pluginfile.php/4418/mod_resource/content/10/xx-t2.pdf");
You need to append the pdf URL to Google Doc Viewer to open pdf in web view, Also you will need to enable JavaScript for your webview
public class MainActivity extends AppCompatActivity {
WebView webview;
String pageURL = "https://developer.android.com/guide/webapps/webview.html";
String pdf = "http://unec.edu.az/application/uploads/2014/12/pdf-sample.pdf"; //your pdf address
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webview = findViewById(R.id.webview);
WebSettings webSettings = webview.getSettings();
webSettings.setJavaScriptEnabled(true);
webview.getSettings().setJavaScriptEnabled(true); // enable javascript
webview.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//progDailog.show();
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, final String url) {
//progDailog.dismiss();
}
});
//webview.loadUrl(pageURL);
webview.loadUrl("http://drive.google.com/viewerng/viewer?embedded=true&url=" + pdf); //simply append pdf address to doc viewer
}
}
Sample PDF opened in webview
It seems that you're webclient is not calling their super methods, which could be one reason for the page to not load, use the following code to load the login page, which once signed in the user could view the intended pdf file.
webView.setWebViewClient(new WebViewClient(){
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);//<-- this is important when overriding
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);//<-- this is important when overriding
}
});
webView.loadUrl("https://lms.jsbl.com//pluginfile.php//4925//mod_resource//content//0//calendar_cal_605.pdf");
And also If you want you view a pdf using the webview you need append the following before your url so that the google plugin could be used to load the pdf
https://docs.google.com/gview?embedded=true&url=
(for this to work make sure you have added the following to your webview
webView.getSettings().setJavaScriptEnabled(true);webView.getSettings().setPluginState(PluginState.ON);)
According to this question some login form submit might not work on android webview so set all/some(check if your login works without it) on your webview to make the login work
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.getSettings().setDatabaseEnabled(true);
mWebView.getSettings().setDatabasePath(dbpath); //check the documentation for info about dbpath
mWebView.getSettings().setMinimumFontSize(1);
mWebView.getSettings().setMinimumLogicalFontSize(1);
But if you know the credentials for the login You could follow the instruction on this site to inject a javascript to the webview which would fire the Login form submit on the webview and redirect to the pdf file directly
UPDATE 16/3/2018
Use the following code which will append the google pdf widget during a redirect
and has a pdf extension
boolean isInititialRedirectComplete = false;
boolean isPdfRedirectComplete = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
f-inal WebView webView = (WebView) findViewById(R.id.testWebView);
webView.getSettings().setJavaScriptEnabled(true);webView.getSettings().setPluginState(WebSettings.PluginState.ON);
webView.setWebViewClient(new WebViewClient(){
#Override
public void onPageFinished(WebView view, final String url) {
super.onPageFinished(view, url);
if (url.substring(url.lastIndexOf(".")).equalsIgnoreCase(".pdf") && !isPdfRedirectComplete && isInititialRedirectComplete ){
isPdfRedirectComplete = true;
webView.loadUrl("https://docs.google.com/gview?embedded=true&url=" + url);
}
isInititialRedirectComplete = true;
}
});
webView.loadUrl(
"https://lms.jsbl.com//pluginfile.php//4925//mod_resource//content//0//calendar_cal_605.pdf");
}

Android - Detecting WebView URL not working

I am developing an app, in that I want to detect each time when a new url gets loaded in webview and then display that url in edittext.
I tried many solution and googled. But, not found significant solution which solves my purpose..
My Code is:
wv.getSettings().setJavaScriptEnabled(true);
wv.setWebViewClient(new WebViewClient()
{
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
String url_new = wv.getUrl();
Log.v("","Webview URL: "+url_new);
edittext.setText("http://"+url_new);
return true;
}
});
Not even printing the Log Statement :(
Please Help..!!
Thanks in advance..!! :)
First off the code you're posting is returning true from shouldOverrideUrlLoading which means you've just prevented the webview from navigating to any location.
Second off, shouldOverrideUrlLoading will not get called for any urls you pass to the webview.loadUrl API. As suggested in one of the other answers, onPageStarted is the simplest API to use for this.
You can use the code below to play around with the APIs:
wv.setWebViewClient(new WebViewClient()
{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
Log.v("","Webview shouldOverrideUrlLoading URL: " + url);
return false;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon){
Log.v("","Webview onPageStarted URL: " + url);
}
});
This is the code I use in my applications.
WebView view = (WebView) findViewById(R.id.myWebView);
EditText edit = (EditText) findViewById(R.id.myEditText);
view.setWebViewClient(new WebViewClient(){
public void onPageStarted (WebView view, String url, Bitmap favicon){
edit.setText(url);
}
});
Set WebViewClient to your webview and geturl on when page finshed laoding
class MyWebView extends WebViewClient {
#Override
public void onPageFinished(WebView view, String url) {
editText.setText(view.getUrl());
}
}
set this WebViewClient to your webview ..
webView.setWebViewClient(new MyWebView());
You can override onLoadResource() method of WebViewClient :
wv.setWebViewClient(new WebViewClient() {
public void onLoadResource(WebView view, String url) {
edittext.setText("http://"+url_new);
}
}

PopUp screen not opening In webview?

I am Currently working with webview in which Javascript popup is created when some button is clicked in webview. But it is not opening in my Webview.
My code is Here:
wvPayements = (WebView) view.findViewById(R.id.wvPaymentsPage);
wvPayements.getSettings().setLoadWithOverviewMode(true);
wvPayements.getSettings().setUseWideViewPort(true);
wvPayements.getSettings().setSupportZoom(true);
wvPayements.getSettings().setAllowFileAccess(true);
wvPayements.getSettings().setJavaScriptEnabled(true);
wvPayements.getSettings()
.setJavaScriptCanOpenWindowsAutomatically(true);
wvPayements.getSettings().setBuiltInZoomControls(true);
wvPayements.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, final String url) {
dialog.cancel();
}
});
wvPayements
.loadUrl("http://192.168.0.114/Parch_ws/testalert.html");
You have to use WebChromeClient for your purpose:
One should use a WebChromeClient to get these alerts and the
functionality can be overwritten using theonJsAlert() method. Hope
this helps!
WebView wv=new WebView(this);
wv.setWebChromeClient(new WebChromeClient() {
#Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
//Required functionality here
return super.onJsAlert(view, url, message, result);
}
});
Source: https://issuetracker.google.com/issues/36905249#comment11
This can also happen if you are executing ES6 on unsupported browsers.
You can always check supported versions here

Android webview app shows "complete the action using" popups

I am working with android app for the web view. It loads the webpage but i cant make more actions on the page using my app. when I am choosing a hyperlink from the page it shows a popup"complete the action using chrome,opera etc.." how can I use this without such browsers..I nedd to browse web contents using my app. how it possible??
I used the code
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://www.google.com");
please help me since I am new to android and thank you.
wv.loadUrl(url);
wv.setWebViewClient(new MyWebViewClient());
Your WebViewCLient class would look like this:
class MyWebViewClient extends WebViewClient {
#Override
// show the web page in webview but not in web browser
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
webProg.setVisibility(View.GONE);
}
#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);
}
}

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

Categories

Resources