3

I have a TextView inside a ScrollView, the TextView is filled with a large text which only some part of its text is visible on screen at anytime. Now I want to (programmatically) force some part of the text to be visible for user. for example, my text is:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut id ante tellus. Nulla facilisi. Sed gravida fringilla velit, non sollicitudin tellus egestas non. Cras congue, nulla id bibendum tempus, dui dui blandit elit, elementum dictum dolor eros et mi. Nullam leo tellus, aliquet vel tincidunt sed, suscipit nec dui. Nunc dapibus, odio vestibulum hendrerit vestibulum, lorem orci dictum arcu, quis porttitor lorem libero sit amet odio. Sed imperdiet viverra diam, eu porttitor ipsum fringilla et. Suspendisse consectetur sapien ac felis tincidunt dictum. Suspendisse ultrices porta dignissim. Maecenas in nibh sed nisi facilisis dignissim non ut tortor. Pellentesque eleifend tempus sem, sit amet mattis arcu auctor id. Vivamus at tortor rutrum diam vestibulum adipiscing. Suspendisse potenti. Integer sollicitudin laoreet enim ac suscipit. Quisque convallis facilisis pharetra.

I want to force the bold word (tortor in this example) to be visible to the user (maybe by scrolling the textview vertically with a correct value). Is there any way to do this?

EDIT: I don't want to make the word bold! question updated to clarify my request.

yrajabi
  • 637
  • 1
  • 9
  • 28
  • As far as I can see, you can't do this with a standard `TextView`. There's a `getLineBounds` method that will return the Y-coordinate of the baseline for a given line, but I don't see a way to determine which line contains a particular string. – quietmint Aug 27 '12 at 18:16

3 Answers3

1

It would be quite an effort, but using android.graphics.Paint#measureText(...) resp. breakText(...) it should be possible to calculate how for you'd have to scroll to make the desired word visible. You'd have to take into account the font, font-size, size of the view showing the text, etc. etc.

Ridcully
  • 23,362
  • 7
  • 71
  • 86
1

Here is one way to scroll to a particular word:

public void scrollToWord(String string) {
    TextView textView = (TextView) findViewById(R.id.text1);
    String text = textView.getText().toString();

    // Find the first occurrence of the word 
    int position = text.indexOf(string);
    // Calculate how far in the word is: 30%, 40%, 73% 
    int percent = (int) (position / (double) text.length() * 100);
    // Calculate how far to scroll
    int y = textView.getHeight() * percent;

    // Finally scroll
    ((ScrollView) textView.getParent()).scrollTo(0, y);
}

I have two notes on this:

  1. This only scrolls to the first occurrence, but it is easy enough to change to String.indexOf(string, start) and find the second, third, etc.

  2. If you want to scroll to this position as soon as the app has started, you must wait until after onResume() has finished. But there is no method for this... I suggest using a Handler and I can help you with that if you need it.

Hope that helps!

Sam
  • 86,580
  • 20
  • 181
  • 179
  • thanks, I liked your answer and both notes. But, It's obvious that your answer is not exactly accurate (you know, because characters in my font haven't same width, also it depends on the text and words, etc). – yrajabi Aug 27 '12 at 19:06
  • It is only an approximation, not a precise measurement. I don't know of any accurate way to calculate the location of a word due to fonts like yours, different sized screens, etc. But this approximation shouldn't create too much of an offset, you should have a large buffer to work with (say 10% - 30%) depending on the font, field of view, and length of the text. So unless you are trying to display a book in one TextView, I wouldn't worry. (If you are trying to display a large body of text consider WebView with HTML links and JavaScript.) – Sam Aug 27 '12 at 19:32
0

You can use html tags with bold option to make that particular word bold.

Programmer
  • 5,360
  • 10
  • 37
  • 61
  • 1
    I think yrajabi means how to make that word visible, i.e. scroll it into view. – Ridcully Aug 27 '12 at 18:17
  • @Programmer I don't want to make the particular word bold! I want to make it visible to the user. – yrajabi Aug 27 '12 at 18:21
  • @Ridcully Yes, you are right. I want the textview to scroll at a position which the particular word is visible to user. – yrajabi Aug 27 '12 at 18:22
  • @yrajabi - check the answer to this question: http://stackoverflow.com/questions/3506696/auto-scrolling-textview-in-android-to-bring-text-into-view – Ridcully Aug 27 '12 at 18:26