Capturing webview clicks (not just from links) - android

Is there some way to set an onClickListener for a webview? I've seen code here to intercept url clicks but I would just like to intercept anytime a user clicks the area the webview's on or swipes across it with their finger. I load html directly into the webview (as opposed to giving it a url) for formatting reasons and I'd really like to be able to tell any time the user clicks anywhere on the screen. I had been using a textview and that works fine, but is it doable with a webview?

Have you tried instead of an onClick, an onLongClick listener?
Taken from my recent project:
printMessageTextView.setOnLongClickListener(new OnLongClickListener() {
int active = 0;
#Override
public boolean onLongClick(View v) {
In this case, the int active = 0; is probably not needed. But should be able to listen for you.

Related

WebView tries to submit form when only one input is available on page

I need to load an HTML page with a form that has only one number input field. The desired behavior is:
When the user enters some data in this field and clicks on forward button from the Android's keyboard, a function is executed and a value appears, without taking him to another page.
The problem is that when the user clicks forward, the app crashes due to a FileUriExposedException. After searching a bit, I saw that this error is related to this bug.
One of the solutions shown in (2) was add an extra invisible form field, but does anyone know another solution? Because I think that changing all HTML forms will be something much more time consuming than trying a solution via Android.
Not sure if avoiding clicking forward would help, but here is a way you can submit to the form without the user typing in the form directly, if you have them enter the number into an edittext, save its value and then use something like this. You can set the webview visibility to invisible if you want to start and then make it visible after it submits. You can find out the IDs of the elements in the form by right clicking and hitting Inspect.
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.loadUrl(getString(R.string.form_url));
mWebView.setWebViewClient(new WebViewClient(){
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
String fieldID;
String submitButtonID;
int numberTypedInByUser
mWebView.loadUrl("javascript: {" +
"document.getElementById('"+fieldID+"').value = '"+numberTypedInByUser+"';" +
"var z = document.getElementById('"+submitButtonID+"').click(); };");
}
});

Android longpress not being passed to webView as oncontextmenu event

My app is designed to be used outdoors (yachting) and displays a web page in a WebView (so I can use all the display area, fix in landscape, disable extraneous inputs like the BACK_KEY etc.).
In the web page, I want to capture the oncontextmenu event on an image like:
<img src="start_line_pin.png" width=55px
id="pinButton"
oncontextmenu = 'startLinePress("PIN"); return false'\>
When I open the page in my app's webview, a long press doesn't fire the event. Android doesn't seem to be passing the longpress event to the web page.
If I open the page directly in Chrome, my startLinePress function is called with a long press as I intended.
So, can anyone suggest how I get the longpress to be passed into the HTML in my WebView instead of it being handled by Android?
One of the most beneficial features of a forum such as this is that it makes you really think about your problem from another point of view.
The answer to my problem lies in the fact that I was trying to use an undocumented feature - the longClick on the web page invoking the oncontextmenu event.
The answer is to use the onLongClick event in java and then pass the event to the javascript function by using the WebView.loadUrl method. My WebView is contentView and the javascript function is javascript:startLinePress as follows:
contentView.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
WebView.HitTestResult hr = ((WebView)v).getHitTestResult();
if(hr.getType() == 5){
contentView.loadUrl("javascript:startLinePress(\"ACTIVITY\")");
}
It needs a little more work to identify which element was clicked, by examining hr.getExtra() but you get the general idea.
Thanks stackoverflow for the great forum.

How to scroll a webview when the page finish loading

here's what I'm trying to achieve: I assemble a local html page with some tags and internet resources in it, and then load the html page webpage with WebView, and there's a "View More" button at the end of the page, when somebody clicks the button, I'll re-assemble the html page(with old data and new data, like refreshing twitter to load more twits) and have the WebView load it again. But as the user has scrolled to certain location when he clicks the button so I'd like to let the WebView to scroll to the very point where the user was before he clicks the button.
And here's what do by now, I create a WebViewClient and implements:
public void onPageFinished(WebView view, String url) {
view.scrollTo(last_X, last_Y);
}
It does not work as I expect it to, I have 2 concerns, guys please help me out:
1. this callback function does not work all the time. for most of the time, it works, but sometimes, for unknown reasons, it just does not work.
2. even if it works, it's not exactly what I want. as I mentioned, the html page contains some internet resources like
<a href='whateversite/whateverimage'><img src='whateversite/whateverimage'></img></a>
So the scrollTo function only got called when all images are loaded, it takes too much time and it's unnecessary, is there any way to start scrolling when the page is loaded but before all other resources got loaded? Per say, as long as webview.getContentHeight() > 0, it's OK to scroll.
Apologize for my poor english that I'll have to use load of words to try to make myself clear.
Guys, please halp~
Do something like this :
view.post(new Runnable() {
public void run() {
view.scrollTo(last_X, last_Y);
}
});

How to automate clicking a link inside a WebView

Ok, guys. I know it sounds weird but what I want to achieve is when i run the app. The webview first load the link and after I enter all the details, it must automatically click a button inside the webview. Is there any possible way to do this?? I have tried with loading different links in order to open the new window.
for example,
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ourBrow = (WebView) findViewById(R.id.wvBrowser);
// cancel the web intent that default in android setting
ourBrow.setWebViewClient(new WebViewClient());
url = (TextView) findViewById(R.id.tvSC);
// webView settings
ourBrow.getSettings().setJavaScriptEnabled(true);
ourBrow.getSettings().setLoadWithOverviewMode(true);
ourBrow.getSettings().setUseWideViewPort(true);
Button SCode = (Button) findViewById(R.id.btSCode);
loadUrl("http://www.google.com.au");
this part works well, but what I want is when the length inside the search bar is equal to 4, the "search" button will automatically press
thanks so much for the help!!!
You could perhaps do this by executing some awkward javascript in the webview.
To just issue the click you could probably do something like
ourBrow.loadUrl("javascript:getElementById('button_id').click()");
Since you need to wait for the search bar to have a lenght of 4 you will probably want to register a listener on the field in javascript and issue the click there. I can't tell you the best way to do this, but you will probably need to set an eventlistener for 'change' or set onKeyUp or something similar and see if the field is of length >= 4 then issue the click. You could nest all of this in the javascript row.
Set it after the page has loaded, that is. Set a WebViewClient on the webview and load the javascript in the onPageFinished(WebView view, String url) callback or something similar.

Limit WebView touch handling to links

Is it possible to capture touch events over a WebView in the activity that contains it, without loosing link functionality?
Consider a WebView showing a webpage with links. If the user taps on a link I would like the WebView to handle the touch event. If the user taps somewhere else I would like the activity to handle the touch event.
How can this be done?
Yes, it is.
(I can't elaborate more on this unless you are more specific on your question.)
If i understand your question correctly it'd seem difficult to interpret whether something is a link or not in the onTouchEvent() since all it knows about is X,Y coordinates (not html). However, off the top of my head it seems you could probably use the javascript binding piece in WebView to have javascript decide if the something is a link or not and act accordingly. Again, I haven't done this before nor tested it...just thinking out loud.
When initializing your WebView, try:
mWebView.setWebViewClient(new WebViewClientOverrider());
Then create a private class:
private class WebViewClientOverrider extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//TODO handle the link
return true;
}
}
Lastly, replace the TODO handle the link line with your own code for handling the link selection.
Or you can monitor the stack trace for:
android.webkit.CallbackProxy.uiOverrideUrlLoading()
See http://www.javapractices.com/topic/TopicAction.do?Id=78 for how to determine your stack trace from a throwable (created in an overriden WebView.loadUrl() method).

Categories

Resources