How to get Android WebView Link Click URL - android

I want to show the URl of a video in a TOAST, each time the user click on a video. I get this toast shown only once when I Launch an app first time. Here is my Code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mywebview=(WebView)findViewById(R.id.webView);
mywebview.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
Toast.makeText(MainActivity.this,url, Toast.LENGTH_SHORT).show();
return true;
}
});
mywebview.getSettings().setJavaScriptEnabled(true);
mywebview.loadUrl("http://www.youtube.com");
}

Related

How to get url after loading page in webview

I need to take url after loading a web page in my web view to check condition and if I use onPageFinished() method, then where to use.
Just set a webview client and override onpagefinished as shown below
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webView = findViewById(R.id.webview);
webView.loadUrl("http://www.google.com");
webView.setWebViewClient(new WebViewClient(){
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
Toast.makeText(getApplicationContext(), url,Toast.LENGTH_LONG).show();
}
});
}
}
inside onpagefinished you have the url

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

Show Interstitial Ad On Button Click In WebView

i make apps of my Wordpress websites for android and i use WebView in my apps, i want to show Interstitial Ads in my app but when users click on Next Page Button Which is coded in my website, my next page button is like this
<?php next_posts_link( '<span class="nextpost"> '.__('Next', 'wp-mobile-edition').' ›</span>' );
i want to show Interstitial ad when user click on next button.
how it can be ? thanks
Try below code
#SuppressLint("SetJavaScriptEnabled")
public class MainActivity extends Activity {
private WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView)findViewById(R.id.webView1);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setLoadsImagesAutomatically(true);
mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
mWebView.setWebViewClient(new MyBrowser());
mWebView.loadUrl("file:///android_asset/index.html");
}
private class myWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
view.addJavascriptInterface(new Object()
{
#JavascriptInterface
public void performClick() throws Exception
{
Toast.makeText(MainActivity.this, "Login clicked", Toast.LENGTH_LONG).show();
// implement your logic here for show Add
}
}, "login");
return true;
}
}
}

Web View onclick event for a specific link in that webview

I have a webview and I am calling data in that wv from a webservice, and in that whole description in a webview, there is a link in the last. so, my problem is that i want to open a new activity onclick of that link only neither onclick of webview nor ontouch of webview
You need to provide an implementation for shouldOverrideUrlLoading. You'll have to setup a WebViewClient for your webview and inside of this method, you'll need to have some logic that recognized that link and then open the new Activity. Something like:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView wv = (WebView) findViewById(R.id.myWebView);
wv.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(isURLMatching(url)) {
openNextActivity();
return true;
}
return super.shouldOverrideUrlLoading(view, url);
}
});
}
protected boolean isURLMatching(String url) {
// some logic to match the URL would be safe to have here
return true;
}
protected void openNextActivity() {
Intent intent = new Intent(this, MyNextActivity.class);
startActivity(intent);
}

Trying to display url in Web View

I am experimenting with the loopj package. I am trying to make a HTTP request to a website and display the website in the webview.
I am successfully getting a result back, however the web view does not display the page as desired, instead chrome opens up and displays the page.
Am I missing something or is there a way I can override this unwanted behaviour?
Below is my oncreate method where I am making the request:
public class MainActivity extends Activity {
Button connectBtn;
TextView status;
WebView display;
String url = "http://www.google.com";
AsyncHttpClient client;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
status = (TextView)findViewById(R.id.statusbox);
connectBtn = (Button)findViewById(R.id.connectBtn);
display = (WebView)findViewById(R.id.webView1);
connectBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
client = new AsyncHttpClient();
client.get(url, new AsyncHttpResponseHandler(){
#Override
public void onSuccess(String response) {
Toast.makeText(getApplicationContext(), "Success!", Toast.LENGTH_SHORT).show();
display.loadUrl(url);
}
});
}
});
}
setWebViewClient to your WebView and override shouldOverrideUrlLoading() now write view.loadUrl(url); in that method.
Just add this code,
display.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}});
You need to set a WebViewClient and override the shouldOverrideUrlLoading method. Something like this:
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(final WebView view, final String url) {
view.loadUrl(url);
}
});
That makes sure that clicks on links in the WebView are handled by the WebView itself.
Edit: Actually, I misread the question. You aren't dealing with a click in the WebView itself, so this isn't relevant. Sorry!
just use this code under you webviewclient
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
final EditText editText = (EditText) findViewById(R.id.urlfield);
editText.setText(url);
}
}

Categories

Resources