Android Question Is this possible? Find, Scroll to

canalrun

Well-Known Member
Licensed User
Longtime User
I am writing a small B4A app, just for personal use, that loads and views a text file.

I have a TextEdit added to the panel of a ScrollView.
I read a text file into the TextEdit as a string. This will display as multiple scrolling pages.

I would like to find the position of a word and scroll to that area. I can use IndexOf to find a word and to get the position within the text. Can I relate that position to the ScrollView position to scroll to?

Or, is there a better way?

Thanks.
 

teddybear

Well-Known Member
Licensed User
I am writing a small B4A app, just for personal use, that loads and views a text file.

I have a TextEdit added to the panel of a ScrollView.
I read a text file into the TextEdit as a string. This will display as multiple scrolling pages.

I would like to find the position of a word and scroll to that area. I can use IndexOf to find a word and to get the position within the text. Can I relate that position to the ScrollView position to scroll to?

Or, is there a better way?

Thanks.
Try TextEdit.SelectionStart=postion
 
Upvote 0

canalrun

Well-Known Member
Licensed User
Longtime User
Doing things the "old fogey, quick and dirty" way:

B4X:
    s1 = etMain.Text.ToLowerCase
    i1 = s1.IndexOf(fd.Input.ToLowerCase)

    etMain.SetSelection(i1, fd.Input.Length)
    etMain.RequestFocus

Where s1 is a string to make the IndexOf case insensitive, etMain is an EditText which I have in a ScrollView, and fd is a FileDialog, if I do a RequestFocus after setting a selection, it seems to scroll the ScrollView to make the selection visible.

This works, but is there a better way?
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
you don't seem to allow for multiple matches. there is an additional
level of complexity, i believe, if your search string is found more than
once in the text. you may want, eg, the second or third match, not
the first...

you imply that you simply want to search for a string and have
the view go to that string (if found). with that in mind, i would
turn to the much maligned webview, which will show all the matches,
conveniently highlighted and which allows you to skip (forward
and backward) among the matches (if any). and your text will
be displayed exactly as it would in an edittext.

try the attached. a long sample text is displayed. a yellow edittext
allows you to search for a string. simply tap "enter" or "return" or
"-->" (arrow) after keying in the search string. the search will be case
insensitive and all the matches will be highlighted. to move forward
to the next match, tap the "Find Next" button. "Clear Search" clears
the matches. i didn't implement a "search backward" button.

the example doesn't allow for editing or saving; that would involve
a few extra tweaks.
 

Attachments

  • search.zip
    10.8 KB · Views: 72
Upvote 0

canalrun

Well-Known Member
Licensed User
Longtime User
Thanks. When I get a chance I will have to take a closer look at that.

You are right. I don't account or display multiple matches. I thought of doing that. This is just a quick and dirty app to display a large text file of names and phone numbers from long ago. It's in alphabetical order, so if I search for "Smith", the first match for Bill Smith will be close enough to Wally Smith that it's not worth the extra programming effort.

This is only for my use.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
if you had referred to your text file as essentially a contacts list (hint), something more appropriate could have been suggested for viewing and selecting. a listview is designed for what you want, not an edittext view. just sayin'
 
Upvote 0

RichardN

Well-Known Member
Licensed User
Longtime User
@canalrun .... Just out of curiosity....

If you are just perusing a simple text file, why have you put the EditText inside a ScrollView? The EditText can be full screen, multi-line with a vertical scroll bar and load/display a large string direct from a file.

If you don't need to edit the info then a ListView or Custom ListView would be much tidier.
 
Upvote 0

canalrun

Well-Known Member
Licensed User
Longtime User
@canalrun .... Just out of curiosity....

If you are just perusing a simple text file, why have you put the EditText inside a ScrollView? The EditText can be full screen, multi-line with a vertical scroll bar and load/display a large string direct from a file.

If you don't need to edit the info then a ListView or Custom ListView would be much tidier.

Simplicity is everything :).

I'm using a multiline EditText with the input type set to None so the keyboard doesn't pop up. I have the EditText added to the Panel of a ScrollView because the text is quite a few pages long. Does an EditText have a scrollbar built-in?

There is no reason to edit the text. I don't use a multiline label just for simplicity. The file is a text file created sometime in the early 1980s that I just want to look at from my phone instead of having to power up my laptop each time. The text file has wraparound long lines and carriage return-linefeed terminated lines I considered using a List, but the formatting might not be quite right. A Webview would work, but then I'd have to escape any HTML reserved characters.
 
Upvote 0

RichardN

Well-Known Member
Licensed User
Longtime User
@canalrun I agree, simplicity is everything. Take a look at the project attached.....

No ScrollView is used. In the Designer set the EditText as anchored to full screen, text alignment is TOP+LEFT and Single Line = False. You don't get a scroll bar with the default Android view, maybe you do with the B4X variety.... I can't recall. The various properties of EditText.SelectionStart and SelectionLength should help you with locating text clues.
 

Attachments

  • test.zip
    8.7 KB · Views: 63
Upvote 0
Top