how to get how many lines are displayed in visible part of TextView
? I use text, which not fully placed in TextView
on every screen resolution.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/logs_text"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
String s = "very big text"
TextView logText = (TextView) view.findViewById(R.id.logs_text);
logText.setText(s);
gpetuhov/textview_limit_text. txt. This will limit number of lines to 1 and if text exceeds limit, TextView will display ... in the end of line.
Line height usually means text size + "padding" top/bottom. So, if your designer write line height 19sp and text size 15sp, it means you need to have extra padding 4sp. 19sp - 15sp = 4sp. To implement it in your layout, use lineSpacingExtra attribute.
To use preset sizes to set up the autosizing of TextView in XML, use the android namespace and set the following attributes: Set the autoSizeText attribute to either none or uniform. none is a default value and uniform lets TextView scale uniformly on horizontal and vertical axes.
android.text.Layout contains this information and more. Use textView.getLayout().getLineCount()
to obtain line count.
Be wary that getLayout()
might return null
if called before the layout process finishes. Call getLayout()
after onGlobalLayout()
or onPreDraw()
of ViewTreeObserver
. E.g.
textView.getViewTreeObserver().addOnPreDrawListener(() -> {
final int lineCount = textView.getLayout().getLineCount();
});
If you want only visible line count you should probably use the approach mentioned in the answer below:
Is there a way of retrieving a TextView's visible line count or range?
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