Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android multiline TextView , check if text fits , or check is TextView is full

Tags:

I have an Android application layout which contains a multiline TextView. When the screen is in portrait orientation, the TextView can display a different length of text to when running in landscape mode. Also when running on a small screen, the TextView can display a different length of text to when running on a larger screen.

Is there any way I can check if the text fits or will be truncated? Or is there any way I can check if the TextView if full?

The problem is the TextView can potentially contain a different number of lines, depending on whether it is landscape, portrait, small screen, large screen, etc.

Thank you for your advice,

Best regards,

James

like image 990
James Avatar asked Aug 06 '13 18:08

James


2 Answers

These answers didn't work very well for me. Here's what I ended up doing

Paint measurePaint = new Paint(myTextView.getPaint()); float pWidth = measurePaint.measureText("This is my big long line of text. Will it fit in here?"); float labelWidth = myTextView.getWidth(); int maxLines = myTextView.getMaxLines();  while (labelWidth > 0 && pWidth/maxLines > labelWidth-20) {     float textSize = measurePaint.getTextSize();     measurePaint.setTextSize(textSize-1);     pWidth = measurePaint.measureText("This is my big long line of text. Will it fit in here?");     if (textSize < TypedValue.applyDimension(             TypedValue.COMPLEX_UNIT_SP, 7,             getContext().getResources().getDisplayMetrics())) break; }  myTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, measurePaint.getTextSize()); 

I'm not saying this will work for every situation as I'm certainly cutting corners here, but the general idea is to measure the text with the textview's paint and keep shrinking it until it will fit inside the textview.

like image 137
whizzle Avatar answered Sep 20 '22 06:09

whizzle


I have found a "cheeky" solution to the problem of measuring the height of the text in a MULTILINE TextView :-

//Calculate the height of the text in the MULTILINE TextView int textHeight = textView.getLineCount() * textView.getLineHeight(); if (textHeight > textViewHeight) {     //Text is truncated because text height is taller than TextView height } else {     //Text not truncated because text height not taller than TextView height } 

However this solution has some caveats :-

Firstly regarding getLineHeight() , markup within the text can cause individual lines to be taller or shorter than this height, and the layout may contain additional first- or last-line padding. See http://developer.android.com/reference/android/widget/TextView.html#getLineHeight()

Secondly , the application needs to calculate the actual height of the TextView in pixels , and (in the case of my layout) it might not be as simple as textView.getHeight() , and calculation may vary from one layout to another layout.

I would recommend avoiding LinearLayout because the actual pixel height of the TextView can vary depending on text content. I am using a RelativeLayout (see http://pastebin.com/KPzw5LYd).

Using this RelativeLayout, I can calculate my TextView height as follows :-

//Calculate the height of the TextView for layout "http://pastebin.com/KPzw5LYd" int textViewHeight = layout1.getHeight() - button1.getHeight() - button2.getHeight(); 

Hope that helps,

Regards,

James

like image 20
James Avatar answered Sep 19 '22 06:09

James