How to scroll Android WebView programmatically to a specific element - android

I load an HTML string directly into a WebView with loadDataWithBaseURL(null, html, null, null, null) and later I'd like to be able to scroll programmatically to an internal element, e.g. <p id="pos1"> or <a name="pos1"></a>
I found a similar question, where the answer was to use javascript:
https://stackoverflow.com/a/2239301/2534762
But is that really the only way? Isn't there another simple way to say directly to a WebView: "go to #pos1" without having to embed a script in every HTML page that you want to scroll? It seems to me a common task begging for a simple solution, but apparently I'm wrong... or is there a way to do that, maybe calling loadUrl or loadDataWithBaseURL in some particular way?

I found that I don't need to embed JavaScript in the HTML page, as suggested in that other answer, I can instead write one line of JavaScript directly in the URL:
webView.loadUrl("javascript:document.getElementById('"+aID+"').scrollIntoView()");

Related

Web View in Android

In my android app whole web page is loading in web view to which i gave the link, but I want to load only some component from that web page not rest of the component. Can anybody tell me is it possible to load only required component from web page and how?
It can be done using Jsoup html parsing library.
1.Get the data from url to jsoup document.
Document doc = Jsoup.connect("http://example.com/").get();
remove the unwanted content html tag using remove().
doc.select("span[style*=display:none]").remove(); //put unwanted tag inside.
Save the remaining content to a string.
String newcontent=doc.toString();
4 Set the newcontent as Webview content.
webView.loadDataWithBaseURL(null, newcontent, "text/html", "UTF-8", null);
Sorry for poor english.
I dont know if there is a standard api to do this, but if not you could load the html first then manipulate it. So you could delete the areas you dont need and only display the necessary parts.
Cheers

Issue with webview

In my application i use a mix of html and native. In my web view i load a html page which has a which has an image in it. On click of the image i call a java script function which in turn calls the native code. My html tag is as below:
<img onclick=nextLevel('0'); id="d2">
My javascript method is as below:
function nextLevel(index)
{
Android.displayNextLevel(index);
}
Within the displayNextLevel method i start the next activity. The issue is when i click on the image on the html page multiple times the event gets triggered multiple times and the activity opens up multiple times. Am i missing out on something? How do i overcome this issue? Kindly help me with this. Thanks in advance.
I guess you could fiddle around in the html/js to make sure you can only click it once every now and then.
You could also ditch your function and instead use the JavascriptInterface so you can handle stuff in your Java code instead.

What is the equivalent of Android's Html.fromHtml() in WP7?

Is there a quick and dirty way to render HTML in a textblock in a fashion similar to Android's Html.fromHtml()? I am aware of how to manually parse it with something like the HtmlAgilityPack, but all I really want is for it to render like its source in the textblock.
If not naively then perhaps with a custom control of some sort and, no I don't want to render it as a web page.
Ok sorry it took so long. I all but forgot how to use git correctly and hadn't had the time to upload till now. This HtmlTextBlock offers a similar level of functionality as to that of its silverlight counterpart which is pretty close to the android equivalent. Its still a bit buggy at times when dealing with more complex tags like the html dtd tag but does the job....
WP7 Html Text Block. The design is largely based on this guy's Bringing-a-bit-of-html-to-silverlight-htmltextblock-makes-rich-text-display-easy. and rewriting the web browser related classes using html agility. One day I'll post the details but, blah... Not right now. lol
Update
Example of usage:
<local:HtmlTextBlock x:Name="htmlTextBlock" Canvas.Left="2" Canvas.Top="2" TextWrapping="Wrap" UseDomAsParser="true" Text="&ltp&gtYour Html here &lt/p&gt" />
Note: Your html will have to be escaped such that &lt = < and &gt = >
For detailed usage see:
https://github.com/musicm122/WP7HtmlTextBlock-/blob/master/HtmlTextBlockTest/HtmlTextBlockTest/MainPage.xaml

how to get html source code in android?

What would the best approach to :
get html codes by calling url and displaying it in some views
if i would be editing the values against title tag, it must be automatically updated in the url link?
How to do this?
Check out the JSoup cookbook.
http://jsoup.org/cookbook/
This has a lot of good information in it and should be able to answer most of your questions.
You are probably looking for something along the lines of this: http://jsoup.org/cookbook/input/parse-body-fragment

Determine element of url from webview shouldOverRideUrlLoading

I am displaying a html in a custom activity with a webview. I am overriding
public boolean shouldOverrideUrlLoading(WebView view, String url) {
to intercept certain urls. Now I would like to be able to figure out some more details about the html around that url.
In my case I would like to get the html element that contains the url. E.g. if the url comes from
Manfred Moser
I would like to be able to somehow retrieve the value "Manfred Moser" from within the anchor tag. Is there a way to do that. I found that you do not have access to the DOM from Java.
One way I can think of would be to download the page separately and parse it all before even loading it in webview. However that is an ugly hack at best. Is there a better way?
There is a dirty hack:
Bind some Java object so that it can be called from Javascript with WebView:
addJavascriptInterface(javaObjectExposed, "JSname")
Force execute javascript within an existing page by
WebView.loadUrl("javascript:window.JSname.passData("some data");");
Described here: http://lexandera.com/2009/01/extracting-html-from-a-webview/
Note: this is a security risk - any JS code in this web page could access/call your binded Java object. Best to pass some one-time cookies to loadUrl() and pass them back your Java object to check that it's your code making the call.
In addition to #Peter Knego's approach, there is the inverse:
Use addJavascriptInterface(), per his step #1
For your link, use an onClick attribute on your <a> tag to call out some method on the Java object from step #1, where the Java object turns around and loads the URL into the WebView, and supplies whatever sort of identifying information you want.
Suffice it to say, there's no way to get the information you want from shouldOverrideUrlLoading().

Categories

Resources