1

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

IAmGroot
  • 13,760
  • 18
  • 84
  • 154
AskQuestion
  • 346
  • 4
  • 18

4 Answers4

0

You can set the android:singleLine attribute to true.

Refer to this documentation

Kyle Emmanuel
  • 2,193
  • 1
  • 15
  • 22
0

if it return 1 then the text is single line as below

int lineCount=mTextView.getLineCount();
0

Try this:

if(yourTv.getLineCount() > 1) {
    yourTv.setTextSize();
else {
    yourTv.setTextSize();
}
bluish
  • 26,356
  • 27
  • 122
  • 180
Kastriot Dreshaj
  • 1,121
  • 6
  • 16
0

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
    }
inverse12
  • 186
  • 1
  • 7