I'm trying to setup a TextView that contains two lines of text, the second line being shorter than the first one.
Is there a way to set up TextView's Layout instance (acquired via TextView.getLayout()) to truncate the second line, or is there another way that I'm not aware of?
EDIT 1
There were misinterpretations of my intentions: I'm trying to set a textview layouted in a way, that i can set arbitrary text and depending on the length the following happens: If the text is long enough to start a second line, make the first line as wide as the textview's content width is, but the second line should be shorter, for example half as wide. But there should be a second line, if text is long enough.
I checked out abstract class Layout and it's subclasses, there is a function
public int getLineEnd(int line)
that returns the position of the last character for a line. A way could be to set the text, then check out how wide the second line is layouted and then change the text to a shorter version. Anyways, that seems to be a hacky way, which could be made much cleaner and more stable, if I could set a Layout instance myself (via a setter method, such as getter
TextView.getLayout().
Has anyone set a custom Layout to a subclass of Textview?
EDIT 2 / My Own Solution
Found a solution: Now I'm checking if the second line overlaps with the region that was the reason for making the second line shorter. If so, I'm truncating the text. Functions I used:
TextView.getLineCount()
TextView.getLayout().getLineStart(int line)
TextView.getLayout().getLineEnd(int line)
TextView.getPaint().measureText()
Maybe that helps a future anyone.
The question how to set an own layout still remains unanswered, though.