fweigl
fweigl

Reputation: 22028

Check if a TextView is completely visible

I have a ListView with an adapter attached. The data behind it is a couple of articles. The articles have a title and a subtitle, the length of both varies. Sometimes the text of either one is so long that the TextView doesn't fit the View created by the adapter which has a fixed height. Is there a possibility to find out if both TextViews are completely visible within the View? I know I would have to wait for the layout to be drawn, would do it with getViewTreeObserver().addOnGlobalLayoutListener(...)

enter image description here

Upvotes: 1

Views: 912

Answers (1)

npace
npace

Reputation: 4258

It seems like you can only completely fit two lines per title and subtitle. If I were you, I'd measure the length of the text with the paint that's used to draw it and see if it's less than (width of line) * (number of lines).

For example, consider the following:

boolean doesTitleFitBounds = titleTextView.getPaint().measureText(titleText) < (TITLE_LINE_WIDTH * TITLE_NUM_ROWS);

where TITLE_LINE_WIDTH is the available width for your text in pixels (accounting for paddings/margins etc) and TITLE_NUM_ROWS is the number of rows you have per title. Similarly, you can do a check for the subtitle to see if it fits its own bounds.

Upvotes: 3

Related Questions