4

i know highlighting in text-view is possible

Highlight Textview Using EditText

and scrolling text-view is also possible (got the scroll code from here and is successfully scrolling too) textView scroll at first line

now the question is, i am searching and i want to highlight that text and navigate to it, when someone presses the search button, the highlighting part is perfect, now i can get the index of the word in the string, but not line number of the string in the text-view,

point is if i want to find a position of certain text in text-view, i.e. which line number is that on, how to do it ?

i found an answer for this, but later i realized its for iOS Search occurrences of a string in a TextView

Community
  • 1
  • 1
legalimpurity
  • 180
  • 3
  • 13

2 Answers2

0

Correct if I am wrong, you want to know the position where a particular text is in a String? If so then you can do it by 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 see that it will tell you the position as to where the word is located but you have to think about case where a word may come more than once. If you are sure that word will occur only once then above code is perfect for you. If not, then you will have to somehow tackle the problem.

Rohan Kandwal
  • 9,112
  • 8
  • 74
  • 107
  • Of course, not. I want to know that if that string is given to a text view. Then at what line number in the text view would a particular text, be. You see, this property cannot be determined by the string itself, as different devices are of different sizes, and so textview will expand in them differently. putting a particular a text on different line numbers on different devices. Hope that clarifies your doubt. – legalimpurity Jan 19 '14 at 21:06
0

This is working for loading a file into a textview so that the user's last selection 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);

page is a TextView, pos and pos2 are ints and the two ends of the last selection by the user (they are the same int if it's just the cursor), scroll is a scrollview containing the textview. It is all in a Handler because of delay issues internal to Android's loading all these objects. Th filename, pos and pos2 are saved as settings on exit.

R Earle Harris
  • 985
  • 9
  • 17