ANDROID - textview find lineindex with a string; search in text form by linenumber

I know that text highlighting is possible

Highlight Textview with EditText

and you can view the scroll text (got the scroll code from here and scrolls successfully) textView scroll the first line

Now the question is what I'm looking for, and I want to select this text and go to it, when someone presses the search button, the highlighting part is perfect, now I can get the index of the word in the line, but not the line number of the line in text form,

point, if I want to find the position of a certain text in a text representation, that is, the line number on which this is done, how to do it?

I found an answer for this, but later I figured it out for iOS Finding a string entry in a TextView

+3
source share
2 answers

That's right, if I'm wrong, do you want to know the position where the specific text is in the line? If so, you can do it using the following

String text = "0123hello9012hello8901hello7890";
String word = "hello";
Log.d("startOfWordPosition",text.indexOf(word)); // prints "4"
Log.d("endOfWordPosition",text.lastIndexOf(word)); // prints "22"

As you can see, it will tell you where the word is, but you need to think about a case where a word may appear more than once. If you are sure that the word will occur only once, then the above code is ideal for you. If not, then you have to somehow solve the problem.

0
source

This works to load the file into a text view so that the user's last choice or cursor position is at the top of the screen.

        new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            page.setSelection(pos, pos2);
            Layout layout = page.getLayout();
            scroll.scrollTo(0, layout.getLineTop(layout.getLineForOffset(pos2)));           }
            }, 500);

- TextView, pos pos2 - ints ( int, ), scroll - , . Handler - , Android . , pos pos2 .

0

All Articles