How much text can be set in the textview for a particular text string with a particular font and size, so that the textview doesn't need to scroll. I mean how much text can be fit inside an TextView, without the need for scrolling. This is similar to How do I determine how much text will fit in a TextView in Android?, but I am not able to find a working solution. Please help.
4 Answers
Assuming you have already looked for other options and there isn't an easy way to do this, here is a theoretical (I haven't tested it) hackish approach that might work.
Create a new class that extends TextView. Then, override a method that would be called after the TextView's content has been updated. There might be a better method to use but for this example let's try onDraw(). This method would check the widths and see if it needs to be able to scroll. If so, it would trim the string and set the text. It would do this in a loop until it didn't need to scroll any longer.
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if(getWidth() < computeHorizontalScrollRange()){
// Requires scrolling, the string is too long
// Do whatever you need to do to trim the string, you could also grab the remaining string and do something with it.
// Set the TextView to use the trimmed string.
}
}
You would want to make sure it didn't get into a endless loop and also check if the width is zero.
You also can look at the various android:ellipsize
options.

- 21,220
- 37
- 123
- 171
-
It works, I used height instead of width. But still looking for better answer. Changing text so many times isn't the best option after all. – Priyank Bolia Jan 25 '11 at 06:41
-
well, there is a problem with the method, I am not able to clip it to the word boundary. – Priyank Bolia Jan 26 '11 at 19:05
I actually needed something similar. I needed a TextView
width a semi-fixed size but the text always needed to fit in there. As the size didn't really matter i created a view that changes the text size until it fits.
import android.content.Context;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.TextView;
/**
* The text inside this TextView will resize to fit.
* It will resize until it fits within the view and padding
*/
public class FitTextView extends TextView {
private float defaultSize = 12;
public FitTextView(Context context) {
super(context);
}
public FitTextView(Context context, AttributeSet attrs) {
super(context, attrs);
defaultSize = getTextSize();
}
public FitTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
defaultSize = getTextSize();
}
/**
* Set the default size. This size is set every time the
* view is resized.
* @param size
*/
public void setDefaultSize(float size) {
defaultSize = size;
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
setTextSize(TypedValue.COMPLEX_UNIT_PX, defaultSize);
fitCharsInView();
super.onSizeChanged(w, h, oldw, oldh);
}
@Override
public void setText(CharSequence text, BufferType type) {
fitCharsInView();
super.setText(text, type);
}
/**
* Decreases the text size until it fits inside the view
*/
public void fitCharsInView() {
int padding = getPaddingLeft() + getPaddingRight();
int viewWidth = getWidth() - padding;
float textWidth = getTextWidth();
int iKillInfite = 0;
int maxIteration = 10000;
while(textWidth > viewWidth && iKillInfite < maxIteration) {
iKillInfite++;
float textSize = getTextSize();
setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize-1);
textWidth = getTextWidth();
}
}
/**
* Gets the width in pixels of the text
* @return
*/
private float getTextWidth() {
return getPaint().measureText(getText().toString());
}
}

- 1,733
- 1
- 14
- 24
This is just a quick brain storm of mine:
- Measure text length in characters
- Get screen dimensions and density information
- Get font size, line height
- Calculate approximate amount of space the given string will occupy given the above data
- Split the string accordingly

- 13,020
- 9
- 37
- 58
-
How to do, that the question, if you look at the question, I have already tried the way proposed in other question link, which is the same as yours. – Priyank Bolia Feb 02 '11 at 02:48
I would extend the TextView
class and override onLayout()
method to trim the string as long as the measured height is larger than computeHorizontalScrollRange()
.
I believe solution is a bit better because:
- It only trims the text once (when the view is being created);
- You can easily store the "trimmed" text if you want to restore it in the future;
- OOP is awesome.

- 1,830
- 15
- 25
-
Haven't tried it yet, but I think it won't work, because how will you found the exact size to be trimmed. Layout method would be called only once. – Priyank Bolia Feb 02 '11 at 17:48
-
You won't find the exact size to be trimmer: you'll iterate until it's the right size. – goncalossilva Feb 02 '11 at 23:50