Android Question Webview scroll?

tsteward

Well-Known Member
Licensed User
Longtime User
I'm looking for a way to load my html into a webview and scroll to an element

I have searched and read but don't get it.
Any chance someone would have a small example of how this is done please.

Thank you
 

drgottjr

Expert
Licensed User
Longtime User
please clarify. you can't see html or dom elements in a webview, so scrolling to one is a
little tricky.

do you mean page loads and "automatically" scrolls to some pre-defined location
on the page? this goes back to the earliest days of html and is still used. you could
have several such pre-defined locations.

do you mean interactively "finding" something on a page and scrolling to it?
finding something on a web page involves finding a word or phrase, not an element,
since you can't see them. webview has a such a find method. we have a couple
webview libraries available to facilitate its use, if that's what you're after.

interactively finding dom elements, on the other hand, would be an interesting exercise.
i can visualize a couple different ways of that working, but you'll need to be comfortable
with javascript. plus you really have to set up your html in such a way that the
javascript will behave the way you're expecting (for example, if you dont give id's to
your elements, you can't expect javascript to find specific elements).

in any case, it's unclear - to me, at least - exactly what you're looking to do.
 
Upvote 0

tsteward

Well-Known Member
Licensed User
Longtime User
do you mean page loads and "automatically" scrolls to some pre-defined location
on the page? this goes back to the earliest days of html and is still used. you could
have several such pre-defined locations.
Thank you for your interest
I dynamically create my html and load and display it from the top of the page. No problem
There are occasions that I want to load the html into the webview and automatically scroll to a certain position to show a user a particular paragraph.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
you either read the link too fast or you didn't understand it. the user doesn't have to select anything. the page automatically jumps to the selected spot on load.
anyway, your reference to "dynamically" created content poses a problem. loading a file with the jump link works fine, but you'll need to insert javascript if you're creating content on the fly for rendering in the webview. actually, if it's the same place all the time, you don't have to "insert" javascript; you'll just have to add a little snippet to your html. if the location might be different each time, then you would have to interact with the app by inserting javascript.
both the automatic jump and the jump via javascript work.
 
Last edited:
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
assuming you're loading a string of html text into a webview, here's one way to do it:

1) give the paragraph you're interested in an id: <p id='paragraphofinterest'>
2) create a "proper" webpage. technically, a webview may be able to render sloppy html correctly,
but at least let's pretend we know what we're doing.
3) add a javascript snippet which runs when the page is loaded. this will automatically take the user
to the paragraph of interest.

here is your template:

HTML:
<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
   function init() {
      var el = document.getElementById('paragraphofinterest');
      el.scrollIntoView(true);
   }
</script>
</head>
<body onload='init();'>
blah blah blah ...
more blah blah blah ...
<p id='paragraphofinterest'>
when the page is loaded, this is what the user will see at the top of the webview
</p>
</body>
</html>


another way to do it would be to wait until the page has loaded and then inject javascript from b4a.
this would allow you to scroll to different places when the page loads, depending on what you wanted
the user to see. you would just have to have given appropriate id's to various sections of the text. the
actual javacript would be the same; it would simply be run from b4a.

you have said the user is always directed to the same place, so the suggested solution was made with that in mind.
 
Last edited:
Upvote 0

watesoft

Active Member
Licensed User
Longtime User
I'm looking for a way to load my html into a webview and scroll to an element

I have searched and read but don't get it.
Any chance someone would have a small example of how this is done please.

Thank you
If you konw the content of particular paragraph,you can use "findAllAsync".
B4X:
Sub webview_Event_PageFinished (Url As String)
    dim tt as string
    tt='particular paragraph'    'Or take a string of a certain length from the particular paragraph
    Dim jo As JavaObject = webview 'your webview name'
    jo.RunMethod("findAllAsync", Array(tt))
End sub
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
op says he wants to "scroll to an element". findAllAsync() finds text, not elements.
 
Upvote 0
Top