Intercepting the call to display of dropdown in Webview - android

While using webview, i can set up a WebChromeClienton it and intercept the alerts(and then customize it also) by overriding
onJsAlert(WebView view, String url, String message,JsResult result)
Is there a way that i can intercept the display of dropdown dialogs( that comes from <select> tags of HTML)? Basically i wanted to customize the view of that dialog? I tried overriding a few functions in WebChromeClient but it didnt work.

<select> elements are handled inside the rendering engine, there are no callbacks provided by WebView for customizing them.
The best you can do is to use JavaScript code in the web page you display. See for example this answer: https://stackoverflow.com/a/23515955/4477684.

Related

Webview within a scroll view loading html wrongly on first load

I have done my research and I haven't found a suitable question for my problem. I got a WebView inside a ScrollView, and once I get the HTML from my service, I call
myWv.loadDataWithBaseURL(null,myHtml,"text/html","utf-8",null);
Nothing special there. The problem I'm having is that, when I run my Application in a 4.4.2 Android tablet, the WebView loads the HTML with the words split in lines, eg:
This
is
my
sentense
And after a second it renders the HTML correctly but the ScrollView's scroll is as big as the number of lines it rendered before loading the HTML correctly. I have tested in 3 different smartphones and everything is fine, only in the tablet it seems to happen, probably due to some performance issue since my HTML is huge.
Does any of you have have had this problem before and knows how to solve it? Thanks in advance.
Extend WebViewClient. And in your custom WebViewClient overload the method onPageFinished.
#Override
public void onPageFinished(WebView view, String url) {
// If you create a listener, here you can trigger it
// you can show the webview here as it will be already measured.
}
If you show the webview when the page is already entirely loaded there won't be any ugly animation.

Webview Android - Add listeners to HTML elements wthout using Javascript

I would like to know if there is a way to the WebView component to support a generic listener for html components. For example, the html contains an img tag with a src attribute and I would like to handle this click on java side without using specific javascript, since I don't have access to the HTML being loaded.
I noticed that Android Studio prints logs into console, so maybe there is a listener for that I can override?
I've tried using the WebView onClick handler, but it is for the component and not its content, so I wasn't able to track the html element which triggered the click to get the src value, for example.
Also, the shouldOverrideUrlLoading method from WebViewClient doesn't work either, since it is just a click and not an url change. :(
"I would like to handle this click on java side without using specific javascript, since I don't have access to the HTML being loaded."
Ah, but you do! You can use [evaluateJavaScript](https://developer.android.com/reference/android/webkit/WebView.html#evaluateJavascript(java.lang.String, android.webkit.ValueCallback)) to run some JavaScript on the page to set up the click handler you want.

Stop Android WebView automatically creating (focusable) links

I'm using a WebView to display some (locally stored) HTML. The WebView automatically converts various parts of the text to clickable links; this is somewhat useful for web addresses, etc. but it often unfortunately mistakes numbers (especially years) in the page as phone numbers and turns them into links also.
Assume I am happy to disable all links in the page. Visually, I can get part-way there by applying various CSS styles, in particular, setting appropriate values for -webkit-touch-callout and -webkit-tap-highlight-color will make the links appear as normal text. I can then set a custom WebViewClient which overrides shouldOverrideUrlLoading(WebView view, String url) (returning true) to prevent the WebView loading the URL.
For touch access, this works fine and certainly gives the impression that there are no links in the page. However, if used on a device with a trackball/directional pad we can still focus on each of the links, and an orange border appears around them.
Is there any way to (ideally) have more control over the auto-linking 'feature' of a WebView, or otherwise stop links from being visually focused when using a directional pad?

Android: How to interact with the contents of a WebView?

Is there a way to programmatically interact with the contents of a WebView?
For example, how could my app:
respond to an event (such as button clicked on a web page)
iterate through all the INPUT elements in a page and retrieve their contents?
Find all DIV elements with the class status and change their content?
Thanks!
You can use Javascript to make a callback to Java for anything that Javascript can detect.
You can see a demo call JS to Java here: Android Calling JavaScript functions in WebView

Android: Put EditText on top of WebView

I have been trying to mimic the behavior of the android browser with the scrolling of the address bar on top of the WebView. If you notice, if the user scrolls down, the address bar "moves" up with the WebView (but the WebView doesn't scroll). Only when the address bar disappears completely the WebView starts to scroll. At first I tried to override the onScrollChanged method of WebView, and got something but it wasn't as smooth as the desired goal. I noticed in the docs that WebView inherits from AbsoluteLayout, so I was wondering if it's possible to add a View programmatically on top the "browser" in the WebView and by that achieve the desired scrolling effect?
EDIT
Ok so after poking around in the source code of the native browser app, I found out that there is an hidden method for that called setEmbeddedTitleBar(View v)
And here is the description (from the Android source):
/**Add or remove a title bar to be embedded into the WebView, and scroll
* along with it vertically, while remaining in view horizontally. Pass
* null to remove the title bar from the WebView, and return to drawing
* the WebView normally without translating to account for the title bar.
* #hide*/
Do you know how I can hack my way to use this ?
You could probably still use reflection to access it. See:
Using Reflection for Backward Compatibility for Android (Android Developers)
Calling a Method Using Reflection (StackOverflow)
As far as I can see this method is public. So you can use it I think.
I've just looked through its implementation. It just calls addView() method of WebView object. So if you don't want to use hidden method you can reimplement this method.

Categories

Resources