How do I detect whether the text in a TextView
is on one line or not.
if it extends past one line, i want to use 128dp
Toolbar
if its on a single line, i want to use 48dp
How do I detect whether the text in a TextView
is on one line or not.
if it extends past one line, i want to use 128dp
Toolbar
if its on a single line, i want to use 48dp
You can set the android:singleLine
attribute to true
.
Refer to this documentation
if it return 1 then the text is single line as below
int lineCount=mTextView.getLineCount();
Try this:
if(yourTv.getLineCount() > 1) {
yourTv.setTextSize();
else {
yourTv.setTextSize();
}
You can use StaticLayout
TextPaint textPaint = new TextPaint();
textPaint.setTypeface(textView.getTypeface());
textPaint.setTextSize(textView.getTextSize());
textPaint.setTextAlign(Paint.Align.ALIGN_NORMAL);
StaticLayout staticLayout = new StaticLayout(textString, textPaint, widthTextView, Layout.Alignment.ALIGN_NORMAL, 1f, 0f, true);
if (1 == staticLayout.getLineCount()) {
//do smth
} else {
//do smth else
}