Invoke click through code in Webview - java

my name is Dan and I am trying to invoke a click on a HTML element through code in my Android application. If I try to click it through pure Javascript the result won't be the same as it is when the user clicks it.
So, how to press elements from a Webview component through Java code? Thanks in advance.

wv.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String link) {
return super.shouldOverrideUrlLoading(view, url);
}
});

Related

programmatically click on a HTML button in WebView from fragment in Android

I need to open a dialog in a website which opens on clicking a HTML button of the website. I'm trying to click on a HTML button based on the id from the fragment, how can I perform the action, I'm using
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
// trying to click the button with id btn-to-be-clicked
webView.loadUrl("javascript:(function(){document.getElementById('btn-to-be-clicked').click();})()");
}
}
thanks
I tried
webView.loadUrl("javascript:(function(){document.getElementById('btn-to-be-clicked').click();})()");
so apparently you must specify every line in a new string
extracted the js to a variable with multi line, it has to be a multi line concatenated string
val jsFunction = "javascript:(function() { " +
"document.getElementById('btn-to-be-clicked').click();" +
"})()"

webview onPageFinished does not detect the selection of a video from the home page on YouTube mobile version

I entered this code under onPageFinished:
edittext1.setText(webview1.getUrl());
When I google YouTube and i select YouTube, edittext changes its name, But when I select a video on the home page in YouTube page, edittext does not change the name
Why don't you try this code? :
binding.webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url){
binding.urlEditText.setText(url); // set the url
view.loadUrl(url);
return false;
}
});

WebView - only show back button if back history exists

I developed a simple webview app. I added a button which should act as the back button, so users can navigate back one page.
The button is initially hidden and should only show if a back history exists.
How can I realise this?
The code is simple:
backButton = findViewById(R.id.backButton);
backButton.setEnabled(blizzView.canGoBack());
but where do I have to call this? How is the "some site loaded" event called?
Update:
I tried to apply #Murats answer, but nothing happens:
private WebView blizzView; // my webview
private Button backButton;
public void onPageFinished(WebView view, String url)
{
backButton = findViewById(R.id.backButton);
backButton.setEnabled(blizzView.canGoBack());
if (blizzView.canGoBack()) {
backButton.setVisibility(View.VISIBLE);
} else {
backButton.setVisibility(View.INVISIBLE);
}
}
You can use WebView#canGoBack to find out if there is a backstack. You can check for that after the page is loaded e.g. in WebViewClient#onPageFinished
It works like this:
blizzView.setWebViewClient(new WebViewClient() { //
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
backButton = findViewById(R.id.backButton);
backButton.setEnabled(blizzView.canGoBack());
if (blizzView.canGoBack()) {
backButton.setVisibility(View.VISIBLE);
} else {
backButton.setVisibility(View.INVISIBLE);
}
}
});

How to detect clicks on links in webview

I use this code to check if a user touched inside a webview
webview.setOnTouchListener(new View.OnTouchListener()
{
public boolean onTouch(View v, MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_UP)
{
//action
}
return false;
}
});
but I will need to do action only if user clicked (touched ) a link only ,not if the the user touch some pictures or do a scrool in webview.
I need to reinit some variables when new webpage is loaded ,or when a user clicks on a link , but not when he click random on screen or he do scrool.
i can not use
public void onPageStarted(WebView view, String url, Bitmap favicon)
or
public boolean shouldOverrideUrlLoading(WebView view, String url)
cause this are called multiple times while a page is loaded and I only need to do action first time only 1 time , when user click on a link (or button or something that make webview to load a new page)
Does anybody knwo how to do this?
Thank you in advance
first step - enable JavaScript:
WebView myWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
second step - create interface between Java and Javascript:
public class WebAppInterface {
Context mContext;
/** Instantiate the interface and set the context */
WebAppInterface(Context c) {
mContext = c;
}
/** Show a toast from the web page */
#JavascriptInterface
public void showToast(String toast) {
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}
}
register your interface:
webView.addJavascriptInterface(new WebAppInterface(this), "Android");
and put this JS in your html page:
<script type="text/javascript">
function showAndroidToast(toast) {
Android.showToast(toast);
}
</script>
example for html:
<input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" />
if pages which you are showing are not your you might download them as plain text, then insert JS and load html with myWebView.loadDataWithBaseURL. this way you have full control of which user is clicking and what to do with it
to be honest I don't understand why you cannot do this with shouldOverrideUrlLoading(WebView view, String url) by checking what is arriving in url String and setting 'consumed' flag or smth like this
also: whole code is stolen from HERE

Android listener for webpage to start loading?

I have a simple webview that loads a website. Once the website is loaded, I have the following to focus/scroll to the login box:
mWebview.setWebViewClient(new WebViewClient(){
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
mWebview.scrollTo(681, 100);
(I've heard that onPageFinished is deprecated, but it's working for me in 2.2>4.4, so I'm just leaving it for now.
Anyway, I would like some sort of way to monitor for when the user actually logs in, that way when the page is done loading for the second time, I can then call some js and forward them to another page, but I have no idea how to do that. :(
I can just do another onPageFinished after they log in, but I don't know how to start monitoring... In other words, I can't start another onPageFinished immediately after the current, because it just redirects automatically.
Does anyone have any suggestions? I would greatly appreciate it! :)
EDIT:
Here's the entirety (minus the import headers) of my MainActivity.java class.
public class MainActivity extends Activity {
private WebView mWebview ;
#Override
public void onBackPressed() {
if(mWebview.canGoBack() == true){
mWebview.goBack();
}else{
super.onBackPressed();
}
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mWebview = new WebView(this);
mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript
mWebview.getSettings().setBuiltInZoomControls(true);
if(android.os.Build.VERSION.SDK_INT >= 11) {
mWebview.getSettings().setDisplayZoomControls(false);
}
final Activity activity = this;
mWebview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
}
});
mWebview .loadUrl("http://example.com");
setContentView(mWebview );
mWebview.setWebViewClient(new WebViewClient(){
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
mWebview.scrollTo(681, 100);
}
}
);
}
I think I might be missing a curly bracket, but ignore that for now, haha. So anyway, after mWebview.scrollTo (the last line), the webpage is done loading, and it just chills at the login page. After a user logs in, the page (obviously) starts to load and directs to another post-login URL. I'd like to check the URL with an if statement after the page is done loading for the second time.
Does that make more sense? Sorry, it's confusing me too, trying to explain it.
Usually there will be two separate URLs, one for the post log-in page and one for the pre log-in page. You can have an if statement that checks the URL to make sure you've logged in, and if you have, then it redirects/closes/whatever.
I had a similar issue here:
Closing a Webview in Android
So basically you just want to execute some JavaScript once you've logged in? I don't see why you can't have an if statement to check whether you're logged in or not, and if you're logged in, then execute the js.
Something like this maybe?
wv.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
if (url.contains(getString(R.string.loginURL))){
super.onPageFinished(view, url);
mWebview.scrollTo(681, 100);
}else if(url.contains(getString(R.string.loggedInURL))) {
wv.loadUrl(js goes here);
}
}
}
This will execute every time a page is finished loading in your WebView

Categories