There is a TextView . It has n-th number of lines. Is it possible to get the last character of the 3rd line? And to be more precise - the position of this symbol.

  • Swing? Awt? Javafx? Android? - VladD
  • Yes, I forgot to specify. xD. android - odradek pm

2 answers 2

The number of lines of a TextView depends on the Layout, so something like this should be done:

 TextView tv; int lines=tv.getLineCount(); //работает только после onLayout()/onMeasure() float height=tv.getLineHeight(); if(lines < 3) // else { Rect rect; tv.getLineBounds(2, rect); //координаты top/left, right/bottom строки //позиция последнего символа в 3-й строке int ch=tv.getOffsetForPosition(rect.right-tv.getMinWidth(), rect.bottom- height/2); //если текст слева-направо } 
  • Again, I did not specify. My text does not fit in one line and is transferred. I am not manually specifying "\ n" so getLineHeight (); always 0 - odradek
  • getLineHeight() cannot be equal to "0". You most likely call it in onCreate() where it is really equal to "0" - read my comments more carefully. And the second I never spoke about carriage transfer! - Barmaley
  • sorry for inattention! I call it from the adapter (where text is being added to text). what am i doing wrong (the adapter is my class that expands the baseAdapter) - odradek

Android:

 String[] lines = textView1.getText().toString().split( "\n" /* или "\r\n" */ ); if( lines.length > 2 ) { String line = lines[2]; return line[line.length - 1]; } else { // а что, если строк меньше трёх? } 
  • this is really cool. thank! I will try. - odradek
  • one
    @atwice The answer is incorrect, since the number of lines in a multi-line TextView depends on the Layout 'and within which is a TextView - so here we have to be more cunning :) - Barmaley
  • does not work! I did not clarify that I do not form the line break myself. if the text does not fit - then the line is moved. for all textbooks (this is all I do in foliage) the value of lines.length () = 1 - odradek