There's no way to achieve this by simply specifying attributes on the layout resource file.
If you look at the setEllipsize()
method in TextView
, you'll see that it says:
Causes words in the text that are longer than the view's width to be ellipsized instead of broken in the middle. You may also want to setSingleLine() or setHorizontallyScrolling(boolean) to constrain the text to a single line. Use null to turn off ellipsizing. If setMaxLines(int) has been used to set two or more lines, only TextUtils.TruncateAt.END and TextUtils.TruncateAt.MARQUEE are supported (other ellipsizing types will not do anything).
From that documentation, you can see that ellipsis in TextView
is actually highly dependent on two factors: view width and number of lines.
As such, I recall that an ellipsis might not show up for a TextView
if it doesn't know the maximum number of lines that your TextView
can have.
So for your intended behavior, this isn't possible because you're depending on the View's height instead. You want to use a specific height then have a TextView calculate the amount of lines that can be shown within that height. Finally, if the amount of lines needed is greater than the amount shown, you want to show ellipses.
That's... not possible with how the ellipses in TextView
is coded. It simply wasn't meant for that.
Therefore, if you want this behavior, you need to either:
Create a custom TextView
for your specific ellipses implementation
Manually calculate the amount of lines visible, then use setMaxLines()
on that TextView
For the second option, you can calculate this by using the TextView
methods getHeight()
and getLineHeight()
.
For example:
textview = (TextView) findViewById(R.id.exampleTextView);
textview.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
final int maxLines = textview.getHeight() / textview.getLineHeight();
textview.setMaxLines(maxLines);
}
});
In my example, I'm doing this inside a ViewTreeObserver
to avoid getHeight()
calling a size of 0.
The second option should be able to get the behavior you want, but if you want this for every TextView
, you should consider creating a custom TextView
instead.