The easiest way to limit text to n lines is to use line-clamp . N can be any positive number, but it will be two or three lines most of the time. On my blog, I'm using line-clamp in all post-type components to ensure that it will have the same height as siblings.
text_view. post(new Runnable() { @Override public void run() { if (text_view. getLineCount() < text_view. getMaxLines()) { // do stuff } } });
How to get the maximum number of lines that can be written in TextView? I tried this:
DisplayMetrics metrics = new DisplayMetrics ();
getWindowManager (). getDefaultDisplay (). getMetrics (metrics);
float width = metrics.widthPixels;
float height = metrics.heightPixels;
int lines = (int) (height / textView.getLineHeight ());
System.out.println (lines);
However, this variant gives the wrong information.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With